반응형

인덱스 리빌드는 통계를 업데이트 할까?

 

  • Version : SQL Server 2005, 2008, 2008R2, 2012, 2014

 

인덱스를 리빌드하면 통계가 업데이트 될까? 이 질문에 많은 사람들은 "YES"라고 답할 것이다. 사실은 모든 통계를 업데이트를 하지 않는다. 인덱스를 리빌드 하는경우 해당 인덱스와 관련된 통계만 업데이트 된다.

 

Index Stats

Non-index stats

ALTER INDEX REORG

NO

NO

ALTER INDEX <index_name> REBUILD

해당 인덱스와 관련된 인덱스 통계 업데이트 됨

NO

ALTER INDEX ALL REBUILD

모든 인덱스 통계 업데이트 됨

NO

DBREINDEX (old syntax)

YES

YES

 

위에서 볼 수 있듯이 모든 통계는 인덱스 리빌드 작업을 통해서 업데이트 된다. 때로는 비 인덱스 통계도 매우 중요하다. 비인덱스 통계는 자동 또는 수동으로 생성된 관련 통계를 의미한다. 임계값이 큰 테이블의 경우 수동 통계 업데이트가 필요할 수 있다. 추적플래그 2371이 도움이 될 수 있다.

 

  • Statistical maintenance functionality (autostats) in SQL Server :

 

http://support.microsoft.com/ko-kr/kb/195565

  • Changes to automatic update statistics in SQL Server – traceflag 2371 :

http://blogs.msdn.com/b/saponsqlserver/archive/2011/09/07/changes-to-automatic-update-statistics-in-sql-server-traceflag-2371.aspx

 

실습을 통해 모든 통계를 업데이트 하지 않는 경우를 확인 할 수 있다.

 

[샘플 데이터 생성]

if object_id ('t') is not null

drop table t

go

create table t(c1 int, c2 as c1 & 1)

go

 

create index t1_indx1 on t(c1 )

go

set nocount on

declare @i int

set @i = 0

while @i < 1000

begin

insert into t (c1) values (@i)

set @i = @i + 1

end

go

 

update statistics t with fullscan

go

 

go

--this will create a stats on c2

select count(*) from t where c2 =1

 

go

 

[통계 정보 확인]

SELECT

obj.name, stat.name, stat.stats_id, last_updated

FROM sys.objects AS obj

JOIN sys.stats stat ON stat.object_id = obj.object_id

CROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp

where obj.name = 't'

 

 

[인덱스 리빌드 진행]

-- alter all indexes

alter index all on t rebuild

--re-organize won't update even stats of the index

--alter index all on t reorganize

 

통계 정보를 확인해 보면 t1_index1의 통계는 최근 업데이트가 반영된 것을 확인 할 수 있으며 __WA_Sys_00000002_34C8D9D1 은 업데이트가 되지 않은 것을 확인 할 수 있다.

SELECT

obj.name, stat.name, stat.stats_id, last_updated

FROM sys.objects AS obj

JOIN sys.stats stat ON stat.object_id = obj.object_id

CROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp

where obj.name = 't'

 

 

 

 

 

[참고자료]

http://blogs.msdn.com/b/psssql/archive/2015/03/06/does-rebuild-index-update-statistics.aspx

 

 

2015-03-16 / 강성욱 / http://sqlmvp.kr

 

 

Sqlserver, msslq, sqlmvp, sql index, 인덱스 통계, 인덱스 리빌드, Index Rebuild, Update Statistics, 인덱스 유지관리작업

반응형

+ Recent posts