mysql performance optimization max, count optimization

黄舟
Release: 2017-02-27 11:48:00
Original
2302 people have browsed it


Note: Add explain before executing the SQL statement to view the execution plan of MySQL
Database: the sakila database officially provided by MySQL

Max optimization:

For example: Query the last payment time

explain select max(payment_date) from payment \G;
Copy after login
Copy after login

mysql performance optimization max, count optimization

The query type is simple, no index is used, the number of scanned rows is more than 10,000, and it takes 0.02sec

Optimization method:

Create an index on the payment_date column

create index idx_paydate on payment(payment_date);
Copy after login
Copy after login

Then execute this sql statement and find:

mysql performance optimization max, count optimization

The reason for this execution result is: because the index is arranged sequentially, through the index, you can immediately know what the last one is

Count optimization

For example: find out simultaneously in a SQL statement What are the number of movies in 2006 and 2007?
Wrong way:

select count(release_year = '2006' OR release_year = '2007') from film;
Copy after login
Copy after login

The number of movies in 2006 and 2007 cannot be calculated separately

select count(*) from film where release_year = '2006' and release_year = '2007'
Copy after login
Copy after login

release_year cannot be 2006 and 2007 at the same time, Therefore, it is logically wrong.

Query optimization is as follows:

select count(release_year='2006' or null) as '2006年的电影数量',count(release_year='2007' or null) as '2007年的电影数量' from film;
Copy after login
Copy after login

Description, in sql,count(*)andcount(a certain column), the execution results are sometimes different, becausecount(*)contains null, and if the other one is null, it will not be counted.
Using this feature, the year other than 2006 will be recorded as null. The execution result is as shown in the figure below
mysql performance optimization max, count optimization

Note: You can view the execution of MySQL by adding explain before executing the SQL statement. Plan
Database: MySQL officially provided sakila database

Max optimization:

For example: Query the last payment time

explain select max(payment_date) from payment \G;
Copy after login
Copy after login

mysql performance optimization max, count optimization

Query The type is simple, no index is used, the number of scanned rows is more than 10,000, and it takes 0.02sec

Optimization method:

Create an index on the payment_date column

create index idx_paydate on payment(payment_date);
Copy after login
Copy after login

Then after executing this sql statement, I found:

mysql performance optimization max, count optimization

The reason for this execution result is: because the index is arranged in order, through the index, you can immediately know what the last one is

Count Optimization

For example: Find out the number of movies in 2006 and 2007 at the same time in one SQL statement
Error method:

select count(release_year = '2006' OR release_year = '2007') from film;
Copy after login
Copy after login

Cannot be calculated separately The number of movies in 2006 and 2007

select count(*) from film where release_year = '2006' and release_year = '2007'
Copy after login
Copy after login

release_year cannot be 2006 and 2007 at the same time, so the logic is wrong

The query is optimized as follows:

select count(release_year='2006' or null) as '2006年的电影数量',count(release_year='2007' or null) as '2007年的电影数量' from film;
Copy after login
Copy after login

Explanation, In sql, the execution results ofcount(*)andcount(a certain column)are sometimes different, becausecount(*)contains If it is null, if the other one is null, it will not be counted.
Using this feature, the year that is not 2006 will be recorded as null, and the execution result is as shown in the figure below
mysql performance optimization max, count optimization

The above is the content of max and count optimization of mysql performance optimization, and more For related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!