在实际业务中对于分页来讲是一个比较常见的业务需求。那么就会使用到limit
查询,当咱们在使用Limit查询的时候,在数据比较小、或者只查询前面一部分数据的时候效率是很高的。可是当数据量大的时候,或者查询offset
数量比较大的时候,如:limit 100000,20
效率每每就不尽人意了。一般的一个办法就是Limit
配合order by
,若是order by
有对用户的索引的话,效率一般是比较不错的。
对于这种状况,最简单的查询就是 使用覆盖索引,查询某些须要的列。这样的效果是很好的
以下面这个mysql
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)
能够看到时间sql
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,1\G *************************** 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>
那么咱们如今把查询修改以下spa
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一条数据记录。为何会有这样的变化呢。这种叫延时关联
,先经过使用覆盖索引查询返回须要的主键,再根据主键关联原表得到须要的数据,尽量的减小了须要扫描的行数。code
在某些特定的场合,其实有另一种优化方案的。好比要获取最新的几条插入记录。那么在上一次查询的时候咱们能够记录下最后一条记录的主键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
性能都会很好。it