1.hash分區 PS::個人覺得HASH分區很好很強大,簡單確分布極其均勻 創建實例: CREATE TABLE HASH_EMP ( tid int, tname char(255) ) PARTITION BY HASH (tid) PARTITIONS 8; 將hash_emp進行的tid進行hash分區,並分為8個區 查詢分區的數據分布情況: select partition_name,partition_expression,partition_description,table_rows from information_schema.partitions where table_schema = schema() and table_name = 'hash_emp'; +----------------+----------------------+-----------------------+------------+ | partition_name | partition_expression | partition_description | table_rows | +----------------+----------------------+-----------------------+------------+ | p0 | tid | NULL | 0 | | p1 | tid | NULL | 0 | | p2 | tid | NULL | 0 | | p3 | tid | NULL | 0 | | p4 | tid | NULL | 0 | | p5 | tid | NULL | 0 | | p6 | tid | NULL | 0 | | p7 | tid | NULL | 0 | +----------------+----------------------+-----------------------+------------+ 創建1個event,用來不間斷寫入數據,測試分布情況: create event hash_emp_event on scheduler every 1 second do insert into hash_emp values (NULL,now()); set GLOBAL event_scheduler = 1; //開啟調度器 再次查看分區數據分布情況: +----------------+----------------------+-----------------------+------------+ | partition_name | partition_expression | partition_description | table_rows | +----------------+----------------------+-----------------------+------------+ | p0 | tid | NULL | 41 | | p1 | tid | NULL | 42 | | p2 | tid | NULL | 42 | | p3 | tid | NULL | 42 | | p4 | tid | NULL | 42 | | p5 | tid | NULL | 42 | | p6 | tid | NULL | 42 | | p7 | tid | NULL | 42 | +----------------+----------------------+-----------------------+------------+ 可以看出來,hash分布極其均勻:; 2.Key分區 PS::所謂key分區則是指mysql默認使用表的主鍵或唯一建進行分區管理 創建實例: CREATE TABLE KEY_EMP ( tid int, tname char(255) ) PARTITION BY KEY (tid) PARTITIONS 8; PS::因為跟hash差不多,就不進行過多測試了!!! 3.子分區 PS::顧名思義就是在分區上再建分區 PS::支持子分區的分區模式有range || list ,它們2者都可以支持hash或list的子分區 創建實例:: CREATE TABLE ZI_EMP ( tid int, tname char(255) ) PARTITION BY RANGE (tid) SUBPARTITION BY HASH (tid) SUBPARTITIONS 2 ( PARTITION p0 values less than (1990), PARTITION p1 values less than (2028), PARTITION p2 values less than (MAXVALUE) ); 將zi_emp分成了3個range分區,每個分區在分為2個子分區,如是,有了下面的分區結構: +----------------+----------------------+-----------------------+------------+ | partition_name | partition_expression | partition_description | table_rows | +----------------+----------------------+-----------------------+------------+ | p0 | tid | 1990 | 0 | | p0 | tid | 1990 | 0 | | p1 | tid | 2028 | 0 | | p1 | tid | 2028 | 0 | | p2 | tid | MAXVALUE | 0 | | p2 | tid | MAXVALUE | 0 | +----------------+----------------------+-----------------------+------------+ 也就是說,如果tid小於1990,那麼數據會被hash分配到p0這2個子分區中