mysql optimization, the reason why the query does not go through the index is summarized

Recently, the company asked me to do SQL optimization work (MySql), and I sent some questions with explain. Common problems such as OR, IN, >=, or nesting cause the index to fail and the query performance to decrease. I will not state it here. Search a lot of articles on the Internet. I just wrote something that I encountered in my personal work, and it is not easy to search on the Internet, but I do not guarantee that all the scenes will be tried out, and I will update it in the future.


1. The order by and limit are used in combination. If the where field and the order by field are both indexes, then the limit index will use the index where the order by field is located. No limit will use where conditionindex. In such a situation, consider using a subquery to separate order by and limit. This happens mainly when you use multiple indexes, so you need to pay attention. It probably doesn't perform the walk indexing you want. (I think mysql will calculate the index automatically)

2. DATE_FORMAT() formats the time, and comparing the formatted time may cause the index to fail.

3. The index of order by in the subquery will be invalid, and at the same time, the index of the where condition in the subquery may not be available.

4. The use of character sets leads to no indexing. Sometimes you will find that there may be a day difference between using a SQL condition value (two different ID numbers I encountered before, one query 80s, one less than 1s)

5. Like statement
6. The column type is string type, and the query is not enclosed in single quotation marks.
7. Use expressions
in the where query statement
. Using the or keyword, the myisam table can use the index, but innodb cannot; (replace OR with UNION, the index can be used) 10. Full table scan is faster than index scan (small amount of data)


Let’s talk about these first. If the execution plan is not ideal, I suggest adding two startup parameters when starting the database, which will make it clearer (the number of executions and execution time of each table) 

--log-slow-queries (query log)

--log-queries-not-using-indexes    (queries not using index logs)

The final optimization method is testing, because different optimization theories of the business cannot always bring high efficiency, use explain or desc to view, and then really improve a query or table.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885385&siteId=291194637