查找表的所有外键的最有效方法是啥?
Posted
技术标签:
【中文标题】查找表的所有外键的最有效方法是啥?【英文标题】:What is the most efficient way to find all foreign keys for a table?查找表的所有外键的最有效方法是什么? 【发布时间】:2021-02-10 06:59:19 【问题描述】:如何最有效地获取外键信息(包括更新/删除规则)?
最明显的方法是使用如下查询:
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。
【问题讨论】:
【参考方案1】:一种解决方案最终是在联接的两侧显式引用表和架构。我知道 information_schema 表(尤其是在 MySQL
我首先尝试反转连接顺序,但没有帮助,或者将 where 放在 key_column_usage 表上,但没有帮助。不过,显式引用连接两侧的架构和表就可以了。
在任何情况下,以下查询平均耗时 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`
【讨论】:
【参考方案2】:这将为您提供有关外键的所有信息
还有更多信息,leke referencd column 和 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 表
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_FOREIGN
给你一个类型
TYPE 一组位标志,其中包含有关外键列的信息,OR 运算在一起。 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
【讨论】:
这很有趣。我会试着找点时间看看加入那张桌子是否更快。我想根据我下面的回答将连接分成两个查询(或运行你的两个查询)可能也会很快,因为看起来你只需要从字面上引用表/模式而不是通过连接来使其快速.以上是关于查找表的所有外键的最有效方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章
通过聚合在pandas组中查找频繁项的最有效方法是啥[重复]