Mysql million-level paging optimization and mysql Paging

Source: Internet
Author: User

Mysql million-level paging optimization and mysql Paging

Normal Paging

Data paging is very common in webpages. pages are generally limit start and offset, and start is calculated based on the page number.

select * from user limit 1,20

When the page size is several hundred thousand, the page efficiency will be relatively low. MySQL needs to calculate the page size from the beginning to the end, which greatly affects the efficiency.

SELECT * from user limit 100001,20; //time 0.151sexplain SELECT * from user limit 100001,20;

We can use the explain statement to analyze the statements without any indexes. The number of rows executed by MySQL is 16 W +, so we can use indexes to implement paging.

   Optimize Paging

Use primary key index to optimize data Paging

select * from user where id>(select id from user where id>=100000 limit 1) limit 20; //time 0.003s

Using the explain statement, the number of lines scanned by MySQL is more than, and the time is greatly shortened.

 explain select * from user where id>(select id from user where id>=100000 limit 1) limit 20; 

Summary

When the data volume is large, we try to optimize the statement by using indexes. If the id is not a primary key index, the query efficiency is lower than the first one. We can use the explain statement to analyze the statement execution sequence and performance.

Added: Performance Optimization of millions of paging queries in mysql

Prerequisites:

1. Unique index of the table

2. Million-level data

SQL statement:

select     c.*     FROM  (  SELECT   a.logid  FROM   tableA a  where 1 = 1     <#if phone?exists&& phone!="">       AND a.phone like "%":phone"%"      </#if>   ORDER BY    a.create_time DESC   limit :startIndex,:maxCount  ) b,tableA c  where 1 = 1 AND b.logid = c.logid

Where:

1: startIndex: indicates the start position of the data query.

2: maxCount: displays the number of data entries per page.

3: a. create_time DESC: sort in descending order. You need to create an index at create_time.

4: Put limiit inside, instead of outside the query, which improves the efficiency a lot.

5: logid: unique index

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.