The count () function in MySQL uses the

Source: Internet
Author: User

Sometimes it is considered that count (*) is slower than count (1) or count (column name) and is actually handled in a sub-case.

Like what:

---initialization statements

Create a table and insert the data:

Create table test2 (id BIGINT PRIMARY key, name varchar)engine=innodb;

insert Into Test2 (id,name) values (1, NULL);

insert into test2 (id,name) values ( Span class= "Hljs-number" >2, ' name1 ');

insert Into Test2 (id,name) values (3, " Name2 ')

Execute the following SELECT statement:

select count (*) from test2;  //Result: 3

select count (ID) from test2;//Result: 3

select count (name) from test2;//Result: 2

select count (name) from test2 where name is null; Result: 0

count (1) refers to the first field of the table rather than the count of 1. If the first field does not have an index, his efficiency is very low;

), in the above count (column name)

in query mode, if the specified column is limited to non-null, MySQL then converts the above expression to count () for querying. So if you want

to query the size of the data, it is recommended that count (*) be used in MySQL to execute, meaning and fast.

The count () function in MySQL uses

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.