728x90

파티션을 고려하지 못한 부분과 index_id 비교를 잘못해서 인덱스 크기 잘못 계산된 부분 재공유 합니다.
파티션을 이루는 파티션의 개수와 압축을 이용한 파티션의 개수를 같이 보여 줍니다.
SQL Server 2008 Enterprise Edition이 아니면 압축 관련 부분은 주석 처리하시면 됩니다.

select    object_name(max(object_id))

,        max(b.PartitionCnt) as 'PartitionCnt'

,        max(b.CompressionCnt) as 'CompressionPartitionCnt'

,        sum(used_page_count) * 8 as 'Total Used(KB)'

,        sum(reserved_page_count) * 8 as 'Total Reserved(KB)'

,        sum(case when index_id = 0 then used_page_count else 0 end) * 8 as 'Heap(KB)'

,        sum(case when index_id = 1 then used_page_count else 0 end) * 8 as 'Cluster(KB)'

,        sum(case when index_id >= 2 then used_page_count else 0 end) * 8 as 'Index(KB)'

,        max(row_count) as 'RowCount'

,        sum(in_row_used_page_count) * 8 as 'Row Used(KB)'

,        sum(in_row_reserved_page_count) * 8 as 'Row Reserved(KB)'

,        sum(lob_used_page_count) * 8 as 'LOB Used(KB)'

,        sum(lob_reserved_page_count) * 8 as 'LOB Reserved(KB)'

,        sum(row_overflow_used_page_count) * 8 as 'Overflow Used(KB)'

,        sum(row_overflow_reserved_page_count) * 8 as 'Overflow Reserved(KB)'

from    (    select    object_id

            ,        index_id

            ,        sum(used_page_count) as used_page_count

            ,        sum(reserved_page_count) as reserved_page_count

            ,        sum(row_count) as row_count

            ,        sum(in_row_used_page_count) as in_row_used_page_count

            ,        sum(in_row_reserved_page_count) as in_row_reserved_page_count

            ,        sum(lob_used_page_count) as lob_used_page_count

            ,        sum(lob_reserved_page_count) as lob_reserved_page_count

            ,        sum(row_overflow_used_page_count) as row_overflow_used_page_count

            ,        sum(row_overflow_reserved_page_count) as row_overflow_reserved_page_count

            from sys.dm_db_partition_stats with(nolock)

            where object_name(object_id) not like 'sys%'

            group by object_id, index_id

        ) as a

        cross apply (    select    count(aa.object_id) as 'PartitionCnt'

                        ,        sum(case when aa.data_compression > 0 then 1 else 0 end) as 'CompressionCnt'

                        from sys.partitions aa

                        where aa.object_id = a.object_id and aa.index_id = a.index_id

                        group by aa.object_id, aa.index_id

                    ) b

group by object_id

order by 'Total Used(KB)' desc

728x90
728x90

 

/********************************************************************************
해당 데이터베이스의 모든 테이블의 인덱스 정보 보기
********************************************************************************/

DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES

-- ObjectName : 테이블이름
-- ObjectId : 테이블번호
-- IndexName : 인덱스이름
-- IndexId : 인덱스번호0:Heap, 1:Clustered 2:Nonclustered
-- Level : 인덱스수준0:인덱스의리프또는데이터, Heap 경우0
-- Pages : 페이지수
-- MinimumRecordSize : 최소레코드크기
-- MaximumRecordSize : 최대레코드크기
-- ForwardedRecords : 전달된레코드크기(8060byte가넘으면전달된레코드증가)
-- Extents : 익스텐트수
-- ExtentSwitches : 익스텐스간의이동횟수
-- AverageFreeBytes : 검색된페이지에서사용가능한평균바이트수(값이클수록페이지사용률낮음)
-- AveragePageDensity : 평균페이지밀도(백분율값이클수록좋음)
-- ScanDensity : 값이작을수록조각화가많이일어난것임(데이터가인접해있는비율)
-- BestCount : 모든데이터가인접해서연결되어있는경우인스텍트변경횟수
-- ActualCount : 실제익스텐트변경횟수
-- LogicalFragmentation : 논리적조각화(백분율이클수록안좋음)
-- ExtentFragmentation

 

or

 

SELECT Object_name(a.object_id) AS table_name -- 테이블이름
,
a.NAME AS index_name -- 인덱스이름
,
a.index_id -- 인덱스형태1:클러스터인덱스2: 이상이면넌클러스터인덱스
,
a.type -- 0:힙1:클러스터형2:비클러스터형3:XML 4:공간
,
a.type_desc -- HEAP, CLUSTERED, NONCLUSTERED, XML, SPATIAL
,
b.rowcnt -- 데이터수준행수
,
b.rows -- 데이터수준행수
,
b.rowmodctr -- 테이블에대해통계를마지막으로업데이트한이후에삽입, 삭제또는업데이트된행수
FROM sys.indexes AS a
INNER JOIN sys.sysindexes AS b
ON a.NAME = b.NAME
INNER JOIN sys.sysobjects AS c
ON a.object_id = c.idwhere c.xtype = 'U' -- 사용자가작성한테이블
ORDER BY table_name

SELECT Object_name(parent_obj) ,
*
FROM sys.sysobjects
WHERE xtype = 'PK'SELECT *
FROM sys.sysobjects
WHERE xtype = 'U'

 

 

 

728x90
728x90

-- 가장 조각이 많이 난 인덱스 TOP 10
 
 SELECT TOP 10

            DB_NAME() AS 'DatabaseName'
 
          , OBJECT_NAME(S.OBJECT_ID) AS 'TableName'
 
          , I.NAME AS 'IndexName'
 
          , ROUND(AVG_FRAGMENTATION_IN_PERCENT, 2) AS 'Fragmentation %'
 
 FROM SYS.DM_DB_INDEX_PHYSICAL_STATS(DB_ID(),NULL,NULL,NULL,NULL) AS S
 
 INNER JOIN SYS.INDEXES AS I
 
 ON S.OBJECT_ID = I.OBJECT_ID AND S.INDEX_ID = I.INDEX_ID
 
 WHERE S.DATABASE_ID = DB_ID() -- 현재 데이터베이스
 
 AND I.NAME IS NOT NULL -- HEAP은 무시
 
 AND OBJECTPROPERTY(S.OBJECT_ID, 'IsMsShipped') = 0 -- 시스템 개체 무시
 
 ORDER BY [Fragmentation %] DESC

 

728x90

+ Recent posts