分页查询
分页查询的问题点主要集中在
- 如何快速定位起始点
- 减少无用数据缓存
mysql为分页查询提供了很方便的关键字limit,但这个关键字在数据量较大时,却很低效。
“limit m,n”关键字的意思是,从第m行开始,扫描满足条件的n个偏移行。若需从第1行开始,则不需要指定m值。
示例
表aaaaa中共有2375690数据。
优化前的SQL
SQL执行结果:
SELECT DISTINCT(device_id) uid FROM aaaaa WHERE status = 0 LIMIT 88000,1000; 1000 rows in set (0.48 sec)
SQL执行计划:
MariaDB [star]> explain SELECT sql_no_cache DISTINCT(device_id) uid FROM aaaaa WHERE status = 0 LIMIT 88000,1000; +------+-------------+---------------+------+---------------+------+---------+------+---------+------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+---------------+------+---------------+------+---------+------+---------+------------------------------+ | 1 | SIMPLE | aaaaa | ALL | NULL | NULL | NULL | NULL | 2375690 | Using where; Using temporary | +------+-------------+---------------+------+---------------+------+---------+------+---------+------------------------------+
优化方式
迅速定位起始ID,利用主键索引,加快扫描速度。可以看到,derived中,SQL使用到了覆盖索引进行扫描,虽然还是全表扫,因为只扫描id列,大大降低了扫描的IO耗费,快速定位到了id。
MariaDB [star]> explain SELECT sql_no_cache DISTINCT(device_id) uid FROM aaaaa join (select id from aaaaa limit 88000,1) k on star_device_5.id>=k.id where status=0 limit 1000; +------+-------------+---------------+-------+---------------+-------------+---------+------+---------+------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+---------------+-------+---------------+-------------+---------+------+---------+------------------------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 88001 | Using temporary | | 1 | PRIMARY | star_device_5 | ALL | PRIMARY | NULL | NULL | NULL | 2377112 | Range checked for each record (index map: 0x1) | | 2 | DERIVED | star_device_5 | index | NULL | idx_star_id | 8 | NULL | 2377112 | Using index | +------+-------------+---------------+-------+---------------+-------------+---------+------+---------+------------------------------------------------+
执行结果:
SELECT sql_no_cache DISTINCT(device_id) uid FROM star_device_5 join (select id from star_device_5 limit 880000,1) k on star_device_5.id>=k.id where status=0 limit 1000; 1000 rows in set (0.19 sec)
随着m的增大和n的增大,两种写法的SQL执行时间会有本质差别。我做了测试,当m值增加到880000时,优化前的SQL需要2分钟,优化后的SQL还是0.1s左右。
时间: 2024-10-11 19:38:49