mysql WHERE IN with group by和order by

时间:2014-01-30 11:52:13

标签: mysql search group-by sql-order-by

我有一个50行的Innodb表main

CREATE table main(
 id INT AUTO_INCREMENT NOT NULL,
 main_id INT NOT NULL,
 feature1 INT NOT NULL,
 feature2 INT NOT NULL,
 feature3 INT NOT NULL,
 time_repetition TIMESTAMP NOT NULL,
 rating SMALLINT NOT NULL,
 PRIMARY KEY (id),
 INDEX search(range_col1,range_col2,range_col3)
 ) engine=innodb;

要点1: feature*字段是一些特征。因此,在搜索过程中,用户可以检查他是否需要表中的行,这些行满足多个值,因此我需要在IN中使用WHEREfeature1 IN (val1,val2,..) and feature2 IN (val1,val2,..) and feature3 IN (val1,val2,...)

POINT2:根据时间的重复对表进行非规范化。因此,我需要在GROUP_CONCAT(time_repetition)中选择GROUP BY main_id

要点3:首先要获得最佳价值评级。所以,我需要ORDER BY rating DESC。我还考虑每天重新计算一次评级,并根据顺序递减评级将数据插入表中。在这种情况下,我需要ORDER BY id。不幸的是,它没有解决问题。

所以,整个查询是: SELECT main_id, feature1, feature2, feature3, GROUP_CONCAT(time_repetition), rating FROM main WHERE feature1 IN (val1,val2,..) and feature2 IN (val1,val2,..) and feature3 IN (val1,val2,...) GROUP BY main_id ORDER BY rating LIMIT 50

问题是查询使用临时和文件排序平均为5K行。例如,下面是解释的结果:

explain(select SQL_NO_CACHE * from main where feature1 in (4,5,6,7)
and feature2 IN (2,3,4,5) and feature3 IN (1,2,3,4,5) group by main_id
order by id limit 10);

+----+-------------+--------+-------+---------------+----------+---------+------+------+----------------------------------------------+
| id | select_type | table  | type  | possible_keys | key      | key_len | ref  | rows | Extra                                        |
+----+-------------+--------+-------+---------------+----------+---------+------+------+----------------------------------------------+
|  1 | SIMPLE      | main | range | search      | search | 12      | NULL | 7333 | Using where; Using temporary; Using filesort |
+----+-------------+--------+-------+---------------+----------+---------+------+------+----------------------------------------------+

如果我在索引中添加main_id则无效。你能推荐什么?

0 个答案:

没有答案