What should I do if my website data is too slow to be paged?

Source: Internet
Author: User
What should I do if my website data is too slow to be paged? When using the MySQL database, data query at a large offset is very slow. How can I optimize SQL statements to accelerate paging query? Tool raw material MySQL database Apache (WEB server software) method step analysis traditional paging SQL statement select * fromtablelimit $ offset, 10, when $ offset is very

What should I do if my website data is too slow to be paged? When using the MySQL database, data query at a large offset is very slow. How can I optimize SQL statements to accelerate paging query? Tool/raw material MySQL database Apache (WEB server software) method/step analysis traditional paging SQL statement select * from table limit $ offset, 10, when $ offset is very

What should I do if my website data is too slow to be paged? When using the MySQL database, data query at a large offset is very slow. How can I optimize SQL statements to accelerate paging query?

Tools/raw materials MySQL database Apache (WEB server software) method/step analysis traditional paging SQL statement select * from table limit $ offset, 10, when $ offset is very large, such as 980000, at this time, the MySQL database needs to query 980010 data records and then discard the first 980000 data records, which is certainly slow. Consider this SQL statement: select 'id' from table limit $ offset, 10 (id is the primary key), because the id field is the primary key, mysql database will use the index, therefore, even if you want to query 980010 pieces of data, the speed is quite fast. Using indexes can greatly improve the speed of mysql database queries. Consider the following SQL statement:

Select * from table where id> = (select id from table limit $ offset, 1) limit 10 explain the preceding SQL statement:

First, the id number after $ offset is obtained through the query with the primary key index. Because the index is used, this subquery is very fast, and then 10 pieces of data with id> = $ offset are queried through the condition.

This paging method is N times faster than the traditional paging method in the case of large data volumes. Note: MYSQL Databases of earlier versions do not support subqueries.

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.