MySQL Stored Procedure for table splitting

Source: Internet
Author: User

The MySQL stored procedure is widely used. The following describes the MySQL Stored Procedure for table sharding. It is helpful for you to learn about the MySQL stored procedure.

Mysql tables or sub-tables have a certain amount of data, which may be or ..) at this time, it is very necessary to subscribe to the table, the simple way is to directly write
-- Assume that the table is divided into 64 tables by user_id.

 
 
  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. ...  

A total of 64 SQL statements, OK. However, this table is scanned 64 times by the entire table, which is useless and causes a long downtime.

Although the MySQL stored procedure is not very familiar, I learned a little and wrote two scripts, one full + one incremental script to split the table.
The online database also practices 8 sub-tables. Each sub-table records are split into 64 sub-tables.
The full time is 150 minutes. When full data is used, several sub-tables can be run together. I run three sub-tables at the same time.
The incremental time is 4-minute and 4-minute run for each table Shard, which is 8 minutes in total. In this way, the downtime and application release take 20 minutes.

Script:

###################
Delimeter //

-----------
--- Full script:

 
 
  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;  

-- Define the table for which the cursor is to be split and define the end time of a quantity)

 
 
  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  

-- Load the required value into a variable

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

-- Obtain the table with the data according to the modulo Field

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

-- Assemble dynamic SQL statements

 
 
  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. -- Execute SQL
  8. Execute stmt using @ vv_id;
  9. Deallocate prepare stmt;
  10. End if;
  11. Set ii = I + 1;
  12.  

-- Submit 100 entries at a time to improve efficiency. Remember to set auto_commit before executing the stored procedure.

 
 
  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. #### Incremental script ######
  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> = '2017-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.  

-- Determine whether the data already exists

 
 
  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;       

-- If the data already exists, delete

 
 
  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.  

If the time between the full and incremental operations is long, you can set the time and perform the incremental operations several times to shorten the final downtime, you know...
Call sp_xf_add_item ()//
 

In-depth discussion on MySQL Lock Mechanism

Set of MySQL Fields

MySQL Field Type

Mysql foreign Key Usage Analysis

Detailed description of MySQL Data Table types

   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.