Index tuning in MySQL SQL tuning

(1) Let's talk about the steps of tuning first

1. Use tools to discover slow SQL. Tools include SkyWalking, VisualVM, JavaMelody, Alibaba Druid, etc.
2. Add explain before analyzing slow SQL and common SQL
3. Use index tuning to see the total execution time of SQL. If it can be controlled to 100-200ms (reference value), it is a good SQL. Of course, this must be combined with the actual use of the system.

(2) Data structure used by MySQL storage

1) Indexes include B-Tree index, hash index, full-text index, and spatial index
1. Binary tree->balanced binary tree->B-Tree->B+Tree

B-Tree keyword with data
B+Tree leaf node is a linked list, if there is data in the linked list of InnoDB engine, there is also a pointer, and the pointer points to the data

(3) Tuning example

1. Use MySQL's own slow query log here
1) First, check whether MySQL has enabled the slow query log (it is enabled in the figure). The slow_query_log_file in the figure is the location of the log output file.

Insert picture description here

2) Since there is no large data table on hand, I can only demonstrate the operation process in general, and then add it later.

Insert picture description here
For tuning column, if type is all, generally adjust it. The smaller the key_len, the better, the smaller the rows, the better, and the smaller the filtered, the better.

3) Add index

####### 1) Click on the table to be indexed, right-click and select Modify Table
Insert picture description here
to add an index here. The added index must follow the leftmost prefix. If the index is added in front of a range query such as a.id> 123, Then the index added at the back is useless.
Do coverage index
####### 2) Add index position
GROUP BY, ON, WHERE, LIKE, ORDER BY
plus ORDER BY index. Putting the order by field in the first place is equivalent to sorting.
It is recommended that the indexed field is not null. If it can be null, the database will store one more data to determine whether it is null.
####### 3) Use straight_join when associating tables to set the associated table as the main table

4) Indexes are not created as many as possible. Database maintenance index also has a certain overhead. Pay attention to redundant indexes, duplicate indexes, and discard indexes

Find redundant indexes...Tools
pt-query-digest, pt-duplicate-key-checker

My strength is limited, if there are any shortcomings, I hope you all comment and give pointers, thank you

Guess you like

Origin blog.csdn.net/A_234_789/article/details/107528894