This article shares the MySQL precompilation function for your reference. The specific content is as follows 1. Benefits of precompilation We have all used the PreparedStatement interface in JDBC, which has a precompilation function. What is the pre-compilation function? What are the benefits of it? 2. MySQL performs precompilation MySQL performs precompilation in three steps: 3. Use Statement to perform precompilation Using Statement to execute precompilation is to execute the above SQL statement once. 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 parameter By default, PreparedStatement cannot be precompiled. This requires the useServerPrepStmts=true parameter to be given in the URL (MySQL Server Versions prior to 4.1 do not support precompilation, and Connector/J versions after 5.0.5 do not enable precompilation by default). For example: jdbc:mysql://localhost:3306/test?useServerPrepStmts=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.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 parameters When using different PreparedStatement objects to execute the same SQL statement, it will still be compiled twice. This is because the driver does not cache the compiled function key, resulting in secondary compilation. If you want to cache the key of the compiled function, set the cachePrepStmts parameter to true. For example: 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. Turn on batch processing MySQL batch processing also needs to be turned on through parameters: rewriteBatchedStatements=true The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed installation tutorial of Docker under CentOS
>>: How to use http and WebSocket in CocosCreator
Next, I will create two tables and execute a seri...
mysql query data from one table and insert it int...
Html event list General Events: onClick HTML: Mous...
Table of contents CentOS rpm installation and con...
Adding/removing classes to elements is a very com...
Recently, new projects have used springcloud and ...
Introduction to AOP The main function of AOP (Asp...
This article mainly introduces the example analys...
When using MySQL, we often sort and query a field...
Table of contents Preface: 1. Create a project wi...
For evenly distributed layouts, we generally use ...
Table of contents Purpose Module Installation Bas...
Nginx uses a fixed number of multi-process models...
Sample code: import java.util.Random; import java...
1. Prepare the environment (download nodejs and s...