We use composite indexes in many places and they work great. Our platform has a relatively complex "report builder" that dynamically writes queries based on what the users select. We choose indexes based on common queries and often our composite indexes line up well with what users want.
For instance (simplifying a ton), a user's report will turn into the query:
SELECT
colA,
colB,
SUM(col1),
SUM(col2)
FROM
someTable
WHERE
colA = "foo"
AND colB = "bar"
AND colC = "baz"
GROUP BY 1, 2;
And the table will look something like:
CREATE TABLE someTable (
id PRIMARY KEY,
col1 data_type,
col2 data_type,
colA data_type,
colB data_type,
colC data_type,
INDEX someIndex (colA,colB,colC)
);
I know MySQL won't be able to use that index on a query like:
SELECT
colA,
colB,
SUM(col1),
SUM(col2)
FROM
someTable
WHERE
colB = "foo"
GROUP BY 1, 2;
My question is - will MySQL use the composite index with a query like:
SELECT
colA,
colB,
SUM(col1),
SUM(col2)
FROM
someTable
WHERE
colA != ""
AND colB = "foo"
GROUP BY 1, 2;
I know, of course, that we could just add an index on colB
, but that's not a good option in this case. I've simplified here to make the question clear. Realistically, we have like 100 columns in this table and can't add indexes on each column and possible column variation, so I'm trying to figure out if we can make some use of what we already have to make things a bit faster for those edge-case queries.
Hope I worded that well. Thanks in advance :)