我分區表被描述爲這樣的劃分: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,這是有關我的問題。
@ypercube,是它出現在我的表格定義中(附在問題中) – Michael
我沒有玩過分區,但答案很可能是這樣。在第二個查詢中,索引被使用,最小值可以被發現非常快。在第一個查詢中,它不使用索引,而是掃描整個(第一個)分區。 –
@ypercube,我也這麼認爲,但爲什麼我們有索引每個分區?我以爲我們有索引樹每個分區文件或至少爲什麼我有每個分區的MYI文件 – Michael