Mysql,使用聚合函数进行分区

时间:2013-01-31 14:56:28

标签: mysql performance aggregate-functions partitioning

我的分区表格如下:

 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)

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

在指示分区时似乎没有使用Timestamp索引,但为什么???我有每个分区文件的MYI文件,我确信为每个这样的文件构建了索引......

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

更新

我找到了此错误报告http://bugs.mysql.com/bug.php?id=66187,这与我的问题相关。

1 个答案:

答案 0 :(得分:0)

我发现了一个严重的错误报告http://bugs.mysql.com/bug.php?id=66187,它描述了同样的问题。我已对此Bug添加了评论