删除、截断或删除以清理 MySQL 中的表
我正在尝试清理表格,但没有摆脱表格的实际结构。我有一个自动递增的 id 列;我不需要保留ID号,但我确实需要它来保持其自动递增的特性。我发现了删除和截断,但我担心其中之一会完全删除整个表,从而使未来的插入命令变得无用。
如何从表中删除所有记录以便插入新数据?
I am attempting to clean out a table but not get rid of the actual structure of the table. I have an id
column that is auto-incrementing; I don't need to keep the ID number, but I do need it to keep its auto-incrementing characteristic. I've found delete and truncate but I'm worried one of these will completely drop the entire table rendering future insert commands useless.
How do I remove all of the records from the table so that I can insert new data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
drop table
将删除包含数据的整个表delete * from table
将删除数据,仅保留自动增量值。如果表中有很多数据,也需要一段时间。truncate table
将删除数据,重置自动增量值(但将它们保留为自动增量列,因此它会从 1 开始并再次从那里向上),并且速度非常快。drop table
will remove the entire table with datadelete * from table
will remove the data, leaving the autoincrement values alone. it also takes a while if there's a lot of data in the table.truncate table
will remove the data, reset the autoincrement values (but leave them as autoincrement columns, so it'll just start at 1 and go up from there again), and is very quick.TRUNCATE
将重置你的自动增量种子(至少在 InnoDB 表上),尽管你可以在截断之前记下它的值,并在之后使用 更改表:TRUNCATE
will reset your auto-increment seed (on InnoDB tables, at least), although you could note its value before truncating and re-set accordingly afterwards using alter table:Drop 就会这样做....删除有问题的表,除非该表是另一个表的父表。
删除将删除所有符合条件的数据;如果没有指定条件,它将删除表中的所有数据。
截断与删除类似;但是,它将 auto_increment 计数器重置回 1(或初始值)。但是,最好使用截断而不是删除,因为删除会按行删除数据,因此比截断性能更高。但是,truncate 不适用于强制执行引用完整性的 InnoDB 表,除非在发出 truncate 命令之前将其关闭。
所以,放松;除非您在桌子上发出 drop 命令,否则它不会被删除。
Drop will do just that....drop the table in question, unless the table is a parent to another table.
Delete will remove all the data that meets the condition; if no condition is specified, it'll remove all the data in the table.
Truncate is similar to delete; however, it resets the auto_increment counter back to 1 (or the initial starting value). However, it's better to use truncate over delete because delete removes the data by each row, thus having a performance hit than truncate. However, truncate will not work on InnoDB tables where referential integrity is enforced unless it is turned off before the truncate command is issued.
So, relax; unless you issue a drop command on the table, it won't be dropped.
截断表就是您正在寻找的
http://www.1keydata.com/sql/sqltruncate.html
Truncate table is what you are looking for
http://www.1keydata.com/sql/sqltruncate.html
另一种可能性涉及创建表的空副本,设置 AUTO_INCRMENT (在非原子操作期间为插入提供一些最终余地),然后旋转两者:
然后,您还有一个额外的优势,即仍然可以在之前改变主意删除旧表。
如果您重新考虑您的决定,您仍然可以在操作之前从表中取回旧记录:
当您准备好时,您可以删除旧表:
Another possibility involves creating an empty copy of the table, setting the AUTO_INCREMENT (with some eventual leeway for insertions during the non-atomic operation) and then rotating both :
And then, you have the extra advantage of being still able to change your mind before removing the old table.
If you reconsider your decision, you can still bring back old records from the table before the operation:
When you're good you can drop the old table:
我有点晚了,但是在 比较 SQL 中的 TRUNCATE TABLE、DELETE 和 DROP TABLE
I am a bit late but there is a good discussion about DELETE VS TRUCATE VS DROP at Comparing TRUNCATE TABLE, DELETE, and DROP TABLE in SQL
我刚刚遇到过这样一种情况:与全文 InnoDB 查询上的 TRUNCATE 相比,DELETE 极大地影响了 SELECT 性能。
如果我删除所有行,然后重新填充表(100 万行),则典型的查询需要 1 秒才能返回。
如果我截断表,并以完全相同的方式重新填充它,则典型的查询需要 0.05 秒才能返回。
YMMV,但无论出于什么原因,我在 MariaDB 10.3.15-MariaDB-log DELETE 上似乎正在破坏我的索引。
I've just come across a situation where DELETE is drastically affecting SELECT performance compared to TRUNCATE on a full-text InnoDB query.
If I DELETE all rows and then repopulate the table (1million rows), a typical query takes 1s to come back.
If instead I TRUNCATE the table, and repopulate it in exactly the same way, a typical query takes 0.05s to come back.
YMMV, but for whatever reason for me on MariaDB 10.3.15-MariaDB-log DELETE seems to be ruining my index.