搜索

bitsCN.com

MySQL SQL分析 - 参数化查询vs query cache功能

 

query cache,  mysql 5 开始附带的一个功能, 与引擎无关, 只与数据查询语法相关。

 

测试描述: 当前使用中是 MySQL-5.6.14 Linux RHEL6  64 位系统产生环境,  使用 INNODB 引擎, 分配 innodb 2g 内存空间

 

[root@TiYanPlat ~]# uname -aLinux TiYanPlat 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64x86_64 GNU/Linuxmysql> select version();+-----------+| version() |+-----------+| 5.6.14    |+-----------+1 row in set (0.00 sec)mysql> show variables like 'innodb_buffer_pool_size';+-------------------------+------------+| Variable_name           | Value      |+-------------------------+------------+| innodb_buffer_pool_size | 2147483648 |+-------------------------+------------+1 row in set (0.01 sec)

 

 

Query cache 功能:

利用 qeury_cache_size 定义内存大小, 内存用于把用户 SQL 放入内存中, 包括 SQL 语句, 包括SQL 语句执行的结果

假如下一次查询时使用相同的 SQL 语句, 则直接从内存中获得结果, 不再进行 SQL 分析, 不在进行磁盘 I/O 读数据。加速数据查询返回结果。

 

实现目标,开启 QCACHE 功能, 如 my.cnf 定义

query-cache-size=16777216

query-cache-type=ON

查询数据库中是否使用当前功能

mysql> show status like '%qcache%';+-------------------------+----------+| Variable_name           | Value    |+-------------------------+----------+| Qcache_free_blocks      | 440      || Qcache_free_memory      | 12306960 || Qcache_hits             | 13176    || Qcache_inserts          | 29777    || Qcache_lowmem_prunes    | 0        || Qcache_not_cached       | 45862    || Qcache_queries_in_cache | 2098     || Qcache_total_blocks     | 4701     |+-------------------------+----------+8 rows in set (0.02 sec)

 

参数返回结果不再一一详细描述, 自行参考官方文档, 从上返回结果可以看到,使用中的数据库 SQL 命中率 (Qcache_hits) 并不理想,原因与业务有关。

 

SQL 分析一, 使用了 QUERY CACHE 的好处

原理, 利用 EXPLAIN 分析当前 SQL 执行计划, 利用 PROFILE 功能分析当前 SQL 执行计划,过程

执行下面语句进行分析

mysql> explain select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=11024;+----+-------------+--------------+------+---------------+------+---------+------+--------+-------------+| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows   | Extra       |+----+-------------+--------------+------+---------------+------+---------+------+--------+-------------+|  1 | SIMPLE      | tbcrbtnumb0_ | ALL  | NULL          | NULL | NULL    | NULL |180182 | Using where |+----+-------------+--------------+------+---------------+------+---------+------+--------+-------------+1 row in set (0.00 sec)当前语法执行的是全表扫描,另外,需要从 180182 行中扫描相关结果SQL 分析二, 判断第一次执行该 SQL 时候的执行过程mysql> set profiling=1;mysql> select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=11024;+-------+---------------+---------------+-------------+| id41_ | business2_41_ | applicat3_41_ | mobile41_   |+-------+---------------+---------------+-------------+| 30838 |         11024 | TH20121127229 | 02038688592 |+-------+---------------+---------------+-------------+1 row in set (0.30 sec)mysql> show profile;                                                                                                          +--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.000191 || Waiting for query cache lock   | 0.000023 || init                           | 0.000090 || checking query cache for query | 0.000499 || checking permissions           | 0.000030 || Opening tables                 | 0.000131 || init                           | 0.000277 || System lock                    | 0.000042 || Waiting for query cache lock   | 0.000005 || System lock                    | 0.000443 || optimizing                     | 0.000364 || statistics                     | 0.000107 || preparing                      | 0.000059 || executing                      | 0.000019 || Sending data                   | 0.290067 || end                            | 0.000483 || query end                      | 0.000169 || closing tables                 | 0.000158 || freeing items                  | 0.000252 || Waiting for query cache lock   | 0.000063 || freeing items                  | 0.000305 || Waiting for query cache lock   | 0.000015 || freeing items                  | 0.000095 || storing result in query cache  | 0.000145 || cleaning up                    | 0.000330 |+--------------------------------+----------+25 rows in set, 1 warning (0.01 sec)

 

从上面看出, 第一次执行该 SQL, MySQL 需要对 SQL 进行锁缓存,初始化,从缓存中查询是否具备之前缓存过的 SQL,检查用户权限, 表权限,打开表,锁定内存,定制执行计划,执行语句,把数据从磁盘中放入内存中操作,关闭表,锁定数据, 缓存数据等操作, 工作原理与 ORACLE 类似

 

