2013-01-31 156 views
1

我分区表被描述为这样的划分:Mysql的,用聚合函数

CREATE TABLE `event_data` (id int(11) NOT NULL AUTO_INCREMENT, 
    ... 
    Timestamp int(10) unsigned NOT NULL, 
    ... 
    ... 
    CurrentDate date NOT NULL, 
    KEY `id_index` (`id`), 
    KEY `ix_filter` (`Action`,`Location`), 
    KEY `ix_time` (`Timestamp`) 
)ENGINE=MyISAM AUTO_INCREMENT=1176568 DEFAULT CHARSET=latin1 
/*!50500 PARTITION BY RANGE COLUMNS(CurrentDate) 
(PARTITION p20130106 VALUES LESS THAN ('2013-01-06') ENGINE = MyISAM, 
PARTITION p20130113 VALUES LESS THAN ('2013-01-13') ENGINE = MyISAM, 
PARTITION p20130120 VALUES LESS THAN ('2013-01-20') ENGINE = MyISAM) */ 

我试图执行以下查询:

explain partitions select min(Timestamp) from event_data where CurrentDate < "2013-01-06";

+----+-------------+------------+------------+------+---------------+------+---------+------+--------+-------------+ 
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+-------------+ 
| 1 | SIMPLE  | event_data | p20130106 | ALL | NULL   | NULL | NULL | NULL | 512983 | Using where | 
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+-------------+ 

而且

explain partitions select min(Timestamp) from event_data;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+------------------------------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra      | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+------------------------------+ 
| 1 | SIMPLE  | NULL | NULL  | NULL | NULL   | NULL | NULL | NULL | NULL | Select tables optimized away | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+------------------------------+ 

看来,如果没有指定分区查询更快(我知道的最小总是在第一个分区)

select min(Timestamp) from event_data where CurrentDate < "2013-01-06";

+----------------+ 
| min(Timestamp) | 
+----------------+ 
|  1321747200 | 
+----------------+ 
1 row in set (0.16 sec) 

而且

select min(Timestamp) from event_data ;

+----------------+ 
| min(Timestamp) | 
+----------------+ 
|  1321747200 | 
+----------------+ 
1 row in set (0.00 sec) 

指定分区的查询不应该更快,因为它只需要在单个分区中查看最小值,而不是在所有分区上查找最小值?

看起来,当指示分区时没有使用时间戳索引,但为什么?我有每个分区文件的MYI文件,我确信索引是为每个这样的文件构建的...

我也知道,索引用于不具有聚合函数(基准测试)的不同选择查询。

UPDATE

我发现这个错误报告http://bugs.mysql.com/bug.php?id=66187,这是有关我的问题。

+0

@ypercube,是它出现在我的表格定义中(附在问题中) – Michael

+0

我没有玩过分区,但答案很可能是这样。在第二个查询中,索引被使用,最小值可以被发现非常快。在第一个查询中,它不使用索引,而是扫描整个(第一个)分区。 –

+0

@ypercube,我也这么认为,但为什么我们有索引每个分区?我以为我们有索引树每个分区文件或至少为什么我有每个分区的MYI文件 – Michael

回答