查找 table 的所有外键的最有效方法是什么?

What is the most efficient way to find all foreign keys for a table?

如何最有效地获取外键信息(包括update/delete规则)?

最明显的方法是使用如下查询:

SELECT 
`RC` . `CONSTRAINT_CATALOG` AS `fkCatalog` , 
`RC` . `CONSTRAINT_SCHEMA` AS `fkSchema` , 
`RC` . `CONSTRAINT_NAME` AS `fkName` , 
`RC` . `UPDATE_RULE` AS `onUpdate` ,
`RC` . `DELETE_RULE` AS `onDelete` , 
`RC` . `TABLE_NAME` AS `fkTable` , 
`RC` . `REFERENCED_TABLE_NAME` AS `refTable` , 
`KCU` . `COLUMN_NAME` AS `fkColumn` , 
`KCU` . `REFERENCED_COLUMN_NAME` AS `refColumn` , 
`KCU` . `ORDINAL_POSITION` AS `fkOrdinal` 
FROM 
`INFORMATION_SCHEMA` . `REFERENTIAL_CONSTRAINTS` AS `RC` 
INNER JOIN `INFORMATION_SCHEMA` . `KEY_COLUMN_USAGE` AS `KCU` 
ON 
`KCU` . `CONSTRAINT_SCHEMA` = `RC` . `CONSTRAINT_SCHEMA` AND 
`KCU` . `CONSTRAINT_NAME` = `RC` . `CONSTRAINT_NAME` 
WHERE 
`RC` . `CONSTRAINT_SCHEMA` = ? AND 
`RC` . `TABLE_NAME` = ? 
ORDER BY 
`RC` . `REFERENCED_TABLE_NAME` , `KCU` . `ORDINAL_POSITION`

请参阅以下答案的示例:

How to find all tables that have foreign keys that reference particular table.column and have values for those foreign keys?

https://dba.stackexchange.com/questions/102371/how-to-check-foreign-keys-related-to-a-table

How to find all tables that have foreign keys that reference particular table.column and have values for those foreign keys?

但是在一个中等大小的数据库中(总大小约 20GB,在约 20 个数据库之间有 1000-10000 个表),这可能需要几秒钟(在我的系统上每个查询几乎需要 3 秒)。

是否有更有效的获取方式?

注意:我使用的是MySQL 5.7.

一个解决方案最终是在连接的两侧显式引用 table 和模式。我知道 information_schema tables(尤其是 MySQL <8.0)不是典型意义上的 tables,所以我猜他们不能有效地进行连接有时。

我首先尝试颠倒连接顺序,但没有帮助,或者将 where 放在 key_column_usage table 上,但没有帮助。不过,在连接的两侧显式引用模式和 table 就可以了。

在任何情况下,以下查询平均耗时 1-2 毫秒,而上面的查询平均耗时不到 3 秒(数字来自 performance_schema 的 1000 次左右的查询)。

SELECT 
`RC` . `CONSTRAINT_CATALOG` AS `fkCatalog` , 
`RC` . `CONSTRAINT_SCHEMA` AS `fkSchema` , 
`RC` . `CONSTRAINT_NAME` AS `fkName` , 
`RC` . `UPDATE_RULE` AS `onUpdate` , 
`RC` . `DELETE_RULE` AS `onDelete` , 
`RC` . `TABLE_NAME` AS `fkTable` , 
`RC` . `REFERENCED_TABLE_NAME` AS `refTable` , 
`KCU` . `COLUMN_NAME` AS `fkColumn` , 
`KCU` . `REFERENCED_COLUMN_NAME` AS `refColumn` , 
`KCU` . `ORDINAL_POSITION` AS `fkOrdinal` 
FROM 
`INFORMATION_SCHEMA` . `KEY_COLUMN_USAGE` AS `KCU` 
INNER JOIN 
`INFORMATION_SCHEMA` . `REFERENTIAL_CONSTRAINTS` AS `RC` 
ON 
`KCU` . `CONSTRAINT_SCHEMA` = `RC` . `CONSTRAINT_SCHEMA` AND 
`KCU` . `CONSTRAINT_NAME` = `RC` . `CONSTRAINT_NAME` 
WHERE 
`KCU` . `TABLE_SCHEMA` = ? AND -- The addition of this and the next row are the only real difference.
`KCU` . `TABLE_NAME` = ? AND 
`RC` . `CONSTRAINT_SCHEMA` = ? AND 
`RC` . `TABLE_NAME` = ? 
ORDER BY 
`KCU` . `REFERENCED_TABLE_NAME` , `KCU` . `ORDINAL_POSITION`

这将为您提供有关外键的所有信息

还有很多信息,leke referencd column and refrencd table

SELECT 
   TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME
   FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
   WHERE REFERENCED_TABLE_SCHEMA IS NOT NULL;

此外 mysql 5.7 中有此选项但仅适用于 innodb tables

 SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_FOREIGN

这给了你一个类型

TYPE A collection of bit flags with information about the foreign key column, ORed together. 1 = ON DELETE CASCADE, 2 = ON UPDATE SET NULL, 4 = ON UPDATE CASCADE, 8 = ON UPDATE SET NULL, 16 = ON DELETE NO ACTION, 32 = ON UPDATE NO ACTION.

http://mysql.babo.ist/#/en/innodb-sys-foreign-table.html