table of Contents
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