首页 > 其他分享 >PreparedStatement预编译与Mybatis

PreparedStatement预编译与Mybatis

时间:2022-12-26 20:44:39浏览次数:67  
标签:PreparedStatement mysql 编译 缓存 sql Mybatis pStmt

大家都知道,Mybatis内置参数,形如#{xxx}的,均采用了sql预编译的形式,大致知道mybatis底层使用PreparedStatement,过程是先将带有占位符(即”?”)的sql模板发送至mysql服务器,由服务器对此无参数的sql进行编译后,将编译结果缓存,然后直接执行带有真实参数的sql。如果你的基本结论也是如此,那你就大错特错了。

  1. mysql是否默认开启了预编译功能?
    mysql是否支持预编译有两层意思:

db是否支持预编译
连接数据库的url是否指定了需要预编译,比如:jdbc:mysql://127.0.0.1:3306/user?useServerPrepStmts=true,useServerPrepStmts=true是非常非常重要的参数。如果不配置PreparedStatement 实际是个假的 PreparedStatement


SELECT VERSION();   // 5.6.24-log
 
SHOW GLOBAL STATUS LIKE '%prepare%'; //Com_stmt_prepare 4  代表被执行预编译次数
 
//开启server日志
SHOW VARIABLES LIKE '%general_log%';
SHOW VARIABLES LIKE 'log_output';
 
SET GLOBAL general_log = ON;
SET GLOBAL log_output='table';
 
TRUNCATE TABLE mysql.general_log;    
SELECT * FROM mysql.general_log;  // 有Prepare命令

注意:mysql预编译功能有版本要求,包括server版本和mysql.jar包版本。以前的版本默认useServerPrepStmts=true,5.0.5以后的版本默认useServerPrepStmts=false

  1. 预编译缓存是服务端还是客户端缓存?
    开启缓存:useServerPrepStmts=true&cachePrepStmts=true,设置了useServerPrepStmts=true,虽然可以一次编译,多次执行

它可以提高性能,但缓存是针对连接的,即每个连接的缓存都是独立的,并且缓存主要是由mysql-connector-java.jar实现的。

当手动调用prepareStatement.close()时PrepareStatement对象只会将关闭状态置为关闭,并不会向mysql发送关闭请求,prepareStatement对象会被缓存起来,等下次使用的时候直接从缓存中取出来使用。没有开启缓存,则会向mysql发送closeStmt的请求。

  1. 开启预编译性能更高?
    也就是说预编译比非预编译更好?其实不然,不行自己可试试看。

public class PreparedStatement_test {
    private String url = "jdbc:mysql://localhost:3306/batch";
    private String sql = "SELECT * FROM export_request WHERE id = ?";
    private int maxTimes = 100000;
 
    @Test
    public void go_driver() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection(url, "root", "123456");
        // PreparedStatement
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < maxTimes; i++) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1, Math.abs(new Random().nextLong()));
            // execute
            stmt.executeQuery();
        }
        System.out.println("go_driver:" + stopwatch);
    }
 
    @Test
    public void go_setPre() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection(url + "?useServerPrepStmts=true", "root", "123456");
        // PreparedStatement
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < maxTimes; i++) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1, Math.abs(new Random().nextLong()));
            // execute
            stmt.executeQuery();
        }
        System.out.println("go_setPre:" + stopwatch);
    }
 
    @Test
    public void go_setPreCache() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection(url + "?useServerPrepStmts=true&cachePrepStmts=true", "root", "123456");
        // PreparedStatement
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setLong(1, Math.abs(new Random().nextLong()));
        // execute
        stmt.executeQuery();
        stmt.close();//非常重要的,一定要调用才会缓存
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < maxTimes; i++) {
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1, Math.abs(new Random().nextLong()));
            // execute
            stmt.executeQuery();
        }
        System.out.println("go_setPreCache:" + stopwatch);
    }
}

基准为10w次单线程:
非预编译::23.78 s
预编译:41.86 s
预编译缓存:20.55 s

经过实践测试,对于频繁适用的语句,使用预编译+缓存确实能够得到可观的提升,但对于不频繁适用的语句,服务端编译会增加额外的round-trip。开发实践中要视情况而定。

  1. 从源码中验证

预编译原理(connection -> prepareStatement )

预编译:JDBC42ServerPreparedStatement(需将对应占位符)

非预编译:JDBC42PreparedStatement(完整的SQL)


