这个简单的查询:
SELECT trip_id from stop_times WHERE stop_id = 345
大约需要 80 毫秒,考虑到我要遍历 ~10k 行,这个时间太多了。我的 stop_times
表有大约 350 万行,stop_id
上有一个索引。关于这里发生的事情以及可能加快速度的任何线索?
EXPLAIN [query]
输出:
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
最佳答案
尝试将 trip_id
添加到 stop_id
的索引中。这样您的查询将具有覆盖索引并且不进行查找。此外,检查执行计划(EXPLAIN [your query]
)。如果索引不够选择性,则不会使用。
关于带有 int 索引的 MySQL 简单查询很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546946/