开发者

Can MYSQL use an index for a WHERE ... IN query?

开发者 https://www.devze.com 2023-02-05 18:12 出处:网络
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:

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 co开发者_开发问答mbos 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?


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号