//com.mysql.jdbc.ConnectionImpl中的代码片段
  /**
     * JDBC 2.0 Same as prepareStatement() above, but allows the default result
     * set type and result set concurrency type to be overridden.
     * 
     * @param sql
     *            the SQL query containing place holders
     * @param resultSetType
     *            a result set type, see ResultSet.TYPE_XXX
     * @param resultSetConcurrency
     *            a concurrency type, see ResultSet.CONCUR_XXX
     * @return a new PreparedStatement object containing the pre-compiled SQL
     *         statement
     * @exception SQLException
     *                if a database-access error occurs.
     */
    public java.sql.PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        synchronized (getConnectionMutex()) {
            checkClosed();
            //
            // FIXME: Create warnings if can't create results of the given type or concurrency
            //当Client开启 useServerPreparedStmts 并且Server支持 ServerPrepare
            PreparedStatement pStmt = null;
            boolean canServerPrepare = true;
            String nativeSql = getProcessEscapeCodesForPrepStmts() ? nativeSQL(sql) : sql;
            if (this.useServerPreparedStmts && getEmulateUnsupportedPstmts()) {
                canServerPrepare = canHandleAsServerPreparedStatement(nativeSql);
            }
            
            if (this.useServerPreparedStmts && canServerPrepare) {// 从缓存中获取 pStmt
                if (this.getCachePreparedStatements()) {
                    synchronized (this.serverSideStatementCache) {
                        pStmt = (com.mysql.jdbc.ServerPreparedStatement) this.serverSideStatementCache
                                .remove(makePreparedStatementCacheKey(this.database, sql));
 
                        if (pStmt != null) {
                            ((com.mysql.jdbc.ServerPreparedStatement) pStmt).setClosed(false);
                            pStmt.clearParameters();// 清理上次留下的参数
                        }
 
                        if (pStmt == null) {
                            try {// 向Server提交 SQL 预编译,实例是JDBC42ServerPreparedStatement
                                pStmt = ServerPreparedStatement.getInstance(getMultiHostSafeProxy(), nativeSql, this.database, resultSetType,
                                        resultSetConcurrency);
                                if (sql.length() < getPreparedStatementCacheSqlLimit()) {
                                    ((com.mysql.jdbc.ServerPreparedStatement) pStmt).isCached = true;
                                }
 
                                pStmt.setResultSetType(resultSetType);
                                pStmt.setResultSetConcurrency(resultSetConcurrency);
                            } catch (SQLException sqlEx) {
                                // Punt, if necessary
                                if (getEmulateUnsupportedPstmts()) {
                                    pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
 
                                    if (sql.length() < getPreparedStatementCacheSqlLimit()) {
                                        this.serverSideStatementCheckCache.put(sql, Boolean.FALSE);
                                    }
                                } else {
                                    throw sqlEx;
                                }
                            }
                        }
                    }
                } else {
                    try {    // 向Server提交 SQL 预编译。
                        pStmt = ServerPreparedStatement.getInstance(getMultiHostSafeProxy(), nativeSql, this.database, resultSetType, resultSetConcurrency);
 
                        pStmt.setResultSetType(resultSetType);
                        pStmt.setResultSetConcurrency(resultSetConcurrency);
                    } catch (SQLException sqlEx) {
                        // Punt, if necessary
                        if (getEmulateUnsupportedPstmts()) {
                            pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
                        } else {
                            throw sqlEx;
                        }
                    }
                }
            } else {// Server不支持 ServerPrepare,实例是JDBC42PreparedStatement
                pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
            }
            return pStmt;
        }
    }

JDBC42ServerPreparedStatement->close,缓存


