mysql查询最小的信息_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 |

+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值