Table event (id, source_id, start_time), 140k records
Index (source_id asc, start_time desc)
My query:
SELECT * FROM event where source_id in (4, 5, 3, 10)
order by start_time desc limit 100;
Run time ~ 2 seconds.
id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | event | null | ALL | idx_source_start_time | null | null | null | 125504 | 100 | Using where; Using filesort |
Currently, my table only has event of source_id 3, no event of source 4, 5, 10. With index (camera_id, start_time), I think that MySQL will dive into BTree and know source 4, 5, 10 have no matched records. But this query runs very slow. How can I optimize this query ?
where source_id = X
, 100 rows per value (Index should work), then UNION ALL and one more ORDER BY + LIMIT.