MySQL预编译功能_第1页
MySQL预编译功能_第2页
MySQL预编译功能_第3页
MySQL预编译功能_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MySQL的预编译功能预编译的好处大家平时都使用过JDBC中的PreparedStatement接口,它有预编译功能。什么是预编译功能呢?它有什么好处呢?当客户发送一条SQL语句给服务器后,服务器总是需要校验SQL语句的语法格式是否正确,然后把SQL语句编译成可执行的函数,最后才是执行SQL语句。其中校验语法,和编译所花的时间可能比执行SQL语句花的时间还要多。如果我们需要执行多次insert语句,但只是每次插入的值不同,MySQL服务器也是需要每次都去校验SQL语句的语法格式,以及编译,这就浪费了太多的时间。如果使用预编译功能,那么只对SQL语句进行一次语法校验和编译,所以效率要高。MySQL执行预编译MySQL执行预编译分为如三步:l 执行预编译语句,例如:prepare myfun from select * from t_book where bid=?l 设置变量,例如:set str=b1l 执行语句,例如:execute myfun using str如果需要再次执行myfun,那么就不再需要第一步,即不需要再编译语句了:l 设置变量,例如:set str=b2l 执行语句,例如:execute myfun using str通过查看MySQL日志可以看到执行的过程:使用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();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();cachePrepStmts参数当使用不同的PreparedStatement对象来执行相同的SQL语句时,还是会出现编译两次的现象,这是因为驱动没有缓存编译后的函数key,导致二次编译。如果希望缓存编译后函数的key,那么就要设置cachePrepStmts参数为true。例如:jdbc:mysql:/localhost:3306/test?useServerPrepStmts=true&cachePrepStmts=trueConnection 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论