MySQL 查询最大最小值优化

Posted rikewang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 查询最大最小值优化相关的知识,希望对你有一定的参考价值。

1. 假设你使用了Innodb存储引擎
2. 假设你在innodb设定了主键(聚集索引)

3. 因为聚集索引页面之间是通过双向链表链接,页按照主键的顺序排序
每个页中的记录也是通过双向链表维护。聚集索引上存储了主键的值
由于B+树的特性,最左端的叶子节点存储最小的值,最右端的叶子节点存储最大的值。

4. 最小值的一般方法:我们可以看到没有使用key,设计的行299600行
root:employees 11:00 > select min(emp_no) from employees where gender=‘M‘;
+-------------+
| min(emp_no) |
+-------------+
| 10001 |
+-------------+
1 row in set (0.11 sec)

root:employees 11:07 > explain select min(emp_no) from employees where gender=‘M‘;
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+

5. 利用上面的说明,取出最左端的叶子节点即可。此时我们看到执行时间很短,虽然explain结果比较困惑!
root:employees 11:12 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;
+--------+
| emp_no |
+--------+
| 10001 |
+--------+
1 row in set (0.00 sec)

root:employees 11:13 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+

6. 同样我们执行max最大值的时候,可以先倒排在取出第一个数据。因为页之间通过双向链表链接。
root:employees 11:18 > select max(emp_no) from employees where gender=‘M‘;
+-------------+
| max(emp_no) |
+-------------+
| 499999 |
+-------------+
1 row in set (0.22 sec)

root:employees 11:18 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+--------+
| emp_no |
+--------+
| 499999 |
+--------+
1 row in set (0.00 sec)

root:employees 11:18 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

7.我们在查询范围的使用,也可以利用B+树的特性来迅速查询到我们想要的信息。因为B+树的索引页存储了主键的范围;
root:employees 11:22 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

以上是关于MySQL 查询最大最小值优化的主要内容,如果未能解决你的问题,请参考以下文章

mysql 查询最大最小值和对应的时间

mysql 多表查询 比较两个字段最大、最小值,并显示对应字段

mysql 中如何拿到每条数据最大值和最小值

Mysql查询,范围内的最大值和最小值没有得到正确的输出

如何在 MySQL 中获取记录数以及最小值/最大值?

mysql AUTO_INCREMENT 能设置区间吗?即有一个自增最小值和最大值,最大值自己指定不是该类型的最大值