MySQL自動增刪分割槽 資料庫實現方案

2021-10-19 05:23:38 字數 1820 閱讀 2145

基於資料儲存過程和事件實現

delimiter $$

use `sbtest`$$

drop procedure if exists `create_drop_partition_test1`$$

create definer=`rim`@`%` procedure `create_drop_partition_test1`()

begin

declare exit handler for sqlexception rollback;

start transaction;

/* 到系統表查出這個表的最大分割槽,得到最大分割槽的日期。*/

select replace(partition_name,'p','') into @padd_name from information_schema.partitions

where table_schema='sbtest' and table_name='test1' order by partition_ordinal_position desc limit 1;

/* 分割槽名*/

set @max_date= date(date_add(@padd_name+0, interval 1 day))+0;

/* 分割槽值 */

set @max_date_values= date(date_add(@padd_name+0, interval 2 day))+0;

/* 修改表,在最大分割槽的後面增加乙個分割槽,時間範圍加1天 */

set @padd=concat('alter table test1 add partition (partition p',@max_date,' values less than (',(@max_date_values),'))');

/* 輸出檢視增加分割槽語句*/

prepare p1 from @padd;

execute p1;

deallocate prepare p1;

/* 取出最小的分割槽的名稱,並刪除。注意:刪除分割槽會同時刪除分區內的資料,慎重 */

select partition_name into @psub_name from information_schema.partitions

where table_schema='sbtest' and table_name='test1' order by partition_ordinal_position limit 1;

/* 修改表,刪除最小分割槽*/

set @psub=concat('alter table test1 drop partition ',@psub_name);

prepare p2 from @psub;

execute p2;

deallocate prepare p2;

/* 提交 */

commit ;

end$$

delimiter ;

delimiter ||

drop event if exists `partition_add_drop_event`||

create event partition_add_drop_event

on schedule

every 1 day starts '2020-03-31 00:00:01'

do

begin

call sbtest.`create_drop_partition_test1`;

end ||

delimiter ;

MySQL資料庫分割槽

資料庫分割槽處理 如果一張表的資料量太大的話,myd myi就會變得很大,查詢資料就會變的很慢,我接觸到的是有關溫州計程車網約車gps資料量的查詢,大概資料量為1天4000萬條記錄,不分割槽查詢速度慢到懷疑人生。在物理上我們可以把資料表分割成不同的小塊,查詢資料只需要查詢需要的那一塊資料即可,查詢速...

mysql 表分割槽指令碼 自動生成資料庫表分割槽指令碼

按時間 日期 動態生成sql server資料庫分割槽指令碼,分好區後,手動和表建立關聯即可 生成分割槽指令碼 declare databasename nvarchar 50 資料庫名稱 declare tablename nvarchar 50 表名稱 declare columnname nv...

mysql 資料庫表分割槽

create table if not exists demo range eventid int 11 unsigned not null,event sk int 11 not null,product sk int 11 not null,date sk int 11 not null,dev...