"Database" optimized sorting && efficient paging

Source: Internet
Author: User

Example:

Select <cols> from profiles where sex = ' M ' ORDER by rating limit 10;

The order By,limit is used at the same time and is slow if no index is available. And the sey choice is very low, you can add some special index to do the sorting. For example, create an (sex,rating) index.


Even if there is an index, the query can be very slow if the user needs to turn the page and page to the back of the comparison.

The following query is then paged through the combination of the order by and limit offsets to the very back:

Mysql>select <cols> from profiles where sex = ' M ' ORDER by rating limit 1000000,10;


Regardless of whether you create an index, this query is a serious problem. Because as offsets increase, MySQL takes a lot of time to scan for data that needs to be discarded.


Deserialization, pre-evaluation, and caching may be the only policies that solve such problems. A better approach is to limit the number of pages a user has, and it will not actually have much impact on the user experience, as users are rarely concerned with the 10,000th page of search results.


Another good strategy for optimizing such indexes is to use deferred correlation to return the required primary key by using the Overwrite index query, and then to get the required rows based on the primary key associated with the original table. This can reduce the number of rows that MySQL scans for those dropped. The following query shows how to efficiently use (sex,rating) indexes for sorting and paging.

Mysql>select <cols> from profiles inner JOIN (

Select <primary key cols >from Profiles

->where x.sex = ' M ' ORDER by rating limit 100000,10

->as x using (<primary key cols>);


Reference: "High performance MySQL"


"Database" optimized sorting && efficient paging

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.