參考:
有乙個表tb_3a_huandan_detail,每天有300w左右的資料。查詢太慢了,網上了解了一下,可以做表分割槽。由於資料較大,所以決定做定時任務每天執行存過自動進行分割槽。
1、在進行自動增加分割槽前一定得先對錶手動分幾個區
alter table tb_3a_huandan_detail partition by range (to_days(servicestarttime))
( partition p20160523 values less than (to_days('2016-05-23')),
partition p20160524 values less than (to_days('2016-05-24')),
partition p20160525 values less than (to_days('2016-05-25')),
partition p20160526 values less than (to_days('2016-05-26')),
partition p20160527 values less than (to_days('2016-05-27'))
)
2、分割槽存過如下:
delimiter $$
use `nres`$$
drop procedure if exists `create_partition_3ahuadan`$$
create definer=`nres`@`%` procedure `create_partition_3ahuadan`()
begin
/* 事務回滾,其實放這裡沒什麼作用,alter table是隱式提交,回滾不了的。*/
declare exit handler for sqlexception rollback;
start transaction;
/* 到系統表查出這個表的最大分割槽,得到最大分割槽的日期。在建立分割槽的時候,名稱就以日期格式存放,方便後面維護 */
select replace(partition_name,'p','') into @p12_name from information_schema.partitions
where table_name='tb_3a_huandan_detail' order by partition_ordinal_position desc limit 1;
set @max_date= date(date_add(@p12_name+0, interval 1 day))+0;
/* 修改表,在最大分割槽的後面增加乙個分割槽,時間範圍加1天 */
set @s1=concat('alter table tb_3a_huandan_detail add partition (partition p',@max_date,' values less than (to_days (''',date(@max_date),''')))');
/* 輸出檢視增加分割槽語句*/
select @s1;
prepare stmt2 from @s1;
execute stmt2;
deallocate prepare stmt2;
/* 取出最小的分割槽的名稱,並刪除掉 。
注意:刪除分割槽會同時刪除分區內的資料,慎重 */
/*select partition_name into @p0_name from information_schema.partitions
where table_name='tb_3a_huandan_detail' order by partition_ordinal_position limit 1;
set @s=concat('alter table tb_3a_huandan_detail drop partition ',@p0_name);
prepare stmt1 from @s;
execute stmt1;
deallocate prepare stmt1; */
/* 提交 */
commit ;
end$$
delimiter ;
3、增加定時事件
delimiter ||
create event partition_3ahuadan_event
on schedule
every 1 day starts '2016-05-27 23:59:59'
dobegin
call nres.`create_partition_3ahuadan`;
end ||
delimiter ;
MYSQL建立或增加分割槽
create table ts rta visit from id int 11 not null auto increment,stat date varchar 8 not null comment 統計日期 日期 yyyymmdd stat minute varchar 4 default n...
增加分割槽空間
增加分割槽空間 root home 1 備份home cp r home homebak 2 解除安裝home umount home 程序占有 fuser m v i k home kill 9pid root bogon do you really want to remove active l...
LVM增加分割槽大小
有的時候,在使用系統一段時間,因為種種原因不得不擴大分割槽,這裡是指擴大已經存在的分割槽,而且不影響原來分割槽的資料 因為在安裝系統時,多留了乙個心眼,就是擔心哪天突然需要增加分割槽容量,所以在當時使用的是lvm分割槽來安裝系統。當然如果你的是其他分割槽格式,也不用擔心,可以轉換為lvm格式。一 準...