I\'m kinda ashamed of asking this since I\'ve been working with MySQL for years, but oh well.
I have a table with two fields, a
and b
. I will b
It's very improbable that mere existence of an index slow down a SELECT
query: it just won't be used.
In theory the optimizer can incorrectly choose more long index on (a, b)
rather than one on (a)
to serve the query which searches only for a
.
In practice, I've never seen it: MySQL
usually does the opposite mistake, taking a shorter index when a longer one exists.
Update:
In your case, either of the following configurations will suffice for all queries:
(a, b); (b)
or
(b, a); (a)
MySQL
can also use two separate indexes with index_intersect
, so creating these indexes
(a); (b)
will also speed up the query with a = 1 AND b = 1
, though to a lesser extent than any of the solutions above.
You may also want to read this article in my blog:
Update 2:
Seems I finally understood your question :)
ALTER TABLE ... ADD INDEX (a); ALTER TABLE ... ADD INDEX (b);
Excellent for a = 1
and b = 1
, reasonably good for a = 1 AND b = 1
ALTER TABLE ... ADD INDEX (a, b);
Excellent for a = 1 AND b = 1
, almost excellent for a = 1
, poor for b = 1
ALTER TABLE ... ADD INDEX (a); ALTER TABLE ... ADD INDEX (b); ALTER TABLE ... ADD INDEX (a, b);
Excellent for all three queries.