On the question of how to optimize MySQL, how to judge whether the SQL statement has been optimized, we need a standard, Explain is our standard! !
What is Explain? What is Explain for?
Ready to work:
1. Create an article table
insert data
Table structure: article information: author, table of contents, number of readings, number of comments, title number, content (simple table, for demonstration~)
experiment procedure:
1. Request to query the information of all articles (common query)
2. Use explain (just add explain before the sql statement, it's super simple~~, but the result is not simple, I can't understand it at all, okay, the following lesson is just the beginning~)
3.id column, there are only two cases of id here, the same id and the different id
The id is the same: the execution order is from top to bottom;
id is different: the execution order is from big to small
What is the execution order: which table to start executing from, the table column is our table, the current query uses only one table article, so we can't understand it very well, remember it here, and then explain it with an example.
4.select_type列:
5.table column is our table, not explained.
6. The partition column does not need to be read
7. Type column, system is the best, all is the worst, this is very complicated, I will give you a link later
8. The possible_keys column refers to which indexes mysql thinks we should use
9. The key column, which index do we actually use, the best, and the worst null
10. The key_len column represents the number of bytes used in the index. The shorter the length, the better without losing precision.
11.ref column:
12.Rows column: The fewer the query records, the faster the number of rows mysql traverses.
13.filtered column: not important
14.Extra: Very important, the following data need to be paid attention to
Example: The following example (id is the primary key, there is no other index), looking at the ID, mysql will first execute 3, 2, 1 (from large to small), in the SQL statement, first execute the third select, t3, and then Execute the second select, t1, and finally execute the first select, t2. Because the t3 query has no index, the type value is all at the bottom, the other two use the id primary key, the const efficiency is OK, ref represents what value matches the index, and rows represents the three queries mysql have searched for a row
Three lines in total.