前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL中min和max查询优化

MySQL中min和max查询优化

作者头像
星哥玩云
发布2022-08-17 19:47:54
1.2K0
发布2022-08-17 19:47:54
举报
文章被收录于专栏:开源部署开源部署

MySQL max() 函数的需扫描where条件过滤后的所有行:

在测试环境中重现:

测试版本:Server version:        5.1.58-log MySQL Community Server (GPL)

testtable表中的索引

mysql> show index from testtable; +-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table    | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | testtable |          0 | PRIMARY    |            1 | id          | A        |          2 |    NULL | NULL  |      | BTREE      |        | | testtable |          1 | key_number |            1 | number      | A        |          2 |    NULL | NULL  | YES  | BTREE      |        | +-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

对比的sql为:

 select sql_no_cache  max(id) from testtable where number=98;  select sql_no_cache id from testtable where number=98 order by id desc limit 1;

查看执行计划:

mysql> explain select sql_no_cache  max(id) from testtable where number=98; +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ | id | select_type | table    | type | possible_keys | key        | key_len | ref  | rows | Extra                    | +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ |  1 | SIMPLE      | testtable | ref  | key_number    | key_number | 5      | const |    4 | Using where; Using index | +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ 1 row in set (0.00 sec)

mysql> explain select sql_no_cache id from testtable where number=98 order by id desc limit 1; +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ | id | select_type | table    | type | possible_keys | key        | key_len | ref  | rows | Extra                    | +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ |  1 | SIMPLE      | testtable | ref  | key_number    | key_number | 5      | const |    4 | Using where; Using index | +----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+ 1 row in set (0.00 sec) 执行计划显示完全一样。

其中,number为98 对应的记录有4行:

mysql> select count(*) from testtable where number=98;

+----------+ | count(*) | +----------+ |        4 | +----------+ 1 row in set (0.00 sec)

执行前查看innodb_rows_read

#innodb_rows_read  从InnoDB表读取的行数

mysql> show global status like 'innodb_rows_read'; +------------------+-------+ | Variable_name    | Value | +------------------+-------+ | Innodb_rows_read | 1022  | +------------------+-------+ 1 row in set (0.00 sec)

执行sql1 mysql> select sql_no_cache  max(id) from testtable where number=98; +---------+ | max(id) | +---------+ |      13 | +---------+

1 row in set (0.00 sec)

执行后查看innodb_rows_read,发现innodb_rows_read增加了4,即number为98 对应的记录有4行 mysql> show global status like 'innodb_rows_read'; +------------------+-------+ | Variable_name    | Value | +------------------+-------+ | Innodb_rows_read | 1026  | +------------------+-------+ 1 row in set (0.00 sec)

执行sql2 mysql> select sql_no_cache id from testtable where number=98 order by id desc limit 1; +----+ | id | +----+ | 13 | +----+ 1 row in set (0.00 sec)

执行后查看innodb_rows_read,发现innodb_rows_read增加了1 mysql> show global status like 'innodb_rows_read'; +------------------+-------+ | Variable_name    | Value | +------------------+-------+ | Innodb_rows_read | 1027  | +------------------+-------+ 1 row in set (0.00 sec)

测试得出:

 select sql_no_cache  max(id) from testtable where number=98;

需要读取 number=98 的所有行,才能得到最大的id

 select sql_no_cache id from testtable where number=98 order by id desc limit 1;

由于id是主键,number是第二索引,只需扫描1行即可得到最大的id

请慎用max()函数,特别是频繁执行的sql,若需用到可转化为测试中的  order by id desc limit 1

因为往往min()或者max()函数往往会造成全表扫描

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档