The situations where indexes need to be used in MySQL include: when using a link that does not start with a wildcard, when extracting rows from a table when performing a join, and when finding the max or min value of a specified index, etc.
When does MySQL use indexes
1. Use >, >=, =, <, <= for a key code , IF NULL and BETWEEN
SELECT * FROM table_name WHERE key_part1=1 and key_part2 > 5; SELECT * FROM table_name WHERE key_part1 IS NULL;
2. When using LIKE that does not start with a wildcard
SELECT * FROM table_name WHERE key_part1 LIKE 'jani%'
3. When extracting rows from another table when performing a join
SELECT * from t1,t2 where t1.col=t2.key_part
4. Find the MAX() or MIN() value of the specified index
SELECT MIN(key_part2),MAX(key_part2) FROM table_name where key_part1=10
5. Use ORDER BY or GROUP BY as the prefix of a key code
SELECT * FROM foo ORDER BY key_part1,key_part2,key_part3
6. Use it in all queries The column is part of the key code
SELECT key_part3 FROM table_name WHERE key_part1=1
Related learning recommendations:mysql tutorial(video)
The above is the detailed content of When does mysql use indexes?. For more information, please follow other related articles on the PHP Chinese website!