我有一个10M行的表product
与color (int), price (float), weight (float), unitprice (int),
等字段等...现在来自Web的用户动态生成查询,以随机条件从此表中查找数据(颜色是必须在这里)和订单由诸如如何索引MySQL中的查找表
select * from product where color=1 and price >5 and price <220 and .... order by unitprice limit 75, 25;
select count(*) from product where color=3 and weight <500 and price <30 ... ;
如何在MySQL索引的表(InnoDB的或NDB)用约10个可能的过滤字段(范围,排序...)这样?
编辑:在我的理解MySQL最有可能将只选择一个索引的查询,只有组合索引的左手部分将工作。显然索引所有可能的组合是不是一个可行的选择,如(color, price, weight, create_date, unitprice, ....)
,(color, weight, price, create_date, unitprice, ....)
,(color, unitprice, weight, ....)
....不是所有的条件都一定存在于所有查询。
你会怎么做索引这个表?
感谢您的提示。我在这个表中有10个这样的字段。你建议只有两个关键部分的复合指数?或全部10?是的,颜色将成为所有指标的第一部分。 – 2010-10-03 08:44:13
@QWJ我的建议是每个索引的两个关键部分,您的表的10个索引。这样MySQL就可以通过查看单个索引(color + x)来满足查询最重要的部分。这里是[一些更多的阅读主题](http://stackoverflow.com/questions/1277865/mysql-indexes-how-many-are-enough)。 – Tomalak 2010-10-03 08:52:22
你可以,也许,收集有关所作查询统计(通过写查询日志,例如),并删除那些很少使用,如果大量的索引开始证明麻烦的索引。 – 2010-10-03 09:18:38