• 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 |
    +----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+

  • 相关阅读:
    单div绘制多元素图
    js笔试题系列之二——数组与对象
    JS设计模式——策略模式
    js笔试题系列之三——函数
    zepto.js中的Touch事件
    java定时任务之Scheduled注解
    汤姆大叔送书,咱也科学抢书
    Asp.net Mvc自定义客户端验证(CheckBox列表的验证)
    摆脱烂项目
    我的ORM发展史
  • 原文地址:https://www.cnblogs.com/wxl-dede/p/5351624.html
Copyright © 2020-2023  润新知