按照 QUERY CACHE 原则, 假如 SQL 语句改变 (tbcrbtnumb0_.business_ring_id=11024) 替换该变量值, 那么该 SQL 会被看作为一个新的 SQL, 这个时候, MySQL 将会对整个 SQL 做一次全新的操作, 如上(黄线标注描述)

 

分析 SQL 三

mysql> select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=11021;+-------+---------------+---------------+-------------+| id41_ | business2_41_ | applicat3_41_ | mobile41_   |+-------+---------------+---------------+-------------+| 30835 |         11021 | TH20121127259 | 02038688592 |+-------+---------------+---------------+-------------+1 row in set (0.34 sec)mysql> show profile;+--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.000795 || Waiting for query cache lock   | 0.000077 || init                           | 0.000045 || checking query cache for query | 0.000337 || checking permissions           | 0.000040 || Opening tables                 | 0.000113 || init                           | 0.000488 || System lock                    | 0.000050 || Waiting for query cache lock   | 0.000030 || System lock                    | 0.000289 || optimizing                     | 0.000512 || statistics                     | 0.000278 || preparing                      | 0.000078 || executing                      | 0.000028 || Sending data                   | 0.322662 || end                            | 0.004777 || query end                      | 0.001703 || closing tables                 | 0.000526 || freeing items                  | 0.000874 || Waiting for query cache lock   | 0.000311 || freeing items                  | 0.001809 || Waiting for query cache lock   | 0.000105 || freeing items                  | 0.000184 || storing result in query cache  | 0.000966 || cleaning up                    | 0.000678 |+--------------------------------+----------+25 rows in set, 1 warning (0.00 sec)

 

上 SQL二,三结果可以看到, 当 WHERE 条件改变, MySQL 会把这两个 SQL 识别为一个新的 SQL, 需要重新操作。

 

SQL 分析四, 假如我们重新执行 SQL 三操作,看看结果如何?(注意,这个时候 QUERY CACHE 真正发挥作用)

mysql> select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=11021;+-------+---------------+---------------+-------------+| id41_ | business2_41_ | applicat3_41_ | mobile41_   |+-------+---------------+---------------+-------------+| 30835 |         11021 | TH20121127259 | 02038688592 |+-------+---------------+---------------+-------------+1 row in set (0.00 sec)mysql> show profile;                                                                                                          +--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.001367 || Waiting for query cache lock   | 0.000071 || init                           | 0.000027 || checking query cache for query | 0.000163 || checking privileges on cached  | 0.000129 || checking permissions           | 0.000386 || sending cached result to clien | 0.000164|| cleaning up                    | 0.000079 |+--------------------------------+----------+8 rows in set, 1 warning (0.01 sec)

 

参考 SQL 分析三,四,数据查询需要使用的时间(绿色标注部分)很明显, SQL 分析四返回速度块了很多,另外,从系统返回的 SQL 分析看出来,系统直接从缓存中返回数据给客户, 没有重复进行 SQL 分析及磁盘 I/O 操作。(蓝色标注部分) 因此, QEURY CACHE 明显加速了 SQL 返回结果。

但必须注意,只有两个 SQL 相同的情况下,才能够获得 QUERY CACHE 的优点。

 

 

参数化查询

参数化查询能够在一定情况下避免了 SQL 注入, 而 ORACLE 也比较推荐使用参数化查询, ORACLE 每次执行 SQL (无论 SQL 是否语法一致)都存在 SQL 分析,

假如SQL语法不一样,则进行硬解析,需要重新定制执行计划

假如SQL语法不一致则进行软解析,避免重复定制执行计划,减少 CPU 消耗,增加 SQL 语句返回时间。

MySQL 官方文档中并没有提出到这点。

 

对 MySQL 进行参数化查询分析

SQL 参数化分析一

 

mysql> set @num=11204;mysql> select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=@num;+-------+---------------+---------------+-----------+| id41_ | business2_41_ | applicat3_41_ | mobile41_ |+-------+---------------+---------------+-----------+| 31051 |         11204 | 1222570       | 85237810  || 31052 |         11204 | 1222570       | 82685386  || 31053 |         11204 | 1222570       | 82783689  || 31054 |         11204 | 1222570       | 82685106  || 31055 |         11204 | 1222570       | 38880051  |+-------+---------------+---------------+-----------+5 rows in set (0.37 sec)mysql> show profile;                                                                                                          +--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.001858 || Waiting for query cache lock   | 0.000089 || init                           | 0.000150 || checking query cache for query | 0.001143 || checking permissions           | 0.000970 || Opening tables                 | 0.000544 || init                           | 0.000743 || System lock                    | 0.000170 || optimizing                     | 0.000332 || statistics                     | 0.000293 || preparing                      | 0.000134 || executing                      | 0.000057 || Sending data                   | 0.438626 || end                            | 0.000694 || query end                      | 0.000221 || closing tables                 | 0.000300 || freeing items                  | 0.000521 || cleaning up                    | 0.000360 |+--------------------------------+----------+18 rows in set, 1 warning (0.01 sec)

 

 

