While testing my simple mail MySQL db, I found a strange behaviour and would be glad to know why.
My 'mail' table has two primary key :
idx : INT(Auto-Increment)
uid : VARCHAR(50)
with some other dummy columns like createdTime, info, etc.
Then, I've filled my table with 200,000 dummy datas, and tested with a simple search query :
SELECT * FROM mail WHERE uid='RANDOMGENERATEDUID';
It took about 0.235sec to execute this query, and from workbench's tabular explain I found out that this query performs full-table scan and uses no keys.
I was curious that what will happen if I force to use 'idx' column for SELECT query, so, I've tested out another query, which will have the same result :
SELECT * FROM mail
WHERE idx IN (SELECT idx FROM foodwagondb.mail WHERE uid='RANDOMGENERATEDUID');
But what surprised me was that this query executes way more faster, 0.078sec to execute!
What's going on behind my eyes here? I would be happy to know why this happens!
idx
which can be retrieved directly from the index alone, without ever accessing the table itself. MySql may decide that when it also needs to select other fields (*
), it becomes a little less interesting to use the index, and more interesting to "only" do table access. This decision can depend on several factors, which I am not aware of. But the above could be one element.