Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大家来聊聊工作中遇到的sql优化案例吧^o^ #545

Open
caizhigang97 opened this issue Aug 5, 2017 · 3 comments
Open

大家来聊聊工作中遇到的sql优化案例吧^o^ #545

caizhigang97 opened this issue Aug 5, 2017 · 3 comments

Comments

@caizhigang97
Copy link
Contributor

对一个大数据量的表使用limit做分页。越往后分页,LIMIT语句的偏移量就会越大,速度也会明显变慢。如下:
SELECT * FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 20

此时,我们可以通过子查询的方式来提高分页效率:
SELECT * FROM big_table WHERE id >= (SELECT id FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 1) LIMIT 10
原因: limit10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里。例如 LIMIT 500000 , 30 扫描了50万行,肯定会慢。所以采用上面的方式可以有效解决这个问题。
注:这里的is是已经索引过的。

欢迎各位兄弟姐妹积极留言呀。

@gordonCN
Copy link
Contributor

gordonCN commented Aug 7, 2017

is? or id?

@gordonCN
Copy link
Contributor

gordonCN commented Aug 7, 2017

另:子查询中仍旧使用limit 1000, 性能会有所提升吗?

@Locker1995
Copy link

有人教过我,单条数据查询 select * from table a where a.xx=xxx 后面加上limit 1
查询速度会快很多

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants