Reputation: 1508
I'm currently designing a database that has a table events
that will be insert (and update)-heavy (I predict anywhere between 1,000 and 5,000 inserts per minute), but also needs to be sorted by date.
Each event has a read
boolean field, user_id
, and sorts by updated_at
(datetime) DESC
. The select statement would be something like
SELECT * FROM `events` WHERE `read`=0 AND `user_id`='<user id>' ORDER BY `updated_at` DESC
Right now, I'm thinking of using an index on read
, user_id
, and updated_at
to optimize the fetch & sort, but does this mean that the whole index will be updated on a
UPDATE `events` SET `read`='1' WHERE `id`='<event id>'
?
What would be the most performance-efficient (insert, update, select) solution in this situation?
Upvotes: 1
Views: 445
Reputation: 1348
MySQL use only one index per table per query (sometime it use more than one when an index-merge happen) so creating multiples indexes will not speed-up the query.
If I recall correctly it is better to use the most discriminant selector first in an index so a user_id.read.updated_at
will probably be a good solution.
I don't know if updating an index is very costly or not, but since MySQL indexes are by default a tree if read
change the engine has just to operate under an user_id
leaf so maybe the impact is lowered.
Nonetheless you can just have an index on user_id.updated_at
and leave MySQL filter the read
, most of the time ordering data from a table is more costly than filtering.
Upvotes: 2