Mysql count (*), DISTINCT Efficiency Research

Source: Internet
Author: User
Tags mysql query mysql tutorial

Suddenly, it is found that the different processing of count (*) In the mysql tutorial will lead to different results.

For example
Select count (*) from tablename
Even for mysql with tens of millions of data, results can be returned very quickly.
For
Select count (*) from tablename where .....
Mysql query time began to rise

Check the manual carefully and find that when there is no where statement for the count operation on the entire mysql table
A myisam Table stores the total number of rows. When a where statement is added, mysql needs to search the entire table.
To obtain the count value.

I suddenly remembered that many of the emerging php Tutorial programs were not very familiar with count processing.
Record

By the way, mysql's distinct keywords are of a lot of unexpected use.
1. It can be used when count does not repeat records
For example, select count (distinct id) from tablename;
Calculate the number of records with different IDs in the talbebname table.

2. You can use
For example, select distinct id from tablename;
Returns the specific values of different IDs in the talbebname table.

3. In case 2 above, there will be ambiguity when you need to return results with more than two columns in the mysql table
For example, select distinct id, type from tablename;
In fact, the returned result is a result of different IDs and types, that is, distinct serves two fields at the same time. It must be the same id and tyoe to be excluded. It is different from the expected result.

4. At this time, you can use the group_concat function for troubleshooting. However, this mysql function is supported only after mysql4.1.

5. Another solution is to use
Select id, type, count (distinct id) from tablename
Although such a returned result contains a column of useless count data (You may need this useless data)
The returned result is that only the results with different IDS can be used in concert with the above four types. It depends on what data you need.

 

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.