我測試了在MySQL 8.0.0-DMR:
ALTER TABLE sentbox1
PARTITION BY RANGE(to_days(CreatedDateTime))
SUBPARTITION BY HASH(userid)
SUBPARTITIONS 4 (
PARTITION p20161130 VALUES LESS THAN (to_days('2016-11-30')),
PARTITION p20161201 VALUES LESS THAN (to_days('2016-12-01'))
);
注意,子分區僅適用於HASH或KEY作爲分區方法。
查看https://dev.mysql.com/doc/refman/5.7/en/partitioning-subpartitions.html瞭解更多信息。從裏克
回覆評論:
@RickJames,我想你知道,完全取決於查詢一個運行是否符合分區和子分區對齊。如果您的查詢邏輯導致查詢跨越子分區,則不會帶來任何好處。
我確認分區修剪需要注意子分區鍵上的條件。
當我把一個條件僅在主要分區鍵,因此它必須掃描該分區的所有子分區:
mysql> explain partitions select * from sentbox1
where createddatetime = from_days(736663) \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sentbox1
partitions: p20161201_p20161201sp0,
p20161201_p20161201sp1,
p20161201_p20161201sp2,
p20161201_p20161201sp3
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
filtered: 100.00
Extra: Using where
當我另外指定的userid
(子分區鍵)的條件,我們可以看到要檢查的子部分的減少子集:
mysql> explain partitions select * from sentbox1
where createddatetime = from_days(736663) and userid=3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sentbox1
partitions: p20161201_p20161201sp3
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
filtered: 100.00
Extra: Using where
您測試了它,但您是否確定對HASH或SUBPARTITION有任何性能優勢? (我把這當成是對你的挑戰,比爾。) –