About innodb count Optimization in mysql

Source: Internet
Author: User

Generally, secondary indexes are used for count:
For example, if id is pk aid, it is secondary index.

Use
Copy codeThe Code is as follows:
Select count (*) from table where id> = 0;
Or
Select count (*) from table;

The results are the same. pk indexes are used by default, and full table scanning is required. Although the first type of performance may be higher, there is no obvious difference.

However, if secondary index is used
Copy codeThe Code is as follows:
Select count (*) from table where aid> = 0;

It will be much faster.

Why is secondary index scan faster than primary key scan? This requires understanding the differences between the clustered index and secondary index of innodb.

The clustered index of innodb stores the primary key and row data together, while the secondary index stores the data separately, and then a pointer points to the primary key.
Therefore, it is faster to use secondary index to scan the count (*) Statistical table records.
Primary key is mainly used to scan indexes and return results records.

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.