MySQL under COUNT (*) and Count (column) distinguish performance comparison

Source: Internet
Author: User

COUNT (*) calculates the number of rows, including null

Count (column) calculates the number of rows for a particular column's value, and does not contain null values.

Count () is also used, and the use of Count (1) is the same as the result of Count (*).


about their performance issues

1. In any case select COUNT (*) from TableName is the optimal choice;
2. Minimize the query of select COUNT (*) from tablename WHERE COL = ' value ';
3. Eliminate the appearance of select COUNT (COL) from tablename WHERE COL2 = ' value '.

If the table does not have a primary key, then count (1) is faster than count (*).
If you have a primary key, count (primary key, union primary key) is faster than count (*).
If the table has only one field, COUNT (*) is the fastest.

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.