MySQL查询中LIMIT的大offset会导致性能低下

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014717036/article/details/84799896

前言

在业务系统中难免少不了分页的需求。想到分页的时候,大家肯定会想到使用SQL中的LIMIT来实现。但是,如果不正确的使用LIMIT会导致性能问题(SQL执行得很慢、有可能会拖垮服务器),也会被领导批的;所以,我们来看看如何正确地使用LIMIT。

LIMIT OFFSET, ROW_COUNT 实现分页

存在性能问题的方式

SELECT * FROM myTable ORDER BY `id` LIMIT 1000000, 30 

写出这样SQL语句的人肯定心里是这样想的:MySQL数据库会直接定位到符合条件的第1000000位,然后再取30条数据。

然而,实际上MySQL不是这样工作的。

LIMIT 1000000, 30 的意思是:扫描满足条件的1000030行,扔掉前面的1000000行,然后返回最后的30行。

较好的方式

SELECT  t.*
FROM    (
        SELECT  id
        FROM    myTable
        ORDER BY
                id
        LIMIT 1000000, 30
        ) q
JOIN    myTable t
ON      t.id = q.id

大概的原理是:

  • 子查询只用到了索引列,没有取实际的数据,所以不涉及到磁盘IO,所以即使是比较大的 offset,查询速度也不会太差。

对具体的原理分析感兴趣的朋友可以看看这篇文章:MySQL ORDER BY / LIMIT performance: late row lookups

后记

未完待续。

参考资料

展开阅读全文

mysql order by + limit 性能问题如何解决?

12-05

主要做设计前端。。PHP是业余看看。。rn没用过mysql语句 今天硬啃了一天了 发现加上 ORDER BY 就没走索引。。rn百度了一天 还是没搞懂原理 和解决方案 求大神科普。。。rnrnorder by + limit 分页 越往后性能越低rnrnrn[code=sql]SELECT * FROM yi_user_joke WHERE status = 2 ORDER BY audit_time desc LIMIT 499950,10;rn//结果rn10 rows in set (2.67 sec)rnrnSELECT * FROM yi_user_joke WHERE status = 2 ORDER BY audit_time desc LIMIT 499950,10;rnexplain 结果rnrnexplain SELECT * FROM yi_user_joke WHERE status = 2 ORDER BY audit_time desc LIMIT 499950,10;rn+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+rn| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |rn+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+rn| 1 | SIMPLE | yi_user_joke | ALL | NULL | NULL | NULL | NULL | 499999 | Using where; Using filesort |rn+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+rn1 row in set (0.00 sec)[/code]rnrn表结构rnrn[code=sql]show columns from yi_user_jokern -> ;rn+-----------------+---------------+------+-----+---------+----------------+rn| Field | Type | Null | Key | Default | Extra |rn+-----------------+---------------+------+-----+---------+----------------+rn| id | int(11) | NO | PRI | NULL | auto_increment |rn| title | varchar(1000) | NO | | NULL | |rn| image | varchar(200) | YES | | NULL | |rn| content | text | NO | | NULL | |rn| is_package | tinyint(1) | NO | | 0 | |rn| package_fee | int(11) | NO | | 0 | |rn| package_user_id | int(11) | NO | | 0 | |rn| created_time | int(11) | NO | MUL | 0 | |rn| audit_time | int(11) | NO | MUL | 0 | |rn| type | tinyint(1) | NO | MUL | 0 | |rn| status | tinyint(1) | NO | | 0 | |rn| user_id | int(11) | NO | MUL | 0 | |rn| audit_num | int(11) | NO | | 0 | |rn| good_num | int(11) | NO | MUL | 0 | |rn| bad_num | int(11) | NO | MUL | 0 | |rn| review_num | int(11) | NO | | 0 | |rn| share_num | int(11) | NO | | 0 | |rn| award_num | int(11) | NO | | 0 | |rn| tags_id | varchar(200) | YES | MUL | NULL | |rn| god_reply | tinyint(1) | NO | MUL | 0 | |rn| reason | varchar(200) | YES | | NULL | |rn| commend | tinyint(1) | NO | | 0 | |rn+-----------------+---------------+------+-----+---------+----------------+rn22 rows in set (0.01 sec)[/code]rn rn索引rnrn[code=sql]show index from yi_user_jokern -> ;rn+--------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+rn| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |rn+--------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+rn| yi_user_joke | 0 | PRIMARY | 1 | id | A | 499999 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | type | 1 | type | A | 1 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | created_time | 1 | created_time | A | 499999 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | user_id | 1 | user_id | A | 1 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | god_reply | 1 | god_reply | A | 1 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | good_num | 1 | good_num | A | 1 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | bad_num | 1 | bad_num | A | 1 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | tags_id | 1 | tags_id | A | 1 | NULL | NULL | YES | BTREE | | |rn| yi_user_joke | 1 | audit_time | 1 | audit_time | A | 499999 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | order_time | 1 | audit_time | A | 499999 | NULL | NULL | | BTREE | | |rn| yi_user_joke | 1 | order_time | 2 | status | A | 499999 | NULL | NULL | | BTREE | | |rn+--------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+rn11 rows in set (0.00 sec)[/code] 论坛

没有更多推荐了,返回首页