mysql - How can I make this SQL non sargable? -


I have used an online tool to analyze one of my SQL Curves (the query made me age Have taken the age).

My query takes one word (in this example the word 'dog') and tries to find it in the 'qa' table, when it joins the line data from the login table, where Login.pid === qa.u

  Select login.pid, login.name, qa.id, qa.end, qa.react, qa.win, qa.stock, qa. Num, qa Qa.k like ratio, qa.u, qa.t, qa.k, qa.swipes, qa.d login, qa where login.pid = qa.u and ('% dog.%' Or qa.k = '.dog.') ORDER BY qa.d DESC LIMIT 0,15   

I understand what the tool is telling me:

  leading There is a prominent wildcard character in the logic argument with wildcards, such as "% foo" is not perturbed with this argument and can not use the pointer if one exists.   

But I do not know how to use the index inside '()', without changing any harmful or results ... Can someone please explain that I How can I use an index in terms of an inquiry?

I think if this is non-surgical then the result will be faster?

First, learn how to use the modern join syntax:

  select login.pid, login.name, qa.id, qa.end, Qa.react, qa.win, qa.stock, qa.num, qa.ratio, qa.u, qa. Log on to qa at t, qa.k, qa.swipes, qa.d login pid = qa. O WHERE (qa.k '% dog.%' Or qa.k = '.dog.') By ​​order of Qad DESC LIMIT 0,15; Actually "Sergable" means that you can use an index on any particular expression (this is not an English word, it's short.) ). Expression can not be used on  qa.k .  

This does not matter depending on the query plan for the query. For example, if the engine decides to scan the login table and then values ​​in qa , the index will not help. It helps in going through other ways.

The bad news is that you can not make this expression sargable in MySQL The good news is that you can use a full text index to do what you want and possibly more. A small note you can read about them is that the default settings ignore small words, up to three characters. If you really want to search for "dog" then you have to change the default setting.

By the way, you can use an index on the following expression: qa.k : <: / P>

  WHERE (like qa.k 'dog ('%' Or 'qa.k =' .dog. '')   

(I'm not sure MySQL will actually use index because it can sometimes or ).

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -