当前位置:网站首页>Detailed explanation of delete, truncate and drop principles in MySQL database
Detailed explanation of delete, truncate and drop principles in MySQL database
2022-04-23 09:03:00 【Lens5935】
How to delete data :
Three common deletion methods : adopt delete、truncate、drop Keywords to delete , All three can be used to delete data , But the scene is different .
Execution speed :
drop > truncate > delete
DELETE principle :
- DELETE Belongs to the database DML Operating language , Delete only the data, not the structure of the table , I can walk away from business , It will trigger trigger;
- stay InnoDB in ,DELETE It doesn't really delete the data ,MySQL In fact, it just marks the deleted data as deleted , therefore delete When deleting data from a table , Table files will not take up less space on disk , Storage will not be released , Just make the deleted data rows invisible . Although no disk space has been released , But the next time you insert data , You can still reuse this space ( reusing → Cover ).
- DELETE Execution time , It will cache the deleted data to rollback segement in , Business commit Effective after ;
- delete from table_name Delete all data from the table , about MyISAM Will immediately free up disk space ,InnoDB It won't free up disk space ;
- about delete from table_name where xxx Conditional deletion , Whether it's InnoDB still MyISAM Will not free up disk space ;
- delete Use after operation optimize table table_name Will immediately free up disk space . Whether it's InnoDB still MyISAM . So in order to free up disk space ,delete Later optimize table operation .
Syntax for viewing the amount of hard disk space occupied by the table (MB) as follows :
select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') as table_size
from information_schema.tables
where table_schema='test' AND table_name='aaa';
Then execute the space optimization statement , And the table size change after execution :
And then look at the size of this table , Only the size of the table structure is left .
delete The operation is to delete line by line , At the same time, the The deletion log is recorded in redo and undo Table space for rollback (rollback) And redo operations , The large amount of logs generated will also take up disk space .
truncate principle :
Truncate table TABLE_NAME
- truncate Belongs to the database DDL Define language , No business , The original data will not be put into rollback segment in , Operation does not trigger trigger, Take effect immediately , Unable to retrieve .
- truncate table table_name Free up disk space immediately , Whether it's InnoDB and MyISAM .truncate table It's kind of like drop table then create, It's just this create table The process has been optimized , For example, the table structure file has already existed before, and so on . So the speed should be close to drop table The speed of .
- truncate Can quickly clear a table . And reset auto_increment Value . But for different types of storage engines, what you need to pay attention to is : about MyISAM,truncate Reset auto_increment( Autogenous sequence ) The value of is 1. and delete The back table remains auto_increment. about InnoDB,truncate Reset auto_increment The value of is 1.delete The back table remains auto_increment. But doing delete Restart after the entire table MySQL Words , After the restart auto_increment Will be set to 1. in other words ,InnoDB The table itself cannot be persisted auto_increment.delete After the table auto_increment Still in memory , But it was lost after the restart , Only from 1 Start . Essentially after the restart auto_increment From SELECT 1+MAX(ai_col) FROM t Start .
drop principle :
Drop table Tablename
- drop: Belongs to the database DDL Define language , Same as Truncate; Take effect immediately , Unable to retrieve
- drop table table_name Free up disk space immediately , Whether it's InnoDB and MyISAM;
- drop Statement will delete the constraint that the structure of the table is dependent on (constrain)、 trigger (trigger)、 Indexes (index); Stored procedures that depend on the table / The function will keep , But it became invalid state .
版权声明
本文为[Lens5935]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230720420661.html
边栏推荐
- LaTeX数学公式
- 2021李宏毅机器学习之Adaptive Learning Rate
- valgrind和kcachegrind使用運行分析
- Write down the post order traversal of the ~ binary tree
- Study notes of deep learning (8)
- 【原创】使用System.Text.Json对Json字符串进行格式化
- Technological innovation in government affairs in the construction of Digital Government
- 搜索树判断 (25 分)
- Notes on xctf questions
- L2-022 rearrange linked list (25 points) (map + structure simulation)
猜你喜欢
L2-024 部落 (25 分)(并查集)
bashdb下载安装
K210 learning notes (II) serial communication between k210 and stm32
Pctp test experience sharing
Notes d'apprentissage oneflow: de functor à opexprinterpreter
GUI编程简介 swing
DJ music management software pioneer DJ rekordbox
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
资源打包关系依赖树
Open services in the bottom bar of idea
随机推荐
Matlab draw five-star red flag
2022-04-22 openebs cloud native storage
资源打包关系依赖树
Share the office and improve the settled experience
Is Zhongyan futures safe and reliable?
L2-022 重排链表 (25 分)(map+结构体模拟)
Complete binary search tree (30 points)
To remember the composition ~ the pre order traversal of binary tree
LeetCode396. Rotate array
Failed to prepare device for development
Data visualization: use Excel to make radar chart
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
On array replication
Learn SQL injection in sqli liabs (Level 11 ~ level 20)
web页面如何渲染
[C language] document operation
L2-024 tribe (25 points) (and check the collection)
完全二叉搜索树 (30 分)
Summary of solid problems
LaTeX数学公式