sql 語句系列(列舉非索引外鍵)[八百章之第九章]

列舉非索引外鍵

列舉出那些外鍵沒有添加索引。性能

目的:
1.減小鎖。
2.外鍵添加索引,提示了查詢性能,由於要與父表作鏈接查詢作笛卡爾積。code

下面只要會複製便可,沒有會去重新寫一遍的。索引

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
相關文章
相關標籤/搜索