This simple query:
SELECT trip_id from stop_times WHERE stop_id = 345
Takes about 80ms, which is way too much time considering I’m iterating through ~10k rows. My stop_times table has about 3.5M rows, and there’s an index on stop_id. Any lead on what’s going on here and what might speed it up?
EXPLAIN [query] output:
id: 1
select_type: SIMPLE
possible_keys: index_stop_times_on_stop_id
key: index_stop_times_on_stop_id
key_len: 5
ref: const
rows: 474
Extra: Using where
Try adding
trip_idto the index onstop_id. This way your query will have a covering index and don’t do a lookup. Also, check execution plan (EXPLAIN [your query]). If the index is not selective enough, it won’t be used.