sql 语句系列(列举非索引外键)[八百章之第九章]

列举非索引外键

列举出那些外键没有添加索引。

目的:
1.减少锁。
2.外键添加索引,提示了查询性能,因为要与父表做连接查询做笛卡尔积。

下面只要会复制即可,没有会去从新写一遍的。

select fkeys.table_name,fkeys.constraint_name,fkeys.column_name,ind_cols.index_name
from (
select a.object_id,d.column_id,a.name table_name,b.name constraint_name,d.name column_name
from sys.tables a join 
sys.foreign_keys b
on (a.name='EMP'
and a.object_id=b.parent_object_id
)
join sys.foreign_key_columns c
on (b.object_id=c.constraint_object_id)
join sys.columns d
on (c.constraint_column_id=d.column_id and a.object_id=d.object_id)
) fkeys
left join(
select a.name index_name,b.object_id,b.column_id
from sys.indexes a,sys.index_columns b
where a.index_id= b.index_id
) ind_cols on (fkeys.object_id=ind_cols.object_id and fkeys.column_id=ind_cols.column_id)
where ind_cols.index_name is null
posted @ 2020-03-26 23:44  敖毛毛  阅读(130)  评论(0编辑  收藏  举报