ITPub博客

首页 > 数据库 > MySQL > MySQL PreparedStatement

MySQL PreparedStatement

原创 MySQL 作者:壹頁書 时间:2014-04-29 20:59:58 0 删除 编辑
世界上的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博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1151799/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339927