0
I implemented an INDEX in the "Name" column, which is varchar Binary (latin1_binary).
This query takes about 1.5 seconds to execute:
SELECT Nome FROM contas WHERE IP='255.255.255.255' AND Nome != BINARY'uhdde23221' LIMIT 10;
However, this takes 43 seconds:
SELECT Nome FROM contas WHERE IP='255.255.255.255' AND Nome != 'uhdde23221' LIMIT 10;
This, even without conversion Inary, takes 0.001s:
SELECT * FROM contas WHERE Nome='uhdde23221';
Why does this happen? I will always have to use "BINARY"?
Version: 10.4.14-Mariadb
I believe that there is a difference between the two, but only by testing, the difference is that the
BINARYdifferentiates capitalized and lowercase (while the without, I believe that does not differentiate). Also, if this is a frequent query, try creating an index (IP, Name), I don’t know if it will help much. It seems to me that IP is more important than Name, the name is only in !=. And... if theIP, Nomeare unique (there is no data with same IP and Name), you can simply doWHERE IP =and then in its application ignores whether theNomeis the same, sometimes it’s faster than usingIP =along withNome !=to take out 1 line.– Inkeliz
In its edition, as
Nome =, you are using the=, while in others equality is based onIP =(and theNomeare using asNome !=).– Inkeliz
I believe the right way to analyze it is by using the
explainandanalyzeand seeing the query execution plan.– Danizavtz
The time of 1.5s is normal since I didn’t actually do an index on
IP. The problem is the 43 seconds when I match the columnNomewith any other in the clauseWHEREwithout converting the static part intoBINARY. This did not happen before, only after I did an index inNome. What should speed up the query ended up making it absurdly slow for this detail, and I don’t know why. OBS: The columnNomeis already binary by default.– Rodrigo