MySQL中min和max查询优化

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:MySQL max() 函数的需扫描where条件过滤后的所有行:在测试环境中重现:测试版本:Server version:        5.1.58-log MySQL Community Server (GPL)

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()函数往往会造成全表扫描

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-12/156067.htm


以上所述就是小编给大家介绍的《MySQL中min和max查询优化》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言点滴

C语言点滴

赵岩 / 人民邮电出版社 / 2013-10-1 / 45.00元

C语言兼具高级语言和汇编语言的特点,学习起来难度较大,令不少初学者望而生畏。同时,C语言又是一门应用非常广泛的编程语言,在实际应用中如何根据不同的应用场景高效地使用C语言,也是大家非常感兴趣的话题。  《C语言点滴》精心选取C语言一些必备知识,这也是初学者容易搞不清楚、犯错误的知识点,进行深入地分析和介绍,以期帮助读者清除C语言学习之路上的“绊脚石”,降低初学入门的难度,培养继续深入的兴趣。  全......一起来看看 《C语言点滴》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具