偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

拆表用的MySQL存儲(chǔ)過(guò)程

數(shù)據(jù)庫(kù) MySQL
MySQL存儲(chǔ)過(guò)程可以實(shí)現(xiàn)許多我們需要的功能,下文介紹的存儲(chǔ)過(guò)程用于拆表,如果您之前遇到過(guò)類(lèi)似的問(wèn)題,不妨一看。

MySQL存儲(chǔ)過(guò)程用途很廣泛,下面就為您介紹拆表用的MySQL存儲(chǔ)過(guò)程,希望對(duì)您學(xué)習(xí)MySQL存儲(chǔ)過(guò)程方面能夠有所幫助。

mysql表或分表的數(shù)據(jù)達(dá)到一定量(也許是800w或者1000w..)這個(gè)時(shí)候非常需要再分表,簡(jiǎn)單的辦法是直接寫(xiě)
--假設(shè)根據(jù)user_id分表,分成64張

  1. insert into table_new_0000 select * from table_old where mod(user_id,64)=0;  
  2. insert into table_new_0001 select * from table_old where mod(user_id,64)=1;  
  3. ...  

一共64條sql,OK 搞定。  但是這個(gè)一張表被全表掃描了64次,做的無(wú)用功比較多,而且導(dǎo)致停機(jī)時(shí)間比較長(zhǎng)。

雖然MySQL存儲(chǔ)過(guò)程不是很熟,稍稍學(xué)習(xí)了下寫(xiě)了兩個(gè)腳本,一個(gè)全量+一個(gè)增量腳本完成表的拆分。
線上庫(kù)也實(shí)踐了下,8個(gè)分表,每個(gè)分表1000W記錄拆分到64個(gè)分表。
全量 時(shí)間 150分鐘,全量的時(shí)候幾個(gè)分表可以一起跑,我是同時(shí)跑3個(gè)分表
增量 時(shí)間 每個(gè)分表4分鐘 4個(gè)一起跑,一共是 8分鐘搞定。 這樣停機(jī)時(shí)間加上應(yīng)用的發(fā)布一共只需要20分鐘就可以搞定了。

附腳本:

###################
delimeter //

-----------
--- 全量腳本:

  1. CREATE PROCEDURE  sp_xf_move_item()  
  2. begin  
  3. declare v_exit int default 0;  
  4. declare v_spid bigint;  
  5. declare v_id bigint;  
  6. declare i int default 0;  
  7. declare c_table int;  

--定義游標(biāo)(要分拆的表,定義一個(gè)數(shù)量的截止時(shí)間)

  1. declare c_ids cursor for select id,user_id from item_records_0000 where gmt_modified < '2010-8-25 00:00:00';  
  2. declare  continue handler for not found set v_exit=1;  
  3. open c_ids;  
  4. repeat  

--將需要的值裝入變量

  1. fetch c_ids into v_id,v_spid;  
  2. if v_exit = 0 then  
  3. set @vv_id = v_id;  

--根據(jù)取模字段獲取數(shù)據(jù)存在的表

  1. select mod(v_spid,64) into c_table;  

--組裝動(dòng)態(tài)sql

  1. SET @SQL_CONTEXT =  
  2. CONCAT('insert into item_record_',  
  3. LPAD(c_table, 4, 0),  
  4. ' select * from item_records_0000 where id = ?');  
  5.  
  6. PREPARE STMT FROM @SQL_CONTEXT;  
  7. --執(zhí)行sql  
  8. EXECUTE STMT using @vv_id;  
  9. DEALLOCATE PREPARE STMT;  
  10. end if;  
  11. set ii=i+1;  
  12.  

--100條提交一次,以提高效率,記得執(zhí)行存儲(chǔ)過(guò)程前設(shè)置auto_commit

  1. if mod(i,100)=0 then commit;  
  2. end if;  
  3. until v_exit=1 
  4. end repeat;  
  5. close c_ids;  
  6. commit;  
  7. end;  
  8. //  
  9.  
  10. -----------  
  11. set auto_commit=0;  
  12. call  sp_xf_move_item();  
  13.  
  14. #### 增量腳本 ######  
  15.  
  16. CREATE PROCEDURE sp_xf_add_item()  
  17. begin  
  18. declare v_exit int default 0;  
  19. declare v_spid bigint;  
  20. declare v_id bigint;  
  21. declare i int default 0;  
  22. declare c_table int;  
  23. declare c_ids cursor for select id,supplier_id from item_records_0000 where gmt_modified >= '2010-8-25 00:00:00';  
  24. declare  continue handler for not found set v_exit=1;  
  25. open c_ids;  
  26. repeat  
  27.  
  28. fetch c_ids into v_id,v_spid;  
  29. if v_exit = 0 then  
  30. set @vv_id = v_id;  
  31. set @v_row=0;  
  32. select mod(v_spid,64) into c_table;  
  33.  

