mysql中min和max查询优化

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

原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/9939789.html

时间: 2024-08-02 09:25:13

mysql中min和max查询优化的相关文章

Visual C++中min()和max()函数的使用

标准库在<algorithm>头中定义了两个模板函数std::min() 和 std::max().通常用它可以计算某个值对的最小值和最大值. 可 惜在 Visual C++ 无法使用它们,因为没有定义这些函数模板.原因是名字min和max与<windows.h>中传统的min/max宏定义有冲突.为了解决 这个问题,Visual C++ 定义了另外两个功能相同的模板:_cpp_min() 和 _cpp_max().我们可以用它们来代替std::min() 和 std::max()

numpy中min和max的用法

min和max用法差不多,只写min 的 min返回数组中的最小值 1.若是数据集.min()里面没有写参数,则选择所有数据里面的最小值,因为给的数组可能有多维,但是没指定参数的话,就选一个最小值 2.min(0),选择第一维中的 所有 最小值,比如说是一个数组[1,2],[3,4],最终结果是[1,2]. [1,2]和[3,4]这两个是第一维,就是1和3比较,2和4比较 3.min(1),选择第二维中的最小值,同样是上面的例子,1和2比较,3和4比较,最终结果[1,3]

关于mysql中limit方面的查询优化

当数据库中的数据量较大时,读取数据需要用limit来做分页 limit offset, n 测试中发现,使用limit时,offset的值越大查询时长越长,查询效率越低 select id from table limit 2000000, 100; 此时,可以用where+id的方法来修改此sql语句,优化查询: select id from table where id>2000000 limit 100; 因为id一般都是主键索引,查询效率较高 原文地址:https://www.cnblo

【MySQL】MySQL中针对大数据量常用技术_创建索引+缓存配置+分库分表+子查询优化(转载)

原文地址:http://blog.csdn.net/zwan0518/article/details/11972853 目录(?)[-] 一查询优化 1创建索引 2缓存的配置 3slow_query_log分析 4分库分表 5子查询优化 二数据转移 21插入数据 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无法满足快速查询与插入数据的需求.这个时候NoSQL的出现暂时解决了这一危机.它通过降低数据的安全性,减少对事务

MySql中查询优化方法

最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当mysql表的数据量达到百万级时,普通SQL查询效率呈直线下降,而且如果where中的查询条件较多时,其查询速度简直无法容忍.曾经测试对一个包含400多万条记录(有索引)的表执行一条条件查询,其查询时间竟然高达40几秒,相信这么高的查询延时,任何用户都会抓狂.因此如何提高sql语句查询效率,显得十分重要.以下是网上流传比较广泛的30种SQL查询语句优化方法: 1.应尽量避免在

3.python小技巧分享-使用min和max函数去找字典中的最大值和最小值

睡前分享一个小技巧- 使用min和max函数来巧妙的查找一个字典中的最大value和最小value. 比如说,现在有一个字典,字典的key是用户名,value则是这个用户的账户有多少钱. 现在想要找出账户内余额最多的用户,请问如何实现? d1 = {'suhaozhi':12345,'tony':4513,'eric':135,'jolin':13000000} 很简单,只要使用zip函数结合max函数就可以做到了. print max(zip(d1.values(),d1.keys())) #

Mysql中max函数的使用说明

MySQL 中的Max() 函数是用来找出记录集中最大值的记录: 注意: 对字符型数据的最大值,是按照首字母由A-Z的顺序排列,越往后,其值越大.当然,对于汉字则是按照其全拼拼音排列的,若首字符相同,则比较下一个字符,以此类推. 对于日期时间类型的数据也可以求其最大/最小值,其大小排列就是日期时间的早晚,越早认为其值越小, 原文地址:http://blog.51cto.com/59465168/2324977

MySQL中MAX函数与Group By一起使用的注意事项(转)

mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+-------+------+-------+ | 1 | qiu | 22 | 1 | | 2 | liu | 42 | 1 | | 4 | zheng | 20 | 2 | | 3 | qian | 20 | 2 | | 0 | wang | 11 | 3 | | 6 | li | 33 | 3 | +----+

mysql中的sql查询优化

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在where 及order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设置默认值0,确保表中num列没有null值,然后这样查询: select id from t where num=0 3.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用