我有一个非聚集索引我想删除(它是聚集索引的副本)。但是,它正由外键约束使用。我希望能够确定哪个约束正在使用索引,因此我可以更新它以使用主键索引。
当我试图放弃它时:
DROP INDEX [idx_duplicate] ON [dbo].[MyTable]
我收到错误:
索引'dbo.MyTable.idx_duplicate'上不允许使用显式DROP INDEX。它用于FOREIGN KEY约束实施。
我试图通过以下查询找到罪魁祸首,但没有运气:
SELECT name
FROM sys.foreign_keys
WHERE OBJECT_NAME (referenced_object_id) = 'idx_duplicate'
答案 0 :(得分:16)
像
这样的东西Select
f.name,
object_name(f.parent_object_id)
From
sys.foreign_keys f
inner join
sys.indexes i
on f.referenced_object_id = i.object_id and
f.key_index_id = i.index_id
Where
i.name = 'idx_duplicate' and
i.object_id = object_id('[dbo].[MyTable]')
答案 1 :(得分:9)
这将告诉您表格,外键和所涉及的列:
select f.name
, parentTable = o.name
, parentColumn = c.name
, foreignTable = ofr.name
, foreignColumn = cfr.name
from sys.foreign_keys f
inner join sys.foreign_key_columns fc on f.object_id = fc.constraint_object_id
inner join sys.objects o on fc.parent_object_id = o.object_id
inner join sys.columns c on fc.parent_column_id = c.column_id
and o.object_id = c.object_id
inner join sys.objects ofr on fc.referenced_object_id = ofr.object_id
inner join sys.columns cfr on fc.referenced_column_id = cfr.column_id
and ofr.object_id = cfr.object_id
inner join sys.indexes i on ofr.object_id = i.object_id
where i.name = 'MyIndex'
答案 2 :(得分:3)
我意识到这篇文章已经有几年的历史了,但我不会放弃任何这样的指数,而不会深入挖掘 LOT 。即使键列相同,非聚集索引 NOT 与聚簇索引相同。 NCI的叶级别比CI的叶级更窄,因此,每页包含比CI更多的信息行。实际上,通过删除NCI,可能会导致性能问题(特别是对于其他表上的插入)。