MySQL counts total rows of records when paging and returns a fixed number of records using limit

Source: Internet
Author: User

  The requirements are simple: Suppose there is a user table, there are actually 10,000 data in the table, but I do not know how many, I want to fetch 20 data from the database every time, then how to complete it?

Programme one:

First perform a

Select COUNT (*) as total from  user;  

    The above SQL statement will detect the total number of records. Another point, perhaps here is not just unconditional check, if it is conditional check, then you can use the temporary table

Select COUNT (*) from user where id>10; or select count (*) from (select Id,name from user) as temp;

     After the total number of records has been calculated, you can use limit to take only a subset of the data:

SELECT * from user limit start,offset;

    In fact, the above approach is completely no problem, but I have a question, the example here is only from a table to operate, but also very simple operation, if you want to do a very complex connection, grouping, deduplication, if the calculation of the data of a few tables is very large, then it means that the above operation is very redundant:

    one is to perform a complete operation and then only the total number of records is obtained;

    one is to perform the complete operation , and then only the offset bar data from the start is obtained.

In fact, the first step of the two operations is coincident, then I would like to be able to do only one operation (such as the connection between several tables, grouping, deduplication, etc.), and then can get the total number of records, and can only take start after the offset bar data

    

  Scenario Two:

MySQL counts total rows of records when paging and returns a fixed number of records using limit

Related Article

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.