Bug #28947 Optimize foreign key checks for truncate table with empty referencing table
Submitted: 7 Jun 2007 14:47 Modified: 13 May 2010 16:03
Reporter: Ed Dawley Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S4 (Feature request)
Version:5.0.33 OS:Any
Assigned to: CPU Architecture:Any
Tags: innodb truncate

[7 Jun 2007 14:47] Ed Dawley
Description:
According to the docs, truncating an innodb table that is referenced in a foreign key of another table uses a "slow" delete in which every row must be checked for references.  For large tables, this is very painful.

A common use-case for foreign keys is the parent/child schema paradigm in which the child has a single foreign key to the parent.  Truncating the child table is very fast since it is not referenced by any other tables.  However, the parent table invokes the slow delete even though it is not possible for a foreign key to fail after the child has been truncated.

I propose adding in a check at the beginning of truncate to perform a fast delete if all referencing tables are empty.

There should be no MVCC/transaction issues since:

"Truncate operations are not transaction-safe; an error occurs when attempting one in the course of an active transaction or active table lock."

How to repeat:
create a parent table
create a child table with a not null foreign key to parent

insert 1,000,000 rows into parent
insert 1,000,000 row into child

truncate table child;   <- fast
truncate table parent;  <- slow
[7 Jun 2007 18:56] Valeriy Kravchuk
Thank you for a reasonable feature request