变量值不变情况下,重复执行该 SQL 语句

mysql> select tbcrbtnumb0_.id as id41_, tbcrbtnumb0_.business_ring_id as business2_41_, tbcrbtnumb0_.application_no as applicat3_41_, tbcrbtnumb0_.mobile as mobile41_ from tb_crbt_numbers_load tbcrbtnumb0_ where 1=1 and tbcrbtnumb0_.business_ring_id=@num;+-------+---------------+---------------+-----------+| id41_ | business2_41_ | applicat3_41_ | mobile41_ |+-------+---------------+---------------+-----------+| 31051 |         11204 | 1222570       | 85237810  || 31052 |         11204 | 1222570       | 82685386  || 31053 |         11204 | 1222570       | 82783689  || 31054 |         11204 | 1222570       | 82685106  || 31055 |         11204 | 1222570       | 38880051  |+-------+---------------+---------------+-----------+5 rows in set (0.34 sec)mysql> show profile;                                                                                                          +--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.001188 || Waiting for query cache lock   | 0.000052 || init                           | 0.000030 || checking query cache for query | 0.001791 || checking permissions           | 0.000172 || Opening tables                 | 0.000614 || init                           | 0.001346 || System lock                    | 0.000268 || optimizing                     | 0.000626 || statistics                     | 0.000674 || preparing                      | 0.000439 || executing                      | 0.000028 || Sending data                   | 0.327278 || end                            | 0.000649 || query end                      | 0.000217 || closing tables                 | 0.000408 || freeing items                  | 0.000692 || cleaning up                    | 0.000570 |+--------------------------------+----------+18 rows in set, 1 warning (0.01 sec)

 

分析结果

从数据返回时间上看来(蓝色标注), 使用参数化查询,并没有在时间返回上获得优势。

从SQL执行计划上看来(紫色标注), 相同 SQL 语句使用参数化查询,系统同样会重新定制执行计划,产生磁盘I/O, 并没有想 ORACLE 一样获得性能上的优化。

另外,参数化查询是不会在 query cache 内存块中取结果的。

 

可以看做每次使用参数化查询都会被认为是一个全新的 SQL 进行分析, (没有学习到 ORACLE 的精髓,比较失望)

 

注意,使用参数化查询时, 即时使用 SQL_cache  语法, 也无法使用 query cache 功能。

 

常见开发下有几种选择

1. 直接把 SQL 变量值提交至 MySQL API 执行,(可利用 QUERY CACHE 功能,有部分 SQL 能够进行数据加速)但可能会遇到 SQL 注入, 升级程序麻烦。

2. 利用  procudure, function 等功能先吧 SQL 进行打包然后再调用执行, 类似参数化查询, 无法获得 QUERY CACHE 功能, 令程序清晰, 程序升级,修改比较方便, 并且有效防止了 SQL 注入。

 

bitsCN.com
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

mySQL:字符串数据类型和枚举?mySQL:字符串数据类型和枚举?May 13, 2025 am 12:05 AM

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable长度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

mysql blob:如何优化斑点请求mysql blob:如何优化斑点请求May 13, 2025 am 12:03 AM

优化MySQLBLOB请求可以通过以下策略:1.减少BLOB查询频率,使用独立请求或延迟加载;2.选择合适的BLOB类型(如TINYBLOB);3.将BLOB数据分离到单独表中;4.在应用层压缩BLOB数据;5.对BLOB元数据建立索引。这些方法结合实际应用中的监控、缓存和数据分片,可以有效提升性能。

将用户添加到MySQL:完整的教程将用户添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用户的方法对于数据库管理员和开发者至关重要,因为它确保数据库的安全性和访问控制。1)使用CREATEUSER命令创建新用户,2)通过GRANT命令分配权限,3)使用FLUSHPRIVILEGES确保权限生效,4)定期审计和清理用户账户以维护性能和安全。

掌握mySQL字符串数据类型:varchar vs.文本与char掌握mySQL字符串数据类型:varchar vs.文本与charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串数据类型和索引:最佳实践MySQL:字符串数据类型和索引:最佳实践May 12, 2025 am 12:11 AM

在MySQL中处理字符串数据类型和索引的最佳实践包括:1)选择合适的字符串类型,如CHAR用于固定长度,VARCHAR用于可变长度,TEXT用于大文本;2)谨慎索引,避免过度索引,针对常用查询创建索引;3)使用前缀索引和全文索引优化长字符串搜索;4)定期监控和优化索引,保持索引小巧高效。通过这些方法,可以在读取和写入性能之间取得平衡,提升数据库效率。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)