My question is about indexes and primary keys.
Let’s suppose we have a table like this:
demo_table
| id | int(11) | NO | PRI | |
| subject | varchar(255) | YES | | NULL |
| body | text | YES | | NULL |
| to | varchar(255) | YES | MUL | NULL |
| from | varchar(255) | YES | MUL | NULL |
| media | varchar(255) | YES | | NULL |
I have a query that looks for the last row of a specific ‘to’ / ‘media’ couple:
select * from demo_table where to="" and media="" order by id desc limit 0,1;
and I would like to create an index for my table. Should I create the index like this:
CREATE INDEX to_media on demo_table(media,to);
or like this:
CREATE INDEX to_media on demo_table(id,media,to);
seeing that the ID is sorted?
1 Answer