MySQL PreparedStatement

壹頁書發表於2014-04-29
世界上的PreparedStatement有兩種
一種是PreparedStatement
另一種是MySQL的PreparedStatement

MySQL版本5.6.14
JDBC版本mysql-connector-java-5.1.6-bin.jar

MySQL JDBC的PreparedStatement僅僅在JAVA端進行了引數的替換。
和Oracle的軟解析,軟軟解析什麼的真是一點關係也沒有。
測試程式如下:

  1. public class Test {
  2.     public static void main(String[] args) throws ClassNotFoundException,
  3.             SQLException {
  4.         long start = System.currentTimeMillis();
  5.         Class.forName("com.mysql.jdbc.Driver");
  6.         Connection connection = DriverManager
  7.                 .getConnection(
  8.                         "jdbc:mysql://127.0.0.1:3306/xx",
  9.                         "xx", "xx");

  10.         connection.setAutoCommit(false);
  11.         PreparedStatement cmd = connection
  12.                 .prepareStatement("insert into test values(?,?)");
  13.         
  14.         for (int i = 0; i < 10; i++) {
  15.             cmd.setInt(1, i);
  16.             cmd.setString(2, "test");
  17.             cmd.executeUpdate();
  18.         }
  19.         connection.commit();
  20.         
  21.         cmd.close();
  22.         connection.close();
  23.         
  24.         long end = System.currentTimeMillis();
  25.         System.out.println(end - start);
  26.         
  27.     }
  28. }
開啟MySQL查詢日誌
mysql> set @@global.general_log=on;
Query OK, 0 rows affected (0.00 sec)

結果如下,明顯只是變數的替換,並沒有任何預解析的過程。
  1. 95 Query SET autocommit=1
  2. 95 Query SET autocommit=0
  3. 95 Query insert into test values(0,'test')
  4. 95 Query insert into test values(1,'test')
  5. 95 Query insert into test values(2,'test')
  6. 95 Query insert into test values(3,'test')
  7. 95 Query insert into test values(4,'test')
  8. 95 Query insert into test values(5,'test')
  9. 95 Query insert into test values(6,'test')
  10. 95 Query insert into test values(7,'test')
  11. 95 Query insert into test values(8,'test')
  12. 95 Query insert into test values(9,'test')
  13. 95 Query commit
  14. 95 Query rollback
  15. 95 Quit
使用useServerPrepStmts和cachePrepStmts顯式開啟預解析
程式如下:

  1. public class Test {
  2.     public static void main(String[] args) throws ClassNotFoundException,
  3.             SQLException {
  4.         StringBuilder builder=new StringBuilder(50);
  5.         builder.append("jdbc:mysql://127.0.0.1:3306/xx?");
  6.         builder.append("useServerPrepStmts=true&");
  7.         builder.append("cachePrepStmts=true&");
  8.         builder.append("prepStmtCacheSqlLimit=256&");
  9.         builder.append("prepStmtCacheSize=256");
  10.         
  11.         long start = System.currentTimeMillis();
  12.         Class.forName("com.mysql.jdbc.Driver");
  13.         Connection connection = DriverManager
  14.                 .getConnection(
  15.                         builder.toString(),
  16.                         "xx", "xx");

  17.         connection.setAutoCommit(false);
  18.         PreparedStatement cmd = connection
  19.                 .prepareStatement("insert into test values(?,?)");

  20.         for (int i = 0; i < 10; i++) {
  21.             cmd.setInt(1, i);
  22.             cmd.setString(2, "test");
  23.             cmd.executeUpdate();
  24.         }
  25.         connection.commit();

  26.         cmd.close();
  27.         connection.close();

  28.         long end = System.currentTimeMillis();
  29.         System.out.println(end - start);

  30.     }
  31. }
檢視MySQL查詢日誌

  1. 96 Query SET autocommit=1
  2. 96 Query SET autocommit=0
  3. 96 Prepare insert into test values(?,?)
  4. 96 Execute insert into test values(0,'test')
  5. 96 Execute insert into test values(1,'test')
  6. 96 Execute insert into test values(2,'test')
  7. 96 Execute insert into test values(3,'test')
  8. 96 Execute insert into test values(4,'test')
  9. 96 Execute insert into test values(5,'test')
  10. 96 Execute insert into test values(6,'test')
  11. 96 Execute insert into test values(7,'test')
  12. 96 Execute insert into test values(8,'test')
  13. 96 Execute insert into test values(9,'test')
  14. 96 Query commit
  15. 96 Query rollback
  16. 96 Quit
顯式開啟預解析之後,確實可以做到一次解析,多次執行。

但是程式測試的結果,效能相差無幾。
我感覺MySQL 解析之後,能夠快取複用的資訊太少,所以沒有明顯的提升。

參考:
http://cs-css.iteye.com/blog/1847772
http://blog.csdn.net/axman/article/details/6913527




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1151799/,如需轉載,請註明出處,否則將追究法律責任。

相關文章