删除、截断或删除以清理 MySQL 中的表

发布于 2024-08-28 07:45:34 字数 144 浏览 6 评论 0原文

我正在尝试清理表格,但没有摆脱表格的实际结构。我有一个自动递增的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

画中仙 2024-09-04 07:45:34

drop table 将删除包含数据的整个表

delete * from table 将删除数据,仅保留自动增量值。如果表中有很多数据,也需要一段时间。

truncate table 将删除数据,重置自动增量值(但将它们保留为自动增量列,因此它会从 1 开始并再次从那里向上),并且速度非常快。

drop table will remove the entire table with data

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

甜嗑 2024-09-04 07:45:34

TRUNCATE 将重置你的自动增量种子(至少在 InnoDB 表上),尽管你可以在截断之前记下它的值,并在之后使用 更改表

ALTER TABLE t2 AUTO_INCREMENT = value

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:

ALTER TABLE t2 AUTO_INCREMENT = value
空城仅有旧梦在 2024-09-04 07:45:34

   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.

终止放荡 2024-09-04 07:45:34

截断表就是您正在寻找的
http://www.1keydata.com/sql/sqltruncate.html

Truncate table is what you are looking for
http://www.1keydata.com/sql/sqltruncate.html

淡忘如思 2024-09-04 07:45:34

另一种可能性涉及创建表的空副本,设置 AUTO_INCRMENT (在非原子操作期间为插入提供一些最终余地),然后旋转两者:

CREATE TABLE t2_new LIKE t2;

SELECT @newautoinc:=auto_increment /*+[leeway]*/ 
  FROM information_schema.tables
 WHERE table_name='t2';

SET @query = CONCAT("ALTER TABLE t2_new AUTO_INCREMENT = ", @newautoinc);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

RENAME TABLE t2 TO t2_old, t2_new TO t2;

然后,您还有一个额外的优势,即仍然可以在之前改变主意删除旧表。

如果您重新考虑您的决定,您仍然可以在操作之前从表中取回旧记录:

INSERT /*IGNORE*/ INTO t2 SELECT * FROM t2_old /*WHERE [condition]*/;

当您准备好时,您可以删除旧表:

DROP TABLE t2_old;

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 :

CREATE TABLE t2_new LIKE t2;

SELECT @newautoinc:=auto_increment /*+[leeway]*/ 
  FROM information_schema.tables
 WHERE table_name='t2';

SET @query = CONCAT("ALTER TABLE t2_new AUTO_INCREMENT = ", @newautoinc);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

RENAME TABLE t2 TO t2_old, t2_new TO t2;

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:

INSERT /*IGNORE*/ INTO t2 SELECT * FROM t2_old /*WHERE [condition]*/;

When you're good you can drop the old table:

DROP TABLE t2_old;
一念一轮回 2024-09-04 07:45:34

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

请止步禁区 2024-09-04 07:45:34

我刚刚遇到过这样一种情况:与全文 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文