mysql儲存過程按月建立表分割槽 方式二

2021-07-09 03:44:36 字數 4885 閱讀 5243

這篇文章與mysql儲存過程按月建立表分割槽 方式一 寫的是相同的內容,只是在表分割槽建立方式上不一樣。方式一是以less than(『yyyy-mm-dd』)含引號作為分割槽格式,這樣分割槽因為含有了引號,再執行sql查詢資料屬於哪個分區內時,容易產生問題;查詢sql:

select partition_name part,partition_description,partition_ordinal_position

from information_schema.partitions

where table_schema = schema() and table_name='表名'

and partition_description>='yyyy-mm-dd'。

因含有引號在查詢時得到的結果永遠都是 maxvalue或者null記錄。

所有就重新寫了這一種分割槽方式

#--設定mysql自動允許定時任務

setglobal event_scheduler =1;

#--建日誌表

create

table

`sys_log_storage` (

`id` bigint(32) not

null comment 'id',

`ip`

varchar(16) default

null comment '伺服器ip',

`lvl`

varchar(16) default

null comment '日誌級別',

`title`

varchar(64) default

null comment '標題',

`content`

varchar(1024) default

null,

`createtime` datetime not

null comment '建立時間',

`classname`

varchar(128) default

null,

`method`

varchar(64) default

null

) engine=innodb charset=utf8 comment='日誌儲存';

#--分割槽儲存過程

create

procedure pro_sys_logbymonth(in tablename varchar(20),in timecolname varchar(20))

comment '每月按時新增表分割槽的儲存過程,由定時任務呼叫'

begin

declare p_id int;

declare nextdate date;

declare lastdate long;

#--獲取表中的現有的分割槽數量數量

select

count(partition_name) into p_id from information_schema.partitions

where table_schema = schema() and table_name=tablename;

if p_id=0 then

#--獲取下個月第一天的時間值,根據此值設定時間分割槽

select date_add(curdate()-day(curdate())+1,interval

1month) into nextdate from dual;

set @v_add=concat('alter table ',tablename,' partition by range(to_days(',timecolname,'))

(partition ',concat('par',p_id),' values less than (to_days(\'',nextdate,'\')))');

else

#--獲取表中現有的最大的分割槽日期

select

max(partition_description) des into lastdate from information_schema.partitions

where table_schema = schema() and table_name=tablename;

select date_add(from_days(lastdate),interval

1month) into nextdate from dual;

set @v_add=concat('alter table ',tablename,' add partition (partition ',concat('par',p_id),

' values less than (to_days(\'',nextdate,'\')))');

endif;

prepare stmt from @v_add;

execute stmt;

deallocate prepare stmt;

end#--每月建立乙個分割槽的定時任務

create event event_syslog on schedule every 1

month starts current_timestamp

on completion preserve

enable

docall pro_sys_logbymonth('sys_log_storage','createtime');

#--新增聯合索引

alter

table sys_log_storage add

primary

key(id,createtime);

alter

table sys_log_storage change id id bigint(32) not

null auto_increment;

alter

table sys_log_storage auto_increment=1;

#--新增索引

alter

table sys_log_storage add index inx_syslog_ip(ip);

#--檢視sql執行時,查詢了那些分割槽,及使用的索引

explain partitions select * from sys_log_storage where ip='125.76.249.62'

and createtime <'2016-01-20';

當資料量大時,需要調整分割槽規則,比如按周或者天,**如下:

-- 按日期格式自動新增分割槽儲存過程

create

procedure pro_sys_logbyweekday (in tablename varchar(20),in timecolname varchar(20),in dateformat varchar(10))

comment '按日期格式(年year,月month,周week,日day)新增表分割槽的儲存過程,由定時任務呼叫'

begin

declare p_id int;

declare lasttime long;

declare nexttime varchar(20);

-- 獲取表中現有的最大的分割槽日期

select

max(partition_ordinal_position),max(partition_description) des into p_id,lasttime from information_schema.partitions where table_schema = schema() and table_name=tablename;

-- lasttime的值是'2015-12-01' 含引號格式的字串 必須轉換成沒有引號的字串

set @v_add_a=concat('select adddate(from_days(',lasttime,'),interval 1 ',dateformat,') into @nexttime from dual ');

prepare stm from @v_add_a;

execute stm;

deallocate prepare stm;

-- 將編譯執行的stm結果儲存到lasttimeadd中

set nexttime=@nexttime;

set @v_add=concat('alter table ',tablename,' add partition (partition ',concat('par',p_id),' values less than (to_days(\'',nexttime,'\')))');

prepare stmt from @v_add;

execute stmt;

deallocate prepare stmt;

end-- 建立每週生成一次表分割槽的定時任務

create event event_syslogweek on schedule every 1

month starts current_timestamp

on completion preserve

enable

docall pro_sys_logbyweekday('sys_log_storage6','createtime','week');

-- 建立每天生成一次表分割槽的定時任務

create event event_syslogday on schedule every 1

day starts current_timestamp

on completion preserve

enable

docall pro_sys_logbyweekday('sys_log_storage6','createtime','day');

mysql 儲存過程建立多個表

要建立64個表,聽到這一需求,必然要用儲存過程。直接上 delimiter use table drop procedure if exists p create definer root localhost procedure p begin declare i int declare table...

儲存過程建立表

create or replace procedure test1 tname varchar2 is v createsql varchar2 400 v dropsql varchar2 100 v count number 9 begin v createsql create table tn...

mysql 按月分表的查詢 mysql分表 查詢

標籤 垂直分表 其實沒啥好講,就是 主鍵 常用列放在原表中,再講 主鍵 一些不常用列放在另外的表中。這樣乙個資料頁就可以存放更多資料。但是缺點也明顯,可能會增加join 或 union之類的操作。水平分表 今天面試被問到水平分表,突然愣住了,分都知道,但分完如何有效查詢就不好說了。原則 具體情況具體...