Here's the thing , Last week, the boss told me that the disk of the test server was full . He came to me and asked me to help clean it up .Found to be MySQL Of data The catalog is too big , So I went into mysql, It turns out that there are several million level tables that are too big , So I deleted , A meal operation as fierce as a tiger , Space occupation or 95%.
bosses :“ How did you delete ?”
I :“ One of merm -rf / *
”
bosses :“???”
I :“ incorrect , I use delete from Deleted ”
bosses :“ You're not going to work tomorrow .”
Later, in order not to be disliked by the big man ,HaC I added some knowledge ,mysql It turns out that more than delete, also drop 、truncate.
1、delete
grammar :
DELETE FROM t_table;
1) 、DELETE The sentence is DML(Data Manipulation Language, Data manipulation language ), So it just deletes data , Do not delete table structure . Does not reduce the space occupied by tables or indexes .
2)、 The process of execution is to delete one row from the table at a time , It needs to be recorded in the log (binlog, If it's on binlog), Execution is slow , You can add where Conditions .
Develop good habits :delete Add limit Conditions , faster .
If you addlimit 1
Hit the first one and return , You don't have to scan the whole table and go back .
3)、 The index will not be reset , Insert index is still from the last item you deleted +1 Start .
4)、MyISAM Will immediately free up disk space , and InnoDB It won't free up disk space , The data is just invisible to you . It's going to create holes , Mark as reusable , Next time you execute insert, It will cover this part of the space .
5) Go business , If you don't commit, have access to rollback . It also triggers triggers .
2、truncate
grammar :
TRUNCATE TABLE t_table
1)、DDL(Data Definition Language, Database definition language ) operation , The space occupied by the tables and indexes will be restored to the original size .
2)、 No logging , Faster .
3)、 You can only delete table, Cannot be rolled back .
4)、 Delete all records in the table . Reset index position .
3、drop
grammar :
DROP TABLE t_table
1)、DDL(Data Definition Language, Database definition language ) operation .
2)、 Delete everything about the table ( data / structure / constraint …), And release all the space occupied by the table .
3)、 Cannot be rolled back , It doesn't trigger .
4、 Space release test
Create a new table , Insert 10w Data ,
CREATE TABLE `t_coke` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Number ',
`name` varchar(500) DEFAULT NULL COMMENT ' coke ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT=' coke ';
# stored procedure , Insert 10w data
CREATE DEFINER=`root`@`localhost` PROCEDURE `coke`()
begin
declare i int;
set i=1;
while(i<=100000)do
insert into t_coke values(i, "1984 Coca Cola ");
set i=i+1;
end while;
end
Copy two more tables :
CREATE TABLE t_coke_copy1 SELECT * FROM t_coke;
CREATE TABLE t_coke_copy2 SELECT * FROM t_coke;
MySQL Of information_schema.TABLES
Of DATA_LENGTH
Records the data size of the table , We can use the size of this table .
SELECT
concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'M' ) AS table_size
FROM
information_schema.TABLES
WHERE
table_schema = 'test' # Database name
AND table_name = 't_coke_copy2'; # Table name
perform delete:
DELETE FROM t_coke_copy2;
Oh, Ho , The space is bigger , This is the table that creates holes .
Check the size of the hole :
show table status from test like 't_coke_copy2';
The hole has 10m So big , It must have been before delete Through the data , There was no time to release .
perform delete After the statement , It doesn't immediately release space , You can use optimize table
Instructions , But the instruction locks the table ; Or you can use MySQL Automatic cleaning of , It takes time .
OPTIMIZE TABLE t_coke_copy2;
OPTIMIZE after , Check the size again :
It's the initialization size .
Although deleted , But your index continues to increase on the basis of the original :
and truncate
One step in place , Initialize the table directly .
5、 recovery
delete Recovery is easier ( The premise is to turn on binlog):
# binlog Open or not
show variables like 'log_bin';
# binlog The format of
SHOW VARIABLES LIKE 'binlog_format';
find binlog That's all right. , Reference resources :
https://blog.csdn.net/huangba...
You can also use Flashback Tools for recovery .
If the production is accidentally used truncate and drop, How to recover data ?
Ready to run ?
duck No need ! If your MySQL There's full backup , And back up in real time binlog.
1、 If HaC I was at noon 12 Click to delete a table . Last night in the morning 0 Point back up a library .
2、 Back to 0 Point Library ,
3、 take 12 Point to 0 Between the dots binlog To restore .( find binlog Of drop table、truncate table Time nodes of )
4、 Re execution drop table、truncate table Time nodes of At the current time node Of binlog The sentence of , This is equivalent to skipping the deletion statement .
therefore , Backup is important !
6、 summary :
delete | truncate | drop | |
---|---|---|---|
Security | low | in | high |
Space | MyISAM Will immediately free up disk space , and InnoDB It won't free up disk space | Free up disk space immediately , Whether it's Innodb and MyISAM | Free up disk space immediately , Whether it's Innodb and MyISAM |
recovery | recoverable | Irrecoverable | Irrecoverable |
Speed | slow | in | fast |
Business | Go business , Trigger trigger | No business , Don't trigger trigger | No business , Don't trigger trigger |
To put it simply , I have a coke in my hand ,delete It's like hiding coke , Actually, the bottle is still , Coke is still there ;truncate It's to finish the coke , Left a bottle ;drop Just drink the coke and throw away the bottle .
Don't say the , The boss is going to urge me again , I'm going to have a coke , No , perform truncate
.