Classic share MySQL's limit query optimization

Source: Internet
Author: User

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 以下的文章主要是对MySQL limit查询优化的具体内容的介绍,我们大家都知道MySQL数据库的优化是相当重要的。其他最为常用也是最为需要优化的就是limit。MySQL的limit给分页带来了极大的方便,但数据量一大的时候,limit的性能就急剧下降。同样是取10条数据    select * from yanxue8_visit limit 10000,10     select * from yanxue8_visit limit 0,10就不是一个数量级别的。 网上也很多关于limit的五条优化准则,都是翻译自MySQL手册,虽然正确但不实用。今天发现一篇文章写了些关于limit优化的,很不错。文中不是直接使用limit,而是首先获取到offset的id然后直接使用limit size来获取数据。根据他的数据,明显要好于直接使用limit。这里我具体使用数据分两种情况进行测试。(测试环境win2033+p4双核 (3GHZ) +4G内存MySQLlimit查询)1、offset比较小的时候    1.select * from yanxue8_visit limit 10,10多次运行,时间保持在0.0004-0.0005之间    Select * From yanxue8_visit Where vid >=(     Select vid From yanxue8_visit Order By vid limit 10,1  ) limit 10多次运行,时间保持在0.0005-0.0006之间,主要是0.0006结论:偏移offset较小的时候,直接使用limit较优。这个显然是子查询的原因。2、offset大的时候    select * from yanxue8_visit limit 10000,10多次运行,时间保持在0.0187左右    Select * From yanxue8_visit Where vid >=(     Select vid From yanxue8_visit Order By vid limit 10000,1  ) limit 10多次运行,时间保持在0.0061左右,只有前者的1/3。可以预计offset越大,后者越优。以后要注意改正自己的limit语句,优化一下MySQL了

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.