The difference between select COUNT (1) and select COUNT (*)

Source: Internet
Author: User

To count how many rows of data a table T has, the usual notation is:

Query A:select Count (*) from T

But you can also use the following statement to check:

Query B:select count (1) from T

The results are usually the same. So where is the difference?

If the T table is a large table, then the query speed will be significantly different. In practice, the T-table has 42 million rows, the use of query B, takes more than 3 minutes, and the use of query A, the time is less than 1 seconds. It is obvious that you must be very cautious on queries on large tables.

So why is query a faster than query B?

Personal analysis that query A does not need to filter, directly with the last row position-the first row position/each row occupies the position. and query B, because each row to be compared to the constant 1, not NULL statistics, so it is slow.

The difference between select COUNT (1) and select COUNT (*)

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.