mysql一括操作性能比較

2868 ワード

  • 1.データベースを操作する場合、プリコンパイル(PreparedStatement)を使用すると、
  • のパフォーマンスが大幅に向上します.
            1). コードの可読性とメンテナンス性.(パラメータをプレースホルダで表しますか?)
            2).パフォーマンスの最大化(事前コンパイル)が可能であり、MySQLではPreparedStatementのパフォーマンス最適化はサポートされていません.
            3).安全性を保証できる
  • 2.プリコンパイル効率実装(sql送信データベースサーバ)
  • セキュリティ分析-->構文解析-->構文コンパイル-->実行の選択-->結果セットの戻り(結果)
    プリコンパイルプール:DBMSに送信されたsql文がプリコンパイルプールに存在するかどうかを判断し、存在する場合は実行ステップを選択し、存在しない場合は上記の手順に従って1回実行し、sqlをプリコンパイルプールに保存します(注意:Oracleはプリコンパイルをサポートし、mysqlはサポートしません)
    三.mysqlの例
      
    //          
    	//InnoDB:15369ms
    	//MyISAM:8464ms
    	@Test
    	public void testSaveByStatement() throws Exception {
    		
    		Connection conn = JdbcUtil.getConnection();
    		Statement st = conn.createStatement();
    		long begin = System.currentTimeMillis();
    		for (int j = 1; j < 5000; j++) {
    			String sql = "insert into t_student (name,age) values('tom',"+ j+")";
    			st.executeUpdate(sql);
    		}
    		long end = System.currentTimeMillis();
    		System.out.println(end - begin);
    		JdbcUtil.realse(null, st, conn);
    	}
    	
    	//        
    	//InnoDB:14641ms
    	//MyISAM:7464ms
    	@Test
    	public void testBatchSaveByStatement() throws Exception {
    		
    		Connection conn = JdbcUtil.getConnection();
    		Statement st = conn.createStatement();
    		long begin = System.currentTimeMillis();
    		for (int j = 1; j < 5000; j++) {
    			String sql = "insert into t_student(name,age) values('tom',"+ j+")";
    			st.addBatch(sql);  //        
    			if(j % 200 == 0){
    				st.executeBatch(); //   
    				st.clearBatch();  //      
    			}
    		}
    		long end = System.currentTimeMillis();
    		System.out.println(end - begin);
    		JdbcUtil.realse(null, st, conn);
    	}
    	
    	//          
    	//InnoDB:13630
    	//MyISAM:12323
    	@Test
    	public void testSaveByPrepareStatement() throws Exception {
    		String sql = "insert into t_student(name,age) values('tom',?)";
    		Connection conn = JdbcUtil.getConnection();
    		PreparedStatement ps =  conn.prepareStatement(sql);
    		long begin = System.currentTimeMillis();
    		for (int j = 1; j < 5000; j++) {
    			ps.setInt(1, j);
    			ps.executeUpdate();
    			ps.clearParameters();
    		}
    		long end = System.currentTimeMillis();
    		System.out.println(end - begin);
    		JdbcUtil.realse(null, ps, conn);
    	}
    	
    	//        
    	//InnoDB:11118
    	//MyISAM:4546
    	@Test
    	public void testBatchSaveByByPrepareStatement() throws Exception {
    
    		String sql = "insert into t_student(name,age) values('tom',?)";
    		Connection conn = JdbcUtil.getConnection();
    		PreparedStatement ps =  conn.prepareStatement(sql);
    		long begin = System.currentTimeMillis();
    		for (int j = 1; j < 5000; j++) {
    			ps.setInt(1, j);
    			ps.addBatch();
    			if(j % 200 == 0){
    				ps.executeBatch();
    				ps.clearBatch();
    			}
    		}
    		long end = System.currentTimeMillis();
    		System.out.println(end - begin);
    		JdbcUtil.realse(null, ps, conn);
    	}