P
P
Puma Thailand2012-01-08 22:39:39
MySQL
Puma Thailand, 2012-01-08 22:39:39

What indexes to make for such a query in mysql?

explain select * from class_actions where type like 'newad' or type like 'renewad' and object_id=335046;
There is already an index on the type field and double on the type + object_id
explain fields shows that the indexes do not work.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Melkij, 2012-01-08
@melkij

where type in ('newad','renewad') and object_id=335046
Here at such request the index on object_id & type will be used.

E
edogs, 2012-01-08
@edogs

It is rather obvious that it is necessary to hang up an index on object_id.
If your type is text (otherwise why like), then it definitely makes sense to change it to enum, then the composite (type + object_id) can start working (only the query will need to be rewritten from like to in ('newad','renewad')

G
galkinrost, 2012-01-09
@galkinrost

The sequence of columns in the index is exactly (1) type, (2) object_id?
For the sake of testing, I put two indexes on type (VARCHAR), object_id (int):
type + object_id and object_id + type - explain shows that the first one works.

4
4dmonster, 2012-01-09
@4dmonster

like 'newad' or type like 'renewad'
sorry, but there are no wildcards here, if it's a full match, wouldn't it be better to use =?

P
PQR, 2012-01-09
@PQR

When using OR, indexes do not work!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question