索引的重建与查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了索引的重建与查询相关的知识,希望对你有一定的参考价值。
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);
以上是关于索引的重建与查询的主要内容,如果未能解决你的问题,请参考以下文章
执行重建索引查询时出错(ORA-29874 和 ORA-29960)