Use the count function in mysql

Source: Internet
Author: User
The count () function uses count (*) to return the number of rows to be retrieved, whether or not it contains NULL values. When SELECT is retrieved from a table, but no other columns are retrieved, and there is no WHERE clause, COUNT (*) is optimized to the fastest return speed. For example, mysqlSELECTCOUNT (*) FROMstudent; this optimization only applies to MyISAM

The count () function uses count (*) to return the number of rows to be retrieved, whether or not it contains NULL values. When SELECT is retrieved from a table, but no other columns are retrieved, and there is no WHERE clause, COUNT (*) is optimized to the fastest return speed. For example, mysql select count (*) FROM student; this optimization only applies to MyISAM

Count () function usage

Count (*) returns the number of rows to be retrieved, whether or not it contains a NULL value.
When SELECT is retrieved from a table, but no other columns are retrieved, and there is no WHERE clause, COUNT (*) is optimized to the fastest return speed. For example:

Mysql> select count (*) FROM student;

This optimization only applies to MyISAM tables because these table types store the exact number of records returned by a function and are very easy to access.
For transaction-type storage engines (InnoDB and BDB), there are many problems with storing a precise number of rows because multiple transactions may occur,
However, each row may have an impact on the number of rows.

COUNT (DISTINCT field)
Returns the number of non-NULL values.
If no matching item is found, COUNT (DISTINCT) returns 0.

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.