SQL Server 인덱스와 통계


13

의 차이점은 무엇입니까 CREATE INDEX그리고 CREATE STATISTICS때 나는 각 사용해야은?

답변:


19

인덱스는 실제 데이터 (우리가 이야기하는 인덱스 유형에 따라 데이터 페이지 또는 인덱스 페이지)와 통계 저장소 데이터 배포를 저장합니다. 따라서 CREATE INDEX인덱스 (클러스터, 비 클러스터 등) CREATE STATISTICS를 작성하는 DDL이되고 테이블 내의 열에 대한 통계를 작성하는 DDL이됩니다.

관계형 데이터의 이러한 측면에 대해 읽어 보는 것이 좋습니다. 다음은 초급 입문 기사입니다. 이것들은 매우 광범위한 주제이므로 이에 관한 정보는 매우 넓고 깊어 질 수 있습니다. 아래의 일반적인 아이디어를 읽고 더 구체적인 질문이 있으면 물어보십시오.

테이블 및 인덱스
구성에 대한
BOL 참조 클러스터형 인덱스 구조에 대한 BOL 참조 비 클러스터형 인덱스 구조
에 대한 BOL 참조 인덱스 소개에 대한 SQL Server Central
통계에 대한 BOL 참조

다음은이 두 부분이 실제로 작동하는 것을 보여주는 실제 예입니다 (설명으로 주석 처리됨).

use testdb;
go

create table MyTable1
(
    id int identity(1, 1) not null,
    my_int_col int not null
);
go

insert into MyTable1(my_int_col)
values(1);
go 100

-- this statement will create a clustered index
-- on MyTable1.  The index key is the id field
-- but due to the nature of a clustered index
-- it will contain all of the table data
create clustered index MyTable1_CI
on MyTable1(id);
go


-- by default, SQL Server will create a statistics
-- on this index.  Here is proof.  We see a stat created
-- with the name of the index, and the consisting stat 
-- column of the index key column
select
    s.name as stats_name,
    c.name as column_name
from sys.stats s
inner join sys.stats_columns sc
on s.object_id = sc.object_id
and s.stats_id = sc.stats_id
inner join sys.columns c
on sc.object_id = c.object_id
and sc.column_id = c.column_id
where s.object_id = object_id('MyTable1');


-- here is a standalone statistics on a single column
create statistics MyTable1_MyIntCol
on MyTable1(my_int_col);
go

-- now look at the statistics that exist on the table.
-- we have the additional statistics that's not necessarily
-- corresponding to an index
select
    s.name as stats_name,
    c.name as column_name
from sys.stats s
inner join sys.stats_columns sc
on s.object_id = sc.object_id
and s.stats_id = sc.stats_id
inner join sys.columns c
on sc.object_id = c.object_id
and sc.column_id = c.column_id
where s.object_id = object_id('MyTable1');


-- what is a stat look like?  run DBCC SHOW_STATISTICS
-- to get a better idea of what is stored
dbcc show_statistics('MyTable1', 'MyTable1_CI');
go

통계의 테스트 샘플은 다음과 같습니다.

여기에 이미지 설명을 입력하십시오

통계는 데이터 분포의 포함입니다. SQL Server가 최적의 계획을 결정하는 데 도움이됩니다. 이것의 좋은 예는, 당신이 무거운 물건을 살게 될 것이라고 상상해보십시오. 당신은 그것에 무게 표시가 있기 때문에 그 무게를 알고 있다면, 당신은 들어 올리는 가장 좋은 방법과 어떤 근육을 결정할 것입니다. 그것은 SQL Server가 통계로하는 일입니다.

-- create a nonclustered index
-- with the key column as my_int_col
create index IX_MyTable1_MyIntCol
on MyTable1(my_int_col);
go

-- let's look at this index
select
    object_name(object_id) as object_name,
    name as index_name,
    index_id,
    type_desc,
    is_unique,
    fill_factor
from sys.indexes
where name = 'IX_MyTable1_MyIntCol';

-- now let's see some physical aspects
-- of this particular index
-- (I retrieved index_id from the above query)
select *
from sys.dm_db_index_physical_stats
(
    db_id('TestDB'),
    object_id('MyTable1'),
    4,
    null,
    'detailed'
);

위의 예에서 인덱스에 실제로 데이터가 포함되어 있음을 알 수 있습니다 (인덱스 유형에 따라 리프 페이지가 다름).

이 게시물은 이러한 두 가지 측면의 SQL Server에 대한 매우 간단한 개요 만 보여줍니다 . 이 두 가지 모두 장과 책을 차지할 수 있습니다. 참고 문헌 중 일부를 읽으면 더 잘 이해할 수 있습니다.


1
나는 이것이 오래된 게시물이라는 것을 알고 있지만 인덱스를 만들면 (대부분의 경우) 인덱스에 대한 통계를 자동으로 생성한다는 점에 주목해야한다고 생각합니다. 통계 작성에 대해서도 마찬가지입니다.
Steve Mangiameli
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.