Mybatis之开启MySQL的预编译功能

本文参考博客https://my.oschina.net/kailuncen/blog/905395,后本身验证的。mysql

    最重要的参数,以下List-1:sql

    List-1bash

useServerPrepStmts=true&&cachePrepStmts=true

 

    首先来看不加List-1参数的状况,以下List-2:服务器

    List-2session

url=jdbc:mysql://127.0.0.1:3306/mjduan?useUnicode=true&characterEncoding=utf-8&useSSL=false

    服务器端MySQL的general日志里面内容以下List-3:优化

    List-3 执行的三条语句被打印出来了url

2018-06-12T23:37:54.142048Z	   10 Query	SET NAMES utf8
2018-06-12T23:37:54.143931Z	   10 Query	SET character_set_results = NULL
2018-06-12T23:37:54.145705Z	   10 Query	SET autocommit=1
2018-06-12T23:37:54.210026Z	   10 Query	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:37:54.236635Z	   10 Query	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:37:54.239985Z	   10 Query	SELECT id,name,age FROM student WHERE id = 1

    JDBC链接的url中添加useServerPrepStmts=true,以下List-4:.net

    List-4日志

url=jdbc:mysql://127.0.0.1:3306/mjduan?useUnicode=true&characterEncoding=utf-8&useSSL=false&&useServerPrepStmts=true

    下面的List-5中,每一个Ececute以前都有一个Prepare,说明执行SQL以前,都对该SQL进行解析、优化了,因此没有起到预编译的做用。code

    List-5

2018-06-12T23:29:34.175252Z	    5 Query	SET NAMES utf8
2018-06-12T23:29:34.176954Z	    5 Query	SET character_set_results = NULL
2018-06-12T23:29:34.178768Z	    5 Query	SET autocommit=1
2018-06-12T23:29:34.214910Z	    5 Prepare	SELECT id,name,age FROM student WHERE id = ?
2018-06-12T23:29:34.244389Z	    5 Execute	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:29:34.265083Z	    5 Close stmt
2018-06-12T23:29:34.266680Z	    5 Prepare	INSERT INTO student(name,age) VALUES(?, ?)
2018-06-12T23:29:34.268304Z	    5 Query	select @@session.tx_read_only
2018-06-12T23:29:34.275772Z	    5 Execute	INSERT INTO student(name,age) VALUES('明明', 20)
2018-06-12T23:29:34.283289Z	    5 Close stmt
2018-06-12T23:29:34.284142Z	    5 Prepare	SELECT id,name,age FROM student WHERE id = ?
2018-06-12T23:29:34.285013Z	    5 Execute	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:29:34.287157Z	    5 Close stmt

    JDBC链接的url加上useServerPrepStmts=true&cachePrepStmts=true,以下List-6所示:

    List-6

url=jdbc:mysql://127.0.0.1:3306/mjduan?useUnicode=true&characterEncoding=utf-8&useSSL=false&&useServerPrepStmts=true&cachePrepStmts=true

    再去看MySQL的general日志,如List-7,此次生效了。

    List-7 执行三条同样的语句,只有一次Prepare

2018-06-12T23:44:03.197228Z	   11 Query	SET NAMES utf8
2018-06-12T23:44:03.202524Z	   11 Query	SET character_set_results = NULL
2018-06-12T23:44:03.206439Z	   11 Query	SET autocommit=1
2018-06-12T23:44:03.310079Z	   11 Prepare	SELECT id,name,age FROM student WHERE id = ?
2018-06-12T23:44:03.378740Z	   11 Execute	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:44:03.427651Z	   11 Execute	SELECT id,name,age FROM student WHERE id = 1
2018-06-12T23:44:03.431207Z	   11 Execute	SELECT id,name,age FROM student WHERE id = 1

注意:

    预编译功能只是在同一个Connection中生效,一个Connection没法获取另外一个Connection中的预编译结果。

    因此,使用Mybatis时,预编译功能只在同一个SqlSession中生效?这种说明不对,俩个SqlSession有可能拿到的是同一个Connection(所以DataSource),这个时候这俩个SqlSession就能共享该Connection中的预编译结果。

    预编译,其实有些人说预准备更恰当,不过如今几本都说预编译了。

 

参考:

    1. https://my.oschina.net/kailuncen/blog/905395

相关文章
相关标签/搜索