mysql skip count_MongoDB和MySQL中的large skip问题、count问题

large skip

在为数据分页时,一般要skip多少记录并limit多少记录,例如在MySQL中:

SELECT * FROM large_table ORDER BY `id` LIMIT 10000, 30

这个过程是很慢的,因为数据库需要从第一个记录开始扫描到第10000个记录,这个比较耗时。

在http://idning.github.io/point-large-skip.html对上面的sql代码总结了两个优化方法:

方法1:

SELECT t.*

FROM (

SELECT id

FROM large_table

ORDER BY

id

LIMIT 10000, 30

) q

JOIN large_table t

ON t.id = q.id

方法2:

SELECT * FROM large WHERE id > 10000 ORDER BY id LIMIT 30

方法2有个问题,如果数据库中没有id为12的记录,那么方法2得到的结果和预期是不一样的

同样,在mongodb中也有类似的问题,一个比较好的解决方法和上面的MySQL的方法2基本相同。

count

另外,count()查询也有较慢的问题,优化方法如下:

方法1: Try COUNT(ID) instead of COUNT(*), where ID is an indexed column that has no NULLs in it. That may run faster.

方法2: If you‘re storing the binary data of the files in the longblob, your table will be massive, which will slow things down.

方法3:MySQL使用MyISAM索引,其内置了一个计数器。

参考: http://idning.github.io/point-large-skip.html

http://stackoverflow.com/questions/7228169/slow-pagination-over-tons-of-records-in-mongo

http://stackoverflow.com/questions/10764187/mongo-db-skip-takes-too-long-time

http://stackoverflow.com/questions/15402141/mysql-query-very-slow-count-on-indexed-column

http://xue.uplook.cn/database/mysqlsjk/2835.html

原文:http://my.oschina.net/letiantian/blog/324076

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值