[MySQL] About count (*) and count (COL)

Source: Internet
Author: User

Although count (*) and count (COL) may have different performance under different circumstances.

However, in general, count (*) performs an index scan on the primary key, which counts the total number of records that meet the requirements in the table. Count (COL) does not necessarily scan the primary key, it counts the number of records of all the compliant Col columns in the table.

For the following two queries:

Select count (*) from tbl_name;

Select count (COL) from tbl_name;

If col is the primary key, count (*) and count (COL) should have equivalent performance; otherwise, count (*) is faster than count (COL.


The following is a summary of others' optimizations:

MySQL's count optimization summary: 1. select count (*) from tablename is the optimal choice under any circumstances; 2. minimize the number of queries such as select count (*) from tablename where Col = 'value'; 3. prevent the appearance of select count (COL) from tablename.

Reference connection: http://www.ccvita.com/347.html

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.