数据库教程:Mysql优化技巧之Limit查询的优化分析

前言 在实际业务中对于分页来说是一个比较常见的业务需求。那么就会使用到limit查询,当我们在使用limit查询的时候,在数据比较小、或者只查询前面一部分数据的时候效率是

前言

在实际业务中对于分页来说是一个比较常见的业务需求。那么就会使用到limit查询,当我们在使用limit查询的时候,在数据比较小、或者只查询前面一部分数据的时候效率是很高的。但是当数据量大的时候,或者查询offset数量比较大的时候,如:limit 100000,20效率往往就不尽人意了。通常的一个办法就是limit配合order by,如果order by有对用户的索引的话,效率通常是比较不错的。

对于这种情况,最简单的查询就是 使用覆盖索引,查询某些需要的列。这样的效果是很好的

如下面这个

  mysql> select * from student limit 1000000,1;  +---------+------------+------------+------------+-------+---------------------+  | id  | first_name | last_name | created_at | score | updated_at   |  +---------+------------+------------+------------+-------+---------------------+  | 1000001 | kf9dxbgnui | ylxnpshjph | 2019-07-11 | 97 | 2019-07-11 14:29:59 | |  +---------+------------+------------+------------+-------+---------------------+  1 rows in set (0.31 sec)

可以看到时间

  mysql> explain select score,first_name from student order by created_at limit 1000000,20 g  *************************** 1. row ***************************     id: 1   select_type: simple    table: student   partitions: null     type: index  possible_keys: null     key: time_sorce_name    key_len: 69     ref: null     rows: 1000001    filtered: 100.00    extra: using index  1 row in set, 1 warning (0.00 sec)    mysql>

这样的话查询的列使用到的了覆盖索引,扫描行数会减少很多,但是这样的效果也不是很尽人意,但是如果有其他的查询的话,这样的查询也会变的很慢。

比如我们加上last_name列。

如下

  mysql> select score,first_name,last_name from student order by created_at limit 1000000,1;  +-------+------------+------------+  | score | first_name | last_name |  +-------+------------+------------+  | 86 | knksv2g2fy | wb5qjelzuk |  +-------+------------+------------+  1 row in set (4.81 sec)    mysql>

这个查询需要执行 4秒多的时间。通过分析可以看到这个查询是没有办法使用索引的

  mysql> explain select score,first_name,last_name from student order by created_at limit 1000000,1g  *************************** 1. row ***************************     id: 1   select_type: simple    table: student   partitions: null     type: all  possible_keys: null     key: null    key_len: null     ref: null     rows: 6489221    filtered: 100.00    extra: using filesort  1 row in set, 1 warning (0.00 sec)    mysql>

那么我们现在把查询修改如下

  mysql> select student.score,student.first_name from student inner join (select id from student order by created_at limit 1000000,1 ) as temp using(id);  +-------+------------+  | score | first_name |  +-------+------------+  | 15 | 2qwz  |  +-------+------------+  1 row in set (0.18 sec)

  mysql> explain select student.score,student.first_name,last_name from student inner join (select id from student order by created_at limit 1000000,1 ) as temp using(id);  +----+-------------+------------+------------+--------+---------------+-----------------+---------+---------+---------+----------+-------------+  | id | select_type | table  | partitions | type | possible_keys | key    | key_len | ref  | rows | filtered | extra  |  +----+-------------+------------+------------+--------+---------------+-----------------+---------+---------+---------+----------+-------------+  | 1 | primary  | <derived2> | null  | all | null   | null   | null | null | 1000001 | 100.00 | null  |  | 1 | primary  | student | null  | eq_ref | primary  | primary   | 4  | temp.id |  1 | 100.00 | null  |  | 2 | derived  | student | null  | index | null   | time_sorce_name | 69  | null | 1000001 | 100.00 | using index |  +----+-------------+------------+------------+--------+---------------+-----------------+---------+---------+---------+----------+-------------+  3 rows in set, 1 warning (0.00 sec)

分析结果,可以看到这个时候只查询了1000001一条数据记录。为什么会有这样的变化呢。这种叫延时关联,先通过使用覆盖索引查询返回需要的主键,再根据主键关联原表获得需要的数据,尽可能的减少了需要扫描的行数。

在某些特定的场合,其实有另外一种优化方案的。比如要获取最新的几条插入记录。那么在上一次查询的时候我们可以记录下最后一条记录的主键id(last_id)。
那么查询就可以改为

  select score,first_name,last_name,id from student where id>=last_id order by id asc limit 1

比如last_id=1000000那么这个查询就会从1000000开始。这样的场景不管数据到多大的offset性能都会很好。

总结

以上就是这篇文章的全部内容了,希望数据库技术:Mysql优化技巧之Limit查询的优化分析的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对<计算机技术网(www.ctvol.com)!!>的支持。

需要了解更多数据库技术:Mysql优化技巧之Limit查询的优化分析,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/824455.html

(0)
上一篇 2021年9月17日
下一篇 2021年9月17日

精彩推荐