我正在使用MySql 5.5.37。我試圖從一組表中截斷數據。我讀到這裏 - Quickest way to delete enormous MySQL table,即重新創建/重命名錶是假想快,但我得到的錯誤...什麼是截斷MySQL中一組表的更快方法?
SET FOREIGN_KEY_CHECKS=0;
…
CREATE TABLE IF NOT EXISTS new_organization LIKE organization;
RENAME TABLE organization TO old_t, new_organization TO organization;
DROP TABLE old_t;
CREATE TABLE IF NOT EXISTS new_organization_address LIKE organization_address;
RENAME TABLE organization_address TO old_t, new_organization_address TO organization_address;
ERROR 1025 (HY000): Error on rename of './sbjunit_ebook/organization_address' to './sbjunit_ebook/old_t' (errno: 150)
進一步展望問題,它說...
Error in foreign key constraint of table sbjunit_ebook/old_t:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "FK1_ORGANIZTION_ADDRESS" FOREIGN KEY ("ORGANIZATION_ID") REFERENCES "old_t" ("ID") ON DELETE CASCADE ON UPDATE NO ACTION
The index in the foreign key in table is "UK_ORGANIZATION_ADDRESS"
爲什麼重命名不更新外鍵引用的表以及什麼是從一組表中截斷數據的更快方法?
我想看看有沒有更好的除了說「truncate table t」之外,還有一種方法可以截斷表,但是在你的回答中,你是否真的使用了「truncate」語句來清除數據?我誤解你的建議嗎? – Dave
@Dave:是的,如果沒有引用表的外鍵約束,則TRUNCATE是最快的。當沒有外鍵約束時,它相當於刪除現有表並創建一個新的空表。 – spencer7593
聽起來不錯。我的截斷所有表的腳本是自動生成的,因此我需要找出一種方法來刪除並重新創建外鍵。然後我可以針對我現在的情況進行速度測試,然後我會回來接受。 – Dave