通用分表儲存過程

2021-09-26 04:16:14 字數 4121 閱讀 1977

使用建立分表儲存過程

set @field_list ='

`syslog_id` int(11) not null auto_increment,

`create_user` varchar(32) default null,

primary key (`syslog_id`)

';

call branch_table('test', @field_list, 4, 2);

建立分表儲存過程

drop procedure if exists `branch_table`;

create procedure `branch_table`(

in p_table_name varchar(200),

in p_field_list varchar(2048),

in p_branch_size int,

in p_lpad int

)begin

/*定義變數*/

declare m_begin_row int default 0;

while m_begin_row前期沒有分表資料量太大後期拆表用的mysql儲存過程

簡單的辦法是直接寫

--假設根據user_id分表,分成64張

insert into table_new_0000 select * from table_old where mod(user_id,64)=0;

insert into table_new_0001 select * from table_old where mod(user_id,64)=1;

一共64條sql,ok 搞定。但是這個一張表被全表掃瞄了64次,做的無用功比較多,而且導致停機時間比較長

建立分表

delimeter //

--- 全量指令碼:

create procedure sp_xf_move_item()

begin

declare v_exit int default 0;

declare v_spid bigint;

declare v_id bigint;

declare i int default 0;

declare c_table int;

--定義游標(要分拆的表,定義乙個數量的截止時間)

declare c_ids cursor for select id,user_id from item_records_0000 where gmt_modified < '2010-8-25 00:00:00';

declare continue handler for not found set v_exit=1;

open c_ids;

repeat

--將需要的值裝入變數

fetch c_ids into v_id,v_spid;

if v_exit = 0 then

set @vv_id = v_id;

--根據取模字段獲取資料存在的表

select mod(v_spid,64) into c_table;

--組裝動態sql

set @sql_context =

concat('insert into item_record_',

lpad(c_table, 4, 0),

' select * from item_records_0000 where id = ?');

prepare stmt from @sql_context;

--執行sql

execute stmt using @vv_id;

deallocate prepare stmt;

end if;

set ii=i+1;

--100條提交一次,以提高效率,記得執行儲存過程前設定auto_commit

if mod(i,100)=0 then commit;

end if;

until v_exit=1

end repeat;

close c_ids;

commit;

end;

//

set auto_commit=0; 

call sp_xf_move_item(); 

新增資料

#### 增量指令碼 ######  

create procedure sp_xf_add_item()

begin

declare v_exit int default 0;

declare v_spid bigint;

declare v_id bigint;

declare i int default 0;

declare c_table int;

declare c_ids cursor for select id,supplier_id from item_records_0000 where gmt_modified >= '2010-8-25 00:00:00';

declare continue handler for not found set v_exit=1;

open c_ids;

repeat

fetch c_ids into v_id,v_spid;

if v_exit = 0 then

set @vv_id = v_id;

set @v_row=0;

select mod(v_spid,64) into c_table;

--判斷資料是否已經存在

set @sql_c =

concat('select count(*) into @v_row from item_record_',

lpad(c_table, 4, 0),

' where id = ?');

prepare stmt_c from @sql_c;

execute stmt_c using @vv_id;

deallocate prepare stmt_c;

set @sql_insert =

concat('insert into bbc_item_record_',

lpad(c_table, 4, 0),

' select * from item_records_0000 where id = ?');

prepare stmt_i from @sql_insert;

set @sql_delete =

concat('delete from bbc_item_record_',

lpad(c_table, 4, 0),

' where id = ?');

prepare stmt_d from @sql_delete;

--如果資料已經存在,則先delete在insert

if @v_row>0 then

execute stmt_d using @vv_id;

deallocate prepare stmt_d;

end if;

execute stmt_i using @vv_id;

deallocate prepare stmt_i;

end if;

set ii=i+1;

if mod(i,100)=0 then commit;

end if;

until v_exit=1

end repeat;

close c_ids;

commit;

end;

//

call sp_xf_add_item()

通用儲存過程

alter proc dbo pagination pagesize int 10 每頁顯示的記錄數 pagecurrent int 1 當前要顯示的頁號 fdname varchar 100 主鍵名或者標識列名 selectstr varchar 2000 select子句,不包含select關鍵...

通用儲存過程

set ansi nulls on set quoted identifier on gocreate procedure dbo getdatawithpage tablename nvarchar max 表名 fields nvarchar max 各欄位 where nvarchar max...

通用儲存過程 分頁儲存過程

名稱 spall returnrows 輸入 輸出 呼叫 exec spall returnrows select from 表名 頁號,返回記錄數,主鍵 排序字段 spall returnrows select from all categories 2,10,id id 說明 百萬級 通用儲存過...