列举出那些外键没有添加索引。
目的:
1.减少锁。
2.外键添加索引,提示了查询性能,因为要与父表做连接查询做笛卡尔积。
下面只要会复制即可,没有会去从新写一遍的。
select fkeys.table_name,fkeys.constraint_name,fkeys.column_name,ind_cols.index_namefrom (select a.object_id,d.column_id,a.name table_name,b.name constraint_name,d.name column_namefrom sys.tables a join sys.foreign_keys bon (a.name=‘EMP‘and a.object_id=b.parent_object_id)join sys.foreign_key_columns con (b.object_id=c.constraint_object_id)join sys.columns don (c.constraint_column_id=d.column_id and a.object_id=d.object_id)) fkeysleft join(select a.name index_name,b.object_id,b.column_idfrom sys.indexes a,sys.index_columns bwhere 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