MySQL optimizes Max () function by index

table of Contents

demand

Optimize SQL by adding indexes


 

demand

 

We want to query the maximum value of a field in the table. You can choose the way to directly check

select max(payment_date) from payment;

 

We use the execution plan to check the performance of this SQL

explain select max(payment_date) from payment\G

You can see that this SQL statement retrieved more than 16,000 rows of data, and we can optimize it by indexing.

 

Optimize SQL by adding indexes

 

Set in which table to add an index to which attribute

create index inx_paydate on payment(payment_date);

 View the execution plan after adding the index

explain select max(payment_date) from payment\G

The index does not need to scan the table, the number of rows retrieved after adding is Null improves the efficiency of the query

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105599458