mysql動態分割槽 MySQL動態建立分割槽

2021-10-17 13:31:57 字數 1370 閱讀 9526

按日期分割槽的資料表,我們希望每年、每個月甚至每天動態建立乙個分割槽,這種情況就需要用事件和儲存過程來實現動態新增分割槽,下面的儲存過程是按年分割槽增加當年分割槽的過程:

begin

#routine body goes here...

declare currenttime date default curdate();

declare partname varchar(12);

declare i_part_name varchar(12);

declare i_flag int default 0;

declare currentyear int default 0;

#指標遍歷分割槽名,避免定義重複分割槽名

declare cur_partition cursor for select partition_name part from information_schema.partitions

where table_schema = schema() and table_name='test';

#此句必須新增,不然會出現死迴圈,含義是當遍歷指標為空後賦值此變數為null

declare continue handler for sqlstate '02000' set i_part_name = null;

#定義欲建立的分割槽名,用於遍歷匹配已有分割槽名

set partname=concat('part_',year(currenttime));

open cur_partition;

fetch cur_partition into i_part_name;

while (i_part_name is not null)

doif i_part_name = partname then

set i_flag = 1;

end if;

fetch cur_partition into i_part_name;

end while;

close cur_partition;

#如果沒有建立分割槽則建立當前年份的分割槽

if i_flag = 0 then

set currentyear=year(currenttime) + 1;

set @v_add_s = concat('alter table test add partition (partition ',partname,' values less than (',currentyear,'))');

prepare stmt from @v_add_s;

execute stmt;

deallocate prepare stmt;

end if;

end

mysql動態選取列 在mysql中選擇動態列

是否可以遍歷這樣的表 mysql select from stackoverflow results id type criteria id result 1 car env 1 2 car gas 3 car age 4 bike env 1 5 bike gas 6 bike age 1 7 b...

mysql分割槽邊 mysql分割槽

檢視資料庫版本是否支援分割槽 分割槽的四種型別 range分割槽 範圍分割槽,根據某個欄位的值來進行分割槽,某個連續的區間來進行區分 建立表時分區create table teacher id varchar 20 not null name varchar 20 age varchar 20 bi...

mysql 分割槽管理 mysql分割槽管理

一 如何管理range和list分割槽 以該分割槽表為例 create table members id int,fname varchar 25 lname varchar 25 dob date partition by range year dob partition p0 values le...