MYSQL 可以在 WHERE ... IN 查询中使用索引吗?
目前,我们的应用程序中有一个如下查询:
SELECT * FROM combos WHERE text = '[text]'
我们在 combos
上放置一个 category_id
列,查询将更改为:
SELECT * FROM combos WHERE category_id in ([c1],[c2],[...]) AND text = '[text]'
目前,有一个索引关于组合(文本)
。我们正在考虑删除它并在 combos(category_id, text)
上添加一个。 MYSQL 能够像使用旧索引一样使用新索引吗?如果答案是“这取决于”,那么它取决于什么?我们应该保留旧的以防万一它不能吗?
We currently have a query in our app like this:
SELECT * FROM combos WHERE text = '[text]'
We're putting a category_id
column on combos
, and the query will change to this:
SELECT * FROM combos WHERE category_id in ([c1],[c2],[...]) AND text = '[text]'
At the moment, there's an index on combos(text)
. We're thinking of removing it and putting one on combos(category_id, text)
. Will MYSQL be able to use the new index like it did the old one? If the answer is 'it depends', what does it depend on? Should we keep the old one in case it can't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时......只要没有范围条件:
http://dev.mysql.com/doc/refman/5.5/en/index-merge-optimization.html
你可能更适合使用
(text,category_id)
--取决于您的数据分布!sometimes....as long as there is no range condition:
http://dev.mysql.com/doc/refman/5.5/en/index-merge-optimization.html
you might be better of with
(text, category_id)
-- depending on your data distribution!