MS SQL 查詢資料庫中所有索引以及對應的表字段 SQL Server Profiler效能跟蹤
阿新 • • 發佈:2018-12-27
新專案上線,需要對庫裡的表進行相關索引檢查。這兒首先需要檢視一下庫裡的表那些有索引,然後用SQL Server Profiler進行跟蹤,檢查SQL語句效能,查詢頻率等。
查詢庫裡所有索引相關資訊這兒給出一個SQL,使用了CTE查詢:
with temp as( select a.name as tabname,a.object_id ,h.name as indexName,h.index_id,ic.column_id,c.name colName from sys.objects as a right join sys.indexes as h on a.object_id=h.object_id left join sys.index_columns ic on h.index_id=ic.index_id and ic.object_id=a.object_id left join sys.columns c on ic.column_id=c.column_id and c.object_id=a.object_id where a.type<>'s' and a.type='U' --and isnull(h.name,'')='' --沒有索引的表 and a.is_ms_shipped<>1 --排除 dtproperties ) select distinct temp.tabname,indexName ,stuff(( select ',' + tc.name from sys.columns tc left join sys.index_columns ic on ic.column_id=tc.column_id and ic.object_id=temp.object_id where tc.object_id=temp.object_id and ic.index_id=temp.index_id and tc.column_id in ( select column_id from temp where object_id=tc.object_id and ic.index_id=temp.index_id ) for xml path('') ),1,1,'' ) columnName from temp order by temp.indexName
效能跟蹤是需要花費一些時間的,首先建立跟蹤:
建立跟蹤後對站點相關功能進行瀏覽測試,最後根據跟蹤結果(可使用sql server profiler開啟儲存的.trc檔案)。如圖:
對SQL耗時以及呼叫頻率高的SQL進行優化。索引不是什麼欄位什麼情況都加的 具體的相關SQL,索引優化參考:http://www.cnblogs.com/zengxiangzhan/archive/2009/12/04/1617186.html