How to delete a large table without affecting production in mysql

mysql methods commonly removed substantially has the following three ways:

1、delete  

  Generally used to remove a small amount of data in the table

  Optimization tips, be sure to add where condition, and must have a primary key or an index on where conditions are listed. Otherwise, there will be cases of full table scan

2、drop   

  Directly to the table removed, including the definition of tables and tables of data.

  This operation, MySQL will add a global lock, block the operation during deletion, so large tables this operation will certainly produce great impact

  Optimization suggestions, do not use the drop table large table this way, consider the way truncate table

3、truncate  

  This way superior to drop table, drop should be smaller than the risk, no lock, so the speed of deleting the table quickly

  Note: Although this method is fast, but need to consider deleting large files generated by IO

 

Use the following show the operation safer Whew!

Each of the above three methods applicable scenario, the following table presents a large deletion and delete large files at the same time be able to circumvent the way to bring IO risk of overload.

 

1, create a hard link to a file idb table, there are two points to the inode file on this file is actually stored. When the file has two or more hard links, delete the file, the operating system will only delete the hard links instead of actual file

2, drop table table_name; this is actually a hard link ibd deleted files, the speed will be very fast, regardless of the lock operation

3, so drop off the table after the data file is actually still there, so we have to delete a file in the file system

4, using the operating system would cut truncate tool to specify the file to the specified size, so we can use this tool to control the speed of deleting files, which means you can control the usage of IO

  truncate tool Example:

  

for I in   ` SEQ     300 - 10  10 `;
 do 
 SLEEP  2 
 TRUNCATE -s {I} $ G / Data / test.ibd.hardlk
 DONE 
RM -rf /data/mysql/mytest/erp.ibd.hardlk 
the above code is speaking each time a file is reduced 300G 10G, two second intervals, to remove the last remaining 10G with rm -rf.

 

 

Guess you like

Origin www.cnblogs.com/cmgg/p/11511711.html