【Data Platform】/SQL Server

[2k5] 파티션 인덱스(Partitioned Index)

디비랑 2008. 9. 7. 15:34
  1. /**********************************************************************************************
    -- Title : [2k5] 파티션 인덱스(Partitioned Index)
    -- Reference : hanbitbook.co.kr
    -- Key word : 파티션 인덱스 partitioned index
    **********************************************************************************************/
    --분할 인덱스는 따로 설정할 내용이 없고, 분할 테이블에 인덱스를 생성하게 되면
    --자동으로 각 파일그룹별로 인덱스가 분할된다.

    use master;
    go

    if exists(select * from master.dbo.sysdatabases where name = 'parttbldb')
       drop database parttbldb;
    go

    -- db 생성
    create database parttbldb
    on primary
      ( name = parttbldb,  -- 75년이전출생자(75년포함)
        filename = N'd:\parttbldb.mdf'
      ),
      filegroup year_76_80 -- 76~80년출생자
      ( name = parttbldb2,
        filename = N'd:\parttbldb2.ndf'
      ),
      filegroup year_81    -- 81년이후출생자(81년포함)
      ( name = parttbldb3,
      filename = N'd:\parttbldb3.ndf'
      )
      log on
      ( name = parttbldb_log, -- 로그파일
        filename = N'd:\parttbldb_log.ldf'
      );
    go

    use parttbldb;
    go

    -- 파티션 함수 생성
    create partition function birtyyearrangepf (int)
    as range left for values (1975, 1981);
    go

    -- 파티션 스키마 생성
    create partition scheme birtyyearrangeps
    as partition birtyyearrangepf
    to ([primary], year_76_80, year_81);
    go

    -- 파티션된 테이블 생성
    create table usertbl
    ( userid  nchar(8) not null ,
      name    nvarchar(10) not null,
      birthyear   int not null,
      addr   nchar(4) not null,
      mobile1 nchar(3),
      mobile2   nchar(8),
      height    smallint
    ) on birtyyearrangeps (birthyear);
    go
     
    -- 인덱스 생성 : 그냥 이케 생성한다는거 보여주는겨~
    /*
    alter table usertbl
    add constraint pk_usertbl primary key (userid, birthyear) on birtyyearrangeps (birthyear);
    go
     
    create index ix_usertbl on usertbl (birthyear) on birtyyearrangeps (birthyear);
    go
    */

    insert into usertbl
    select N'ajh', N'안정환', 1979, N'강원', null, null, 182 union all
    select N'cjc', N'최진철', 1975, N'제주', N'019', N'0000000 ', 185 union all
    select N'jjj', N'조재진', 1986, N'충북', N'019', N'3333333 ', 179 union all
    select N'kni', N'김남일', 1977, N'경북', N'016', N'6666666 ', 183 union all
    select N'lcs', N'이천수', 1983, N'인천', N'011', N'4444444 ', 179 union all
    select N'lyp', N'이영표', 1983, N'전북', null, null, 178 union all
    select N'pjs', N'박지성', 1983, N'서울', N'011', N'1111111 ', 181 union all
    select N'pjy', N'박주영', 1986, N'경기', N'011', N'2222222 ', 178 union all
    select N'sjk', N'송종국', 1979, N'경기', N'018', N'9999999 ', 178 union all
    select N'skh', N'설기현', 1978, N'서울', N'011', N'8888888 ', 182;
    go
     
    -- 일반 테이블 생성
    create table usertbl_nopart
    ( userid  nchar(8) not null ,
      name    nvarchar(10) not null,
      birthyear   int not null,
      addr   nchar(4) not null,
      mobile1 nchar(3),
      mobile2   nchar(8),
      height    smallint
    );
    go
     
    insert into usertbl_nopart
    select N'ajh', N'안정환', 1979, N'강원', null, null, 182 union all
    select N'cjc', N'최진철', 1975, N'제주', N'019', N'0000000 ', 185 union all
    select N'jjj', N'조재진', 1986, N'충북', N'019', N'3333333 ', 179 union all
    select N'kni', N'김남일', 1977, N'경북', N'016', N'6666666 ', 183 union all
    select N'lcs', N'이천수', 1983, N'인천', N'011', N'4444444 ', 179 union all
    select N'lyp', N'이영표', 1983, N'전북', null, null, 178 union all
    select N'pjs', N'박지성', 1983, N'서울', N'011', N'1111111 ', 181 union all
    select N'pjy', N'박주영', 1986, N'경기', N'011', N'2222222 ', 178 union all
    select N'sjk', N'송종국', 1979, N'경기', N'018', N'9999999 ', 178 union all
    select N'skh', N'설기현', 1978, N'서울', N'011', N'8888888 ', 182;
    go
     
    -- io 비교 확인
    set statistics io on;
     
    select * from usertbl;
    select * from usertbl_nopart;
    go
    /*
    (10개 행 적용됨)
    테이블 'usertbl'. 검색 수 3, 논리적 읽기 수 3, 물리적 읽기 수 0, 미리 읽기 수 0,
    lob 논리적 읽기 수 0, lob 물리적 읽기 수 0, lob 미리 읽기 수 0.
    (10개 행 적용됨)
    테이블 'usertbl_nopart'. 검색 수 1, 논리적 읽기 수 1, 물리적 읽기 수 0, 미리 읽기 수 0,
    lob 논리적 읽기 수 0, lob 물리적 읽기 수 0, lob 미리 읽기 수 0.
    */