我有一张拥有1000万行数据的表格,名为product
,其中包含了color(整数)、price(浮点数)、weight(浮点数)和unitprice(整数)
等字段。现在Web端的用户会动态生成查询条件,以便从该表中查找数据,并按照随机条件进行排序(这里必须要有颜色作为查询条件)。
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中对一个具有约10个可能的过滤字段(包括范围、排序等)的表格(InnoDB或NDB)进行索引?
编辑:据我所知,MySQL很可能只会为一个查询选择一个索引,并且仅左侧部分的组合索引才有效。显然,对所有可能的组合进行索引不是可行的选择,例如(颜色、价格、重量、创建日期、单价,...)
,(颜色、重量、价格、创建日期、单价,...)
,(颜色、单价、重量,...)
等。并非所有条件都必须存在于所有查询中。
你会如何对这个表格进行索引呢?