MySQL预编译功能详解

所属分类: 数据库 / Mysql 阅读数: 983
收藏 0 赞 0 分享

本文为大家分享了MySQL预编译功能,供大家参考,具体内容如下

1、预编译的好处

  大家平时都使用过JDBC中的PreparedStatement接口,它有预编译功能。什么是预编译功能呢?它有什么好处呢?
  当客户发送一条SQL语句给服务器后,服务器总是需要校验SQL语句的语法格式是否正确,然后把SQL语句编译成可执行的函数,最后才是执行SQL语句。其中校验语法,和编译所花的时间可能比执行SQL语句花的时间还要多。
  如果我们需要执行多次insert语句,但只是每次插入的值不同,MySQL服务器也是需要每次都去校验SQL语句的语法格式,以及编译,这就浪费了太多的时间。如果使用预编译功能,那么只对SQL语句进行一次语法校验和编译,所以效率要高。

2、MySQL执行预编译

MySQL执行预编译分为如三步:
执行预编译语句,例如:prepare myfun from 'select * from t_book where bid=?'
设置变量,例如:set @str='b1'
执行语句,例如:execute myfun using @str
如果需要再次执行myfun,那么就不再需要第一步,即不需要再编译语句了:
设置变量,例如:set @str='b2'
执行语句,例如:execute myfun using @str
通过查看MySQL日志可以看到执行的过程:

3、使用Statement执行预编译

使用Statement执行预编译就是把上面的SQL语句执行一次。

Connection con = JdbcUtils.getConnection();
Statement stmt = con.createStatement();
stmt.executeUpdate("prepare myfun from 'select * from t_book where bid=?'");
stmt.executeUpdate("set @str='b1'");
ResultSet rs = stmt.executeQuery("execute myfun using @str");
while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

stmt.executeUpdate("set @str='b2'");
rs = stmt.executeQuery("execute myfun using @str");

while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

rs.close();
stmt.close();
con.close();

4、useServerPrepStmts参数

  默认使用PreparedStatement是不能执行预编译的,这需要在url中给出useServerPrepStmts=true参数(MySQL Server

4.1之前的版本是不支持预编译的,而Connector/J在5.0.5以后的版本,默认是没有开启预编译功能的)。

  例如:jdbc:mysql://localhost:3306/test?useServerPrepStmts=true
  这样才能保证mysql驱动会先把SQL语句发送给服务器进行预编译,然后在执行executeQuery()时只是把参数发送给服务器。

Connection con = JdbcUtils.getConnection();
String sql = "select * from t_book where bid=?";
PreparedStatement pstmt = con.prepareStatement(sql);

pstmt.setString(1, "b1");
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

pstmt.setString(1, "b2");
rs = pstmt.executeQuery();
while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

rs.close();
pstmt.close();
con.close();

5、cachePrepStmts参数

  当使用不同的PreparedStatement对象来执行相同的SQL语句时,还是会出现编译两次的现象,这是因为驱动没有缓存编译后的函数key,导致二次编译。如果希望缓存编译后函数的key,那么就要设置cachePrepStmts参数为true。例如:
  jdbc:mysql://localhost:3306/test?useServerPrepStmts=true&cachePrepStmts=true

Connection con = JdbcUtils.getConnection();
String sql = "select * from t_book where bid=?";
PreparedStatement pstmt = con.prepareStatement(sql);

pstmt.setString(1, "b1");
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

pstmt = con.prepareStatement(sql);
pstmt.setString(1, "b2");
rs = pstmt.executeQuery();
while(rs.next()) {
 System.out.print(rs.getString(1) + ", ");
 System.out.print(rs.getString(2) + ", ");
 System.out.print(rs.getString(3) + ", ");
 System.out.println(rs.getString(4));
}

rs.close();
pstmt.close();
con.close();


6、打开批处理

MySQL的批处理也需要通过参数来打开:rewriteBatchedStatements=true

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

mysql多表连接查询实例讲解

本篇文章中给大家通过实例代码讲述了mysql多表连接查询的方法,有需要的朋友们可以参考学习下。
收藏 0 赞 0 分享

MySQL设置global变量和session变量的两种方法详解

这篇文章主要介绍了MySQL设置global变量和session变量的两种方法,每种方法给大家介绍的非常详细 ,需要的朋友可以参考下
收藏 0 赞 0 分享

8种手动和自动备份MySQL数据库的方法

作为流行的开源数据库管理系统,MySQL的使用者众多,为了维护数据安全性,数据备份是必不可少的。本文就为大家介绍几种适用于企业的数据备份方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用JDBC连接Mysql数据库会出现的问题总结

这篇文章主要给大家介绍了关于使用JDBC连接Mysql数据库会出现的问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Ubuntu中MySQL的参数文件my.cnf示例详析

这篇文章主要给大家介绍了关于Ubuntu中MySQL的参数文件my.cnf的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mysql具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解决启动MongoDB错误:error while loading shared libraries: libstdc++.so.6:cannot open shared object file:

本文提供了解启动MongoDB时提示:error while loading shared libraries: libstdc++.so.6: cannot open shared object file: 错误的解决方案
收藏 0 赞 0 分享

PHP定时备份MySQL与mysqldump语法参数详解

本文为大家介绍了PHP利用mysqldump命令定时备份MySQL与mysqldump语法参数大全以及定时备份的PHP实例代码
收藏 0 赞 0 分享

定时备份 Mysql并上传到七牛的方法

常见的 MySQL 数据备份方式有,直接打包复制对应的数据库或表文件(物理备份)、mysqldump 全量逻辑备份、xtrabackup 增量逻辑备份等。这篇文章主要介绍了定时备份 MySQL 并上传到七牛 ,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL锁(表锁,行锁,共享锁,排它锁,间隙锁)使用详解

本文全面讲解了MySQL中锁包括表锁,行锁,共享锁,排它锁,间隙锁的详细使用方法
收藏 0 赞 0 分享

MySQL中的排序函数field()实例详解

这篇文章主要给大家介绍了关于MySQL中排序函数field()的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多