[Performance optimization] Reasons for MySQL index failure and how to optimize SQL

Interface performance optimization The first thing you think of may be: 优化索引.

That's right, the cost of optimizing indexes is minimal.

By checking online logs or monitoring reports, you can find out that a certain SQL statement used by a certain interface takes a long time.

At this point you may have the following questions:

  1. Has the sql statement been indexed?

  2. Did the added index take effect?

  3. Did mysql choose the wrong index?

1. Index

1.1 Not indexed

whereThe key field of the condition in the sql statement , or order bythe sort field behind, forgot to add an index. This problem is very common in projects.

At the beginning of the project, due to the small amount of data in the table, there is little difference in SQL query performance with or without indexes.

Later, with the development of the business, the amount of data in the table increased, so it had to be indexed.

You can pass the command:

show index from `order`;

You can view the index status of a table separately.

You can also pass the command:

show create table `order`;

View the table creation statement of the entire table, which will also display the index status.

Indexes can be added by ALTER TABLEcommand:

ALTER TABLE `order` ADD INDEX idx_name (name);

Indexes can also CREATE INDEXbe added via the command:

CREATE INDEX idx_name ON `order` (name);

However, there is one thing to note here: it is impossible to modify the index through commands.

At present, if you want to modify the index in mysql, you can only delete the index first, and then add a new one.

To delete an index, you can use DROP INDEXthe command:

ALTER TABLE `order` DROP INDEX idx_name;

DROP INDEXYou can also use the command:

DROP INDEX idx_name ON `order`;

1.2 The index does not take effect

Through the above command, we have been able to confirm that the index exists, but has it taken effect? At this point, you may have such a question in your heart.

So, how to check whether the index has taken effect?

Answer: You can use explainthe command to view the execution plan of mysql, which will display the usage of the index.

For example:

explain select * from `order` where code='002';

result:

These columns can be used to judge the index usage. The meaning of the columns included in the execution plan is shown in the figure below: If you want to know more about the detailed usage of explain, you can read my other article "explain | This is a peerless tool for index optimization Sword, do you really know how to use it? "

To be honest, the sql statement does not use the index, except that the index is not built, the biggest possibility is that the index is invalid.

Here are some common reasons for index failure:

If it is not the above reasons, you need to further investigate other reasons.

1.3 Choosing the wrong index

In addition, have you ever encountered such a situation: it is obviously the same sql, only the input parameters are different. Sometimes the index a is taken, and sometimes the index b is taken?

Yes, sometimes mysql will choose the wrong index.

force indexIt can be used to force the query sql to go to a certain index when necessary .

As for why mysql chooses the wrong index, there will be a special article to introduce it later, so let’s leave some suspense here.

2. SQL optimization

If the index is optimized, it will have no effect.

Next, try to optimize the sql statement, because its transformation cost is much smaller than that of java code.

Here are 15 tips for SQL optimization:

Guess you like

Origin blog.csdn.net/gongzi_9/article/details/125900744