PhpMySQL and paging efficiency _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PhpMySQL and paging efficiency. The most basic paging method: SELECT... FROM... WHERE... ORDERBY... LIMIT... in the case of small data volumes, such SQL is enough. The only problem that needs to be noted is that the most basic paging method of cable is used:
SELECT... FROM... WHERE... order by... LIMIT...
In the case of small data volumes, such SQL statements are enough. The only problem that needs to be noted is that indexes are used:
For example, if the actual SQL statement is similar to the following statement, it is better to create a composite index on the category_id and id columns:
SELECT * FROM articles WHERE category_id = 123 order by id LIMIT 50, 10

Subquery paging method:

As the amount of data increases, the number of pages will increase. the SQL statements on the last few pages may be similar:

SELECT * FROM articles WHERE category_id = 123 order by id LIMIT 10000, 10

In a word, the more paging goes, the larger the offset of the LIMIT statement, and the slower the speed.

In this case, we can use subqueries to improve paging efficiency, which is roughly as follows:

SELECT * FROM articles WHERE category_id = 123 AND id> = (
SELECT id FROM articles order by id LIMIT 10000, 1
) LIMIT 10

----------------------------------------

In practice, we can use a method similar to the policy mode to process pages. for example, if the number of pages is less than one hundred, we use the most basic paging method. if the number of pages is greater than one hundred, we use the paging method of subquery.

Explain SELECT... FROM... WHERE... ORDERBY... LIMIT... in the case of small data volumes, such SQL is enough, and the only problem that needs to be paid attention is to ensure that the cable is used...

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.