我有一个大约 8M 行和 35 列的表(总体大小为 1G)。这用于涉及多级group by
条件的报告。这是一个这样的查询。
explain select min(a), max(a), avg(a), sum(b) from test where (c=2 or c=20 or c=18 or c=21 or c=12) and d>='2013-01-01' and d <= '2013-12-01' group by c,e;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: range
possible_keys: PRIMARY
key: PRIMARY
key_len: 7
ref: NULL
rows: 154911
Extra: Using where; Using temporary; Using filesort
1 row in set (0.00 sec)
这需要大约 1.8 秒来执行。主键打开c,d
并用于评估 where 子句。count(*)
具有相同 where 条件且没有 group by的查询返回大约 150000 行。所以在实际查询中扫描的行也是可以的。但是,没有一个索引被用于对结果进行分组。我试过把索引放在c
and上c,e
。
我的问题是,一旦应用了条件并且结果集在 tmp 表中,是否可以使用另一个索引进行排序/分组。
我已经分配了足够的内存给tmp_table_size
. 因此,tmp 表肯定不会磁盘。该表正在使用MyISAM
.