简体   繁体   English

具有int索引的MySQL简单查询速度很慢

[英]MySQL simple query with int index is slow

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. 需要大约80ms,考虑到我正在迭代~10k行,这是太多的时间。 My stop_times table has about 3.5M rows, and there's an index on stop_id . 我的stop_times表有大约3.5M行,而stop_id上有一个索引。 Any lead on what's going on here and what might speed it up? 关于这里发生了什么以及可能加速它的任何领先?

EXPLAIN [query] output: 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

Try adding trip_id to the index on stop_id . 尝试添加trip_id于索引stop_id This way your query will have a covering index and don't do a lookup. 这样,您的查询将具有覆盖索引并且不进行查找。 Also, check execution plan ( EXPLAIN [your query] ). 另外,检查执行计划( EXPLAIN [your query] )。 If the index is not selective enough, it won't be used. 如果索引选择不够充分,则不会使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM