Mysql Laterowlookups (delayed row lookup) _ MySQL

Source: Internet
Author: User
Mysql's Laterowlookups (delayed row lookup) bitsCN.com

Mysql Late row lookups (delayed row lookup)

SQL code

Create table '201430122handler '(

'Id' int (11) not null AUTO_INCREMENT,

'Uid' int (11) not null,

'Content' varchar (50) not null,

Primary key ('id '),

KEY '201430122handler _ idx_uid '('uid ')

) ENGINE = InnoDB

There are 60 million data records in it. now we simulate paging by uid.

To view the content on page 7, use

SQL code

Select SQL _NO_CACHE * from 20130122 handler

Order by uid LIMIT 120,20

Finding 20 pieces of data is almost an instant task.

If the user is abnormal, click Page 102 and use

SQL code

Select SQL _NO_CACHE * from 20130122 handler

Order by uid LIMIT 2020,20

Search for 20 pieces of data and find that the performance is quite poor.

It seems that mysql reads 2040 secondary records from the 20130122handler_idx_uid index in this case,

Then, the system executes 2040 primary key queries and returns 20 results. Therefore, 2020 primary key queries are wasted.

This method can be used to reduce useless row lookup.

SQL code

Select SQL _NO_CACHE m. * from (

Select uid from 20130122 handler

Order by uid LIMIT 2020,20) t, 20130122 handler m

Where t. uid = m. uid

This is because 20130122handler_idx_uid is a secondary index, so row lookup is required.

Used

SQL code

Select SQL _NO_CACHE * from 20130122 handler

Order by id LIMIT 120,20

Select SQL _NO_CACHE * from 20130122 handler

Order by id LIMIT 2040,20

The difference is not obvious.

BitsCN.com

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.