Mysql optimization

SELECT COUNT (*) AS num FROM test WHERE ( a = 111AND cid = 21432 OR s_status < > "yes" OR t_status < > yes") AND test . delete_time IS NULL LIMIT 1

explain the statement type is displayed as ALL. How to optimize it?

Mar.21,2021

personal understanding, I hope it will be helpful to you

  • uses the condition of or splitting. If the column in the condition before or has an index and there is no index in the following column, then none of the indexes involved will be used.
    reference: merge queries using UNION
  • negative queries (not,not in,not like, < >,! =,! >,! <) do not use the index
    reference: if it is a definite and limited collection, you can use IN
  • try to avoid judging the null value of the field in the where clause, otherwise it will cause the engine to give up using the index and perform a full table scan
    reference: assign the null value to the default value for the field type (String is "", and so on)

upstairs correct solution, first of all, to minimize some of the operations said upstairs. Otherwise, it will always be all. :). Look at your sql statement, you can know your requirements, should be the number of statistics. The conditions are a = 111and cid = 21432 | s_status! = yes | t_status! = yes, test.delete is null. The end result is: well, it's impossible, it can't be optimized, (at least I am). First of all, if you want to use an index, disable null and replace it with 0 or something else. Second, yes this do not use different, use like for retrieval, you can cooperate with the if method.

Menu