--判斷數(shù)據(jù)是否已經(jīng)存在

  1. SET @SQL_C =  
  2. CONCAT('select count(*) into @v_row from item_record_',  
  3. LPAD(c_table, 4, 0),  
  4. ' where id = ?');  
  5.  
  6. PREPARE STMT_C FROM @SQL_C;  
  7. EXECUTE STMT_C using @vv_id;  
  8. DEALLOCATE PREPARE STMT_C;                         
  9.  
  10. SET @SQL_INSERT =  
  11. CONCAT('insert into bbc_item_record_',  
  12. LPAD(c_table, 4, 0),  
  13. ' select * from item_records_0000 where id = ?');  
  14.  
  15. PREPARE STMT_I FROM @SQL_INSERT;           
  16.  
  17. SET @SQL_DELETE =  
  18. CONCAT('DELETE FROM bbc_item_record_',  
  19. LPAD(c_table, 4, 0),  
  20. ' where id = ?');  
  21. PREPARE STMT_D FROM @SQL_DELETE;       

--如果數(shù)據(jù)已經(jīng)存在,則先delete在insert             

  1. if @v_row>0 then   
  2.  
  3. EXECUTE STMT_D using @vv_id;  
  4. DEALLOCATE PREPARE STMT_D;  
  5.  
  6. end if;  
  7. EXECUTE STMT_I using @vv_id;  
  8. DEALLOCATE PREPARE STMT_I;         
  9.  
  10. end if;  
  11. set ii=i+1;  
  12. if mod(i,100)=0 then commit;  
  13. end if;  
  14. until v_exit=1 
  15. end repeat;  
  16. close c_ids;  
  17. commit;  
  18. end;  
  19. //  
  20.  
  21. -------  
  22.  

如果全量和增量之間的時(shí)間拖的比較長(zhǎng),那么可以設(shè)置時(shí)間,多做幾次增量已縮短最后的停機(jī)時(shí)間,你懂的。。。
call sp_xf_add_item()//
 

 

 

【編輯推薦】

深入探討MySQL鎖機(jī)制

MySQL字段中的集合

MySQL字段類(lèi)型簡(jiǎn)介

Mysql外鍵用法分析

詳解MySQL數(shù)據(jù)表類(lèi)型

   

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-11-24 16:35:01

MYSQL命令行

2010-05-19 14:13:12

MySQL存儲(chǔ)過(guò)程

2021-10-15 06:43:11

數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程

2010-05-19 14:03:41

MySQL 存儲(chǔ)過(guò)程

2010-10-08 16:55:44

MySql存儲(chǔ)過(guò)程

2018-04-18 09:18:44

數(shù)據(jù)庫(kù)MySQL存儲(chǔ)過(guò)程

2010-10-09 16:26:59

mysql存儲(chǔ)過(guò)程

2020-11-26 10:33:44

MySQL存儲(chǔ)函數(shù)

2010-06-01 15:09:55

MySQL 存儲(chǔ)過(guò)程

2019-01-02 13:03:53

MySQL存儲(chǔ)權(quán)限

2010-06-07 15:36:36

MySQL存儲(chǔ)過(guò)程

2009-01-19 08:59:04

PHP調(diào)用MySQL存儲(chǔ)過(guò)程MySQLi擴(kuò)展

2023-07-27 07:03:24

MySQL存儲(chǔ)SQL

2020-11-02 13:24:49

MySQL數(shù)據(jù)庫(kù)存儲(chǔ)

2010-10-09 17:08:15

MySQL存儲(chǔ)過(guò)程

2011-05-03 10:09:37

MySQL存儲(chǔ)引擎

2010-10-28 13:53:13

ORACLE存儲(chǔ)過(guò)程

2010-09-16 16:23:06

sql server批

2011-05-16 13:15:55

MySQL存儲(chǔ)字符集

2010-05-21 12:43:06

MySQL 5.0存儲(chǔ)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)