1. 程式人生 > >mysql按時間分割槽插入資料

mysql按時間分割槽插入資料

//給表新增按時間分割槽功能(新增之前必須給分割槽的欄位加上主鍵)
ALTER TABLE `records` 
PARTITION BY RANGE((YEAR(`visit_time`)*100+MONTH(`visit_time`))*100+DAY(`visit_time`))  
(  
    PARTITION p20161114 VALUES LESS THAN (20161115), 
    PARTITION p20161115 VALUES LESS THAN (20161116)  
)

//給表新增分割槽
ALTER TABLE `records` ADD PARTITION (PARTITION p20161116 VALUES LESS THAN (20161117));
ALTER TABLE `records` ADD PARTITION (PARTITION p20161117 VALUES LESS THAN (20161118));

//檢視分割槽表的資料總數
SELECT PARTITION_NAME,TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS

//刪除表裡面的分割槽,不能刪除所有的分割槽
ALTER TABLE `records` DROP PARTITION p20161118;

//查看錶某分割槽的資料
SELECT * FROM `records` PARTITION (p20161117);

搞了一天,終於搞好了!