Home > Database > Mysql Tutorial > body text

使用存储过程和Event事件为Mysql表自动建立分区_MySQL

WBOY
Release: 2016-06-01 13:26:07
Original
1313 people have browsed it

bitsCN.com

闲话少叙,直接上马

BEGIN#Routine body goes here...declare minMonitTime date default CURDATE();DECLARE partname   varchar(50);SET partname=CONCAT('part',CURDATE()-0);#首先新建一个分区set @v_add = CONCAT('ALTER TABLE part_tab12123  PARTITION BY RANGE COLUMNS(date)  (PARTITION partmin VALUES LESS THAN (/'1991-12-13/'))');        -- 定义预处理语句 prepare stm from @v_add;        -- 执行预处理语句execute stm;        -- 释放预处理语句deallocate prepare stm;set @v_add_s = CONCAT('ALTER TABLE part_tab12123  ADD PARTITION  (PARTITION ',partname,' VALUES LESS THAN (/'',minMonitTime,'/'))');        -- 定义预处理语句 prepare stmt from @v_add_s;        -- 执行预处理语句execute stmt;        -- 释放预处理语句deallocate prepare stmt;END
Copy after login
在Event事件中设置每天执行一次,那就会每天都建立一个分区,分区的名字是part20140102,part加上日期,分区已当前日期进行划分

注:因为之前直接语句执行建立分区的时候,语句中如果有参数或者系统的函数,比如CURDATE(),等执行会报错,所以在存储过程中将语句拼接上然后执行

由于该存储过程使用的是ADD添加分区,如果新建的表之前未添加分区,则会报错,所以新建表之后应该先添加一个分区

最后使用:

SELECT            partition_name part,             partition_expression expr,             partition_description descr,             table_rows     FROM            INFORMATION_SCHEMA.partitions     WHERE    	TABLE_NAME='表名'; 
Copy after login
查看已经创建的分区  bitsCN.com
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template