//com.mysql.jdbc.ServerPreparedStatement中选取代码
@Override
public void close() throws SQLException {
    MySQLConnection locallyScopedConn = this.connection;
 
    if (locallyScopedConn == null) {
        return; // already closed
    }
 
    synchronized (locallyScopedConn.getConnectionMutex()) {
        if (this.isCached && isPoolable() && !this.isClosed) {
            clearParameters();// 若开启缓存,则只会将状态位设为已关闭,并且刷新缓存
            this.isClosed = true;
            this.connection.recachePreparedStatement(this);
            return;
        }
        //没有开启缓存,则会向mysql发送closeStmt的请求
        realClose(true, true);
    }
}
 public void recachePreparedStatement(ServerPreparedStatement pstmt) throws SQLException {
        synchronized (getConnectionMutex()) {
            if (getCachePreparedStatements() && pstmt.isPoolable()) {
                synchronized (this.serverSideStatementCache) {
                    Object oldServerPrepStmt = this.serverSideStatementCache.put(makePreparedStatementCacheKey(pstmt.currentCatalog, pstmt.originalSql), pstmt);
                    if (oldServerPrepStmt != null) {// 将sql语句作为key,reparedStatement对象作为value存放到缓存中
                        ((ServerPreparedStatement) oldServerPrepStmt).isCached = false;
                        ((ServerPreparedStatement) oldServerPrepStmt).realClose(true, true);
                    }
                }
            }
        }
    }
  1. 总结
    预编译显式开启(在url中指定useServerPrepStmts=true),否则PreparedStatement不会向mysql发送预编译(Prepare命令)的请求;
    每次向mysql发送预编译请求,不管之前有没有执行过此SQL语句,只要请求的命令是Prepare或Query,mysql就会重新编译一次SQL语句,并返回此链接当前唯一的Statement ID,后续执行SQL语句的时候,程序只需拿着Statement ID和参数就可以了;
    当预编译的SQL语句有语法错误,则mysql的响应会携带错误信息,但此错误信息JDBC感知不到(或者说mysql-connetor-java.jar包里的实现将其忽略掉了),此时还会继续往下执行代码,当执行到executeXxx()方法时,由于没有Statement ID(所以就会将拼接完整的SQL语句值已经将占位符(?)替换掉再次发给mysql请求执行,此时mysql响应有语法错误,这时JDBC就会抛出语法错误异常),所以检查语法那一步实在mysql-server中做的(通过抓包可以看到);
    PreparedStatement对性能的提高是利用缓存实现的,需要显式开启(在url中指定cachePrepStmts=true),此缓存是mysql-connetor-java.jar包里实现的(非mysql-server中的缓存),缓存的key是完整的sql语句,value是PreparedStatement对象。放入缓存是PreparedStatement.close()触发的,所以只要缓存PreparedStatement对象没有关闭,你不管调用多少次connection.prapareStatement(sql)对相同的sql语句进行预编译,都会将预编译的请求发给mysql,mysql也会对每一个sql语句不管是否相同进行预编译,并生成一个唯一的Statement ID并返回;
    缓存是针对链接的,每个链接都是独立的,不共享缓存
    ————————————————
    版权声明:本文为CSDN博主「布道」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/alex_xfboy/article/details/83901351

标签:PreparedStatement,mysql,编译,缓存,sql,Mybatis,pStmt
From: https://www.cnblogs.com/sisiyibailiu/p/17006824.html

相关文章

  • MyBatis-Plus基础
    一、MyBatis-Plus1.1简介MyBatis-Plus(opensnewwindow)(简称MP)是一个MyBatis(opensnewwindow)的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高......
  • mybatis 之定义拦截器 控制台SQL的打印
    类型先说明Mybatis中可以被拦截的类型具体有以下四种:1.Executor:拦截执行器的方法。2.ParameterHandler:拦截参数的处理。3.ResultHandler:拦截结果集的处理。4.StatementHa......
  • 1、C++环境与编译
    C++环境与编译章节概览我们将在本章讨论以下内容:a.什么是一个程序(抽象与实现)b.什么是程序的程序的开发环境c.什么是程序的编译d.什么是IDE、compiler、assembler、li......
  • C++项目中编译部分C的代码
    在C++项目中如果真能编译部分C的代码,那么一定会用到一下语句#ifdef__cplusplusextern"C"{#endif/*...*/#ifdef__cplusplus}#endif下面......
  • mybatis中的土鸡杂鱼
    mybatis中的土鸡杂鱼目录mybatis中的土鸡杂鱼1、mapper接口为什么要和mapper.xml在同一个路径下?2、主键生成为什么配置一个字段就可以?原理3、为什么默认使用的是预编译Pre......
  • 迅为RK3588开发板单独编译Android固件
    进入到3588-android12文件夹,输入以下命令设置java版本为1.8版本,如下图所示:sourcejavaenv.shjava-version输入以下命令使能编译环境:sourcebuild/envsetup.shlunch......
  • 数据库悲观锁和乐观锁使用Mybatis
    一下是转载的oracle和Mysql两种数据库悲观锁和乐观锁机制及乐观锁实现方式:一、OracleOracle数据库悲观锁与乐观锁是本文我们主要要介绍的内容。有时候为了得到最大的性能,一......
  • 关于UTF-8、GBK编码以及编译时charset的指定的一些总结
    首先分清两个概念:C文件中(转换为16进制)汉字的编码,编译后bin文件中汉字的编码。gcc的-finput-charset和-fexec-charset两个选项的存在就是为了实现这两者的转换。默认......
  • 什么?Coolbpf 不仅可以远程编译,还可以发现网络抖动! | 龙蜥技术
    近日,在 ​​2022云栖大会龙蜥峰会eBPF&Linux稳定性专场​​上,来自eBPF技术探索SIGMaintainer的毛文安分享了《Coolbpf的应用实践》技术演讲,以下为本次演讲内容:......
  • Mybatis精讲课程详情
    Mybatis精讲课程详情一.课程介绍本套视频课程为 IT私塾 出品的《跟一一哥学Java》系列课程之Mybatis精讲课程。分类名称操作系统无要求环境要求JDK1.7+,MySQL5.5+注意:本课......