Julius S. - 3 months ago 12

MySQL Question

I have these queries:

1st query:

`SELECT (..) FROM db WHERE A = const AND B > const AND C >= const ORDER BY B DESC LIMIT const`

2nd query (different db):

`SELECT (...) FROM db' WHERE A' = const ORDER BY X' DESC LIMIT const`

Question about 1st query:

Is it sufficient to have a multiple row index (A, B, C) or do I need an additional single row index (B) (or a different one) because of the

`ORDER BY`

Question about 2nd query: Do I need a multiple row index (A', X') or two single row indices (A'), (X') to make us of them in this query?

Answer

It is an important thing to know that MySQL will use at most one index (for searching, filtering and ordering) per table and subquery (so basically per row in `explain`

), so you can use only one index here.

For your first query, an index `(A,B)`

will allow MySQL to do a range scan and use the order. If you use `(A,B,C)`

, the column `C`

cannot be used in the range condition (because `B`

is already a range), but MySQL will save the time to read the actual tabledata to get the value for `C`

to check the last condition. So `(A,B,C)`

is in general the fastest choice here.

"In general", because you can of course have a data distribution where another index would be best: If you e.g. have only one or two rows that match `C >= const`

and 10M+ rows with `A = const`

, using an index on just `C`

would be fastest. And if `C`

is a very big column (e.g. `varchar(700)`

), it could blow up the index and slow it down. But to estimate such exceptions would require deeper knowledge of your data.

For your second query, `(A', X')`

will be the best choice. If you have the two indexes `(A')`

, `(X')`

, MySQL will in most cases (unless `A'`

is unique, but then you wouldn't need an `order by`

anyway) use the index on `X'`

and hope it will find matching rows for `A'`

soon. This will sometimes be unexpectedly and painfully slow if you only have some rows that match `A' = const`

(because it has to jump back and forth in the table (that is ordered by the primary key) in the order of `X'`

to find rows that match the condition for `A'`

).

You might get the same problem for your first query if you have the indexes `(A)`

and `(B)`

(but not `(A,B)`

or `(A,B,C)`

) there: MySQL will probably use `(B)`

instead of `(A)`

(but check the `explain`

to make sure). Even if you just add one index now, this can e.g. happen when you add the index `(B)`

to optimize a different query next week and forgot about this query, so I'd suggest to stick with (at least) `(A,B)`