Mysql – Are these two statements the same performance wise

index-tuningMySQLoptimization

Setup

I have a combination index in table SubmissionStatus as show below. And running a simpl query 2 different ways.

 KEY `Combo_Status_Type_BidID` (`fk_ActivityStatus`,`ActivityType`,`fk_BidIDAssigned`),

Query 1

SELECT *
FROM SubmissionStatus SS
WHERE fk_ActivityStatus IN (5,12)
AND ActivityType = 61

Query 2

SELECT *
FROM SubmissionStatus SS
WHERE ActivityType = 61
AND fk_ActivityStatus IN (5,12)

My understanding

From my understanding the index I have in place here should benefit Query 1 because it has fk_ActivityStatus first, and ActivityType second, just as the Where/And conditions are listed in the statement.

Question

But, Is query 2 also getting to take full advantage of this index? Or does the fact that it has ActivityType listed first prevent it from being able to use the combo Index? (Making it having to do a full table scan, or look for an index that has ActivityType as the first.)

P.S.

The data table are currently to small to be able to get much reliable feedback from the EXPLAIN, and execution plan.

Best Answer

The order of ANDs in a WHERE clause does not matter.
The order of columns in an INDEX does matter.

See here for more discussion of indexes.

For small tests, use the Handler trick:

FLUSH STATUS;
SELECT ...;
SHOW SESSION STATUS LIKE 'Handler%';

When comparing two possible selects and/or indexes, the sizes of the handler numbers give a pretty reliable clue, even for small tables.