mysql 分區
阿新 • • 發佈:2018-04-12
custom char null tween 產生 有用 customer 例如 create
PARTITION p201601 VALUES LESS THAN (TO_DAYS(‘2016-01-01‘)),
PARTITION p201709 VALUES LESS THAN (TO_DAYS(‘2017-09-30‘)),
PARTITION p201710 VALUES LESS THAN (TO_DAYS(‘2017-10-31‘)),
PARTITION p201711 VALUES LESS THAN (TO_DAYS(‘2017-11-30‘)),
PARTITION p201712 VALUES LESS THAN (TO_DAYS(‘2017-12-31‘)),
PARTITION p201801 VALUES LESS THAN (TO_DAYS(‘2018-01-31‘)),
PARTITION p201802 VALUES LESS THAN (TO_DAYS(‘2018-02-28‘)),
PARTITION p201803 VALUES LESS THAN (TO_DAYS(‘2018-03-31‘)),
PARTITION p201804 VALUES LESS THAN (TO_DAYS(‘2018-04-30‘)),
PARTITION p201805 VALUES LESS THAN (TO_DAYS(‘2018-05-31‘)),
PARTITION p201806 VALUES LESS THAN (TO_DAYS(‘2018-06-30‘)),
PARTITION p201807 VALUES LESS THAN (TO_DAYS(‘2018-07-31‘)),
PARTITION p201808 VALUES LESS THAN (TO_DAYS(‘2018-08-31‘)),
PARTITION p201809 VALUES LESS THAN (TO_DAYS(‘2018-09-30‘)),
PARTITION p201810 VALUES LESS THAN (TO_DAYS(‘2018-10-31‘)),
PARTITION p201811 VALUES LESS THAN (TO_DAYS(‘2018-11-30‘)),
PARTITION p201812 VALUES LESS THAN (TO_DAYS(‘2018-12-31‘))
);
本次的分區並沒有用到實際的項目中,因為現有業務數據確實有些小
本次是使用按月進行分區
本次分區發現自己檢索兩個月間隔的時候會出現去掃描第一個分區
比方說掃描 1月1號到1月30號的數據
會去掃描1月的分區和第一分區
解決的方法是添加一個閑置的第一分區,所以即使掃描到了第一分區也不會產生什麽消耗,之所以出現數據添加第一分區的情況很大的原因是因為輸入數據為null,無法處理就往第一分區裏頭放了
CREATE TABLE `wd_customer_partition` (
`customer_id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_time` datetime NOT NULL DEFAULT ‘1970-01-01 00:00:00‘ COMMENT ‘添加時間‘,
PRIMARY KEY (`customer_id`,`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘客戶表‘ PARTITION BY RANGE(TO_DAYS(create_time))
(
PARTITION p_null VALUES LESS THAN (0),#這個分區只會存放失敗和異常的數據
PARTITION p201601 VALUES LESS THAN (TO_DAYS(‘2016-01-01‘)),
PARTITION p201709 VALUES LESS THAN (TO_DAYS(‘2017-09-30‘)),
PARTITION p201710 VALUES LESS THAN (TO_DAYS(‘2017-10-31‘)),
PARTITION p201711 VALUES LESS THAN (TO_DAYS(‘2017-11-30‘)),
PARTITION p201712 VALUES LESS THAN (TO_DAYS(‘2017-12-31‘)),
PARTITION p201801 VALUES LESS THAN (TO_DAYS(‘2018-01-31‘)),
PARTITION p201802 VALUES LESS THAN (TO_DAYS(‘2018-02-28‘)),
PARTITION p201803 VALUES LESS THAN (TO_DAYS(‘2018-03-31‘)),
PARTITION p201804 VALUES LESS THAN (TO_DAYS(‘2018-04-30‘)),
PARTITION p201805 VALUES LESS THAN (TO_DAYS(‘2018-05-31‘)),
PARTITION p201806 VALUES LESS THAN (TO_DAYS(‘2018-06-30‘)),
PARTITION p201807 VALUES LESS THAN (TO_DAYS(‘2018-07-31‘)),
PARTITION p201808 VALUES LESS THAN (TO_DAYS(‘2018-08-31‘)),
PARTITION p201809 VALUES LESS THAN (TO_DAYS(‘2018-09-30‘)),
PARTITION p201810 VALUES LESS THAN (TO_DAYS(‘2018-10-31‘)),
PARTITION p201811 VALUES LESS THAN (TO_DAYS(‘2018-11-30‘)),
PARTITION p201812 VALUES LESS THAN (TO_DAYS(‘2018-12-31‘))
);
在使用分區函數的字段,需要使用datetime類型,而且需要將該字段設置為主鍵
未來查詢的時候需要使用該類型,指定數據範圍來鎖定縮小目標區域
例如使用between
mysql 分區