索引的重建與查詢
阿新 • • 發佈:2017-07-06
where _id 錯誤信息 set 提高 ble cnblogs 指定表 physical
sp_helpindex ‘tablename‘ ---------------------------------------------------------------------------------- --查詢當前數據庫的所有索引值 SELECT database_id,object_id,index_id,object_name(object_id) as table_name, avg_fragmentation_in_percent,fragment_count, avg_fragment_size_in_pages FROM sys.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,‘DETAILED‘); ---------------------------------------------------------------------------- --查詢所有數據庫的所有索引值 SELECT I.database_id,D.NAME,I.object_id,I.index_id,object_name(object_id) as table_name, I.avg_fragmentation_in_percent,I.fragment_count, I.avg_fragment_size_in_pages FROM sys.dm_db_index_physical_stats(null,NULL,NULL,NULL,‘DETAILED‘) I left join sys.databases D on D.DATABASE_ID=I.DATABASE_ID; ------------------------------------------------------------------------- --顯示指定的表的數據和索引的碎片信息 use datbase --數據庫 declare @table_id int set @table_id=object_id(‘tablename‘) --所要查詢索引所在表 dbcc showcontig(@table_id) ---------------------------------------------------------------------- /* DBCC SHOWCONTIG 正在掃描 ‘tablename‘ 表... 表: ‘tablename‘ (1149299204);索引 ID: 1,數據庫 ID: 18 已執行 TABLE 級別的掃描。 - 掃描頁數................................: 327 - 掃描區數..............................: 45 - 區切換次數..............................: 291 - 每個區的平均頁數........................: 7.3 - 掃描密度 [最佳計數:實際計數].......: 14.04% [41:292] - 邏輯掃描碎片 ..................: 85.32% --重點查詢區域 - 區掃描碎片 ..................: 31.11% --重點查詢區域 - 每頁的平均可用字節數.....................: 3435.6 - 平均頁密度(滿).....................: 57.55% DBCC 執行完畢。如果 DBCC 輸出了錯誤信息,請與系統管理員聯系。 */ ----------------------------------------------------------------------------------- --重建索引,提高查詢效率 dbcc dbreindex(tablename,‘‘,0) ----------------------------------------------------------------------------------- use database GO -- 查詢一個表中的索引及索引列 SELECT indexname = a.name , tablename = c. name , indexcolumns = d .name , a .indid FROM sysindexes a JOIN sysindexkeys b ON a .id = b .id AND a .indid = b.indid JOIN sysobjects c ON b .id = c .id JOIN syscolumns d ON b .id = d .id AND b .colid = d .colid WHERE a .indid NOT IN ( 0 , 255 ) -- and c.xtype=‘U‘ and c.status>0 -- 查所有用戶表 --AND c .name = ‘DatabaseLog‘ --查指定表 ORDER BY c. name ,a.name ,d.name --------------------------------------------------------------------------------------------- SELECT tab.name AS [表名], idx.name AS [索引名稱], col.name AS [列名] FROM sys.indexes idx JOIN sys.index_columns idxCol ON (idx.object_id = idxCol.object_id AND idx.index_id = idxCol.index_id ) JOIN sys.tables tab ON (idx.object_id = tab.object_id) JOIN sys.columns col ON (idx.object_id = col.object_id AND idxCol.column_id = col.column_id);
索引的重建與查詢