태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.
페이지를 읽고 있습니다. ( 아쿠아바다's Blog )
분류 전체보기 (769)
쉐어포인트 (24)
Exchange (12)
SQL (121)
XML (36)
WEB (294)
O / S (97)
삶의향기 (162)
기획 (19)
RSS 피드(IE 7.0부터 기본 지원됩니다. 이전 버전 사용자는 접합한 툴을 사용하세요!!)

심심풀이 12탄

SQL 2007/06/07 11:18 by 아쿠아바다
과연 char, varchar, int형의 컬럼으로 했을때 select시 속도가 얼마나 차이가날까 ?

그냥, 갑자기 궁금해서 테스트...
테스트의 공정성을 기하기 위해 r 이라는 별도의 랜덤 테이블을 두고서 다시 테스트
/* ------------------------------------------------------------------------- */
set statistics io off
set statistics time off

use tempdb
go
--drop table t
create table t
( c1 char(10) not null        --10자리 꽉꽉채울놈
, c2 varchar(10) not null     --10자리 꽉꽉채울놈
, c3 char(10) not null        --공백 있는놈
, c4 varchar(10) not null     --공백 있는놈
, c5 int not null)            --숫자
go
/* ------------------------------------------------------------------------- */
--100만건 : 테스트를 위해서 샘플만들구
set nocount on
declare @i int
set @i = 1
while (@i <= 1000000)
begin
   insert into t values (right('000000000'+convert(varchar(10),@i),10),
                         right('000000000'+convert(varchar(10),@i),10),
                         convert(varchar(10), @i),
                         convert(varchar(10), @i),
                         @i)
   set @i = @i + 1
end
set nocount off --소요시간 대략 2분 30초
/* ------------------------------------------------------------------------- */
--모두 동일하게 unique index 생성
create unique index idx1 on t(c1)
create unique index idx2 on t(c2)
create unique index idx3 on t(c3)
create unique index idx4 on t(c4)
create unique index idx5 on t(c5)
go --소요시간 대략 1분 30초
/* ------------------------------------------------------------------------- */
--테스트를 위해 sp를 만들어 두고
--일반적으로 범위조회도 많기는 하지만
--그래두, 인덱스를 이용한 1 row seek 방식이 대부분이겠지요
/*
drop proc p1
drop proc p2
drop proc p3
drop proc p4
drop proc p5
*/
create proc p1(@c char(10))
as
   select c1 from t where c1=@c
go
create proc p2(@c char(10))
as
   select c2 from t where c2=@c
go
create proc p3(@c char(10))
as
   select c3 from t where c3=@c
go
create proc p4(@c char(10))
as
   select c4 from t where c4=@c
go
create proc p5(@c char(10))
as
   select c5 from t where c5=@c
go
/* ------------------------------------------------------------------------- */
--drop table r
create table r(id int identity(1,1) not null primary key, no int)
go
set nocount on
declare @i int
set @i = 1
while(@i <= 10000)  --1만개의 샘플 생성
begin
   insert into r(no) values (convert(int,rand()*1000000))
   set @i = @i + 1
end
set nocount off
/* ------------------------------------------------------------------------- */
dbcc dropcleanbuffers
dbcc freeproccache
/* ------------------------------------------------------------------------- */
--동일한 sp를 10000번 반복하여 시간비교
set nocount on
declare @i int, @j int
declare @c1 varchar(10), @c2 int
set @i = 1
while (@i <= 10000)
begin
   select @j = no from r where id = @i
   --set @c1=right('000000000'+convert(varchar(10),@j),10) --p1,p2
   --set @c1=convert(varchar(10),@j)                       --p3,p4
   set @c2=@j                                            --p5
   --exec p4 @c1
   exec p5 @c2
   set @i = @i + 1
end
set nocount off
/* ------------------------------------------------------------------------- */
--sp실행시간비교(5회씩 반복 테스트) : 프로필러에서 캡춰한 내용임
     Reads   CPU   Duration
     ------ ------ --------
p1 - 50071   2468   24920
     50000   2172   11550
     50000   1969   12046
     50000   2375   11486
     50000   2062   12140

     Reads   CPU   Duration
     ------ ------ --------
p2 - 50071   2781   24983
     50000   2219   11783
     50000   2250   11780
     50000   2218   11560
     50000   2204   11576

     Reads   CPU   Duration
     ------ ------ --------
p3 - 50071   2375   22920
     50000   2250   11826
     50000   2328   11420
     50000   2219   11890
     50000   2140   11516

     Reads   CPU   Duration
     ------ ------ --------
p4 - 50071   2375   23873
     50000   2235   11876
     50000   1812   11390
     50000   2078   11923
     50000   2234   11486

     Reads   CPU   Duration
     ------ ------ --------
p5 - 50069   2094   17890
     50000   1938   10860
     50000   1765   10923
     50000   1875   11220
     50000   1797   11266
/* ------------------------------------------------------------------------- */
--범위검색 테스트를 위해 sp를 다시 만들어 두고
/*
drop proc p21
drop proc p22
drop proc p23
drop proc p24
drop proc p25
*/
create proc p21(@c1 char(10), @c2 char(10))
as
   select c1 from t where c1 between @c1 and @c2
go
create proc p22(@c1 varchar(10), @c2 varchar(10))
as
   select c2 from t where c2 between @c1 and @c2
go
create proc p23(@c1 char(10), @c2 char(10))
as
   select c3 from t where c3 between @c1 and @c2
go
create proc p24(@c1 varchar(10), @c2 varchar(10))
as
   select c4 from t where c4 between @c1 and @c2
go
create proc p25(@c1 int, @c2 int)
as
   select c5 from t where c5 between @c1 and @c2
go
/* ------------------------------------------------------------------------- */
--drop table r
create table r(id int not null identity(1,1) primary key, no1 int, no2 int)
go
set nocount on
declare @i int
set @i = 1
while(@i <= 100)
begin
   insert into r(no1, no2)
      values (convert(int,rand()*1000000), convert(int,rand()*1000))
   set @i = @i + 1
end
set nocount off
/* ------------------------------------------------------------------------- */
dbcc dropcleanbuffers
dbcc freeproccache
/* ------------------------------------------------------------------------- */
--동일한 sp를 100번 반복하여 시간비교
set nocount on
declare @i int, @j int, @k int
declare @c1 varchar(10), @c2 varchar(10)
declare @c3 int, @c4 int
set @i = 1
while (@i <= 100)
begin
   select @j=no1, @k=no1+no2 from r where id = @i
   --set @c1=right('000000000'+convert(varchar(10),@j),10) --p1,p2
   --set @c2=right('000000000'+convert(varchar(10),@k),10) --p1,p2
   --set @c1=convert(varchar(10),@j)                       --p3,p4
   --set @c2=convert(varchar(10),@k)                       --p3,p4
   set @c3=@j                                            --p5
   set @c4=@k                                            --p5
   --exec p24 @c1, @c2
   exec p25 @c3, @c4
   set @i = @i + 1
end
set nocount off
/* ------------------------------------------------------------------------- */
--sp실행시간비교(5회씩 반복 테스트) : 프로필러에서 캡춰한 내용임
     Reads   CPU   Duration
     ------  ----  --------
p21 - 806    157   11970
      718    78    11313
      718    94    11796
      718    31    11580
      718    47    11173

     Reads   CPU   Duration
     ------  ----  --------
p22 - 833    171   11890
      745    62    11766
      745    78    11313
      745    63    11813
      745    141   11436

     Reads   CPU   Duration
     ------  ----  --------
p23 - 933    188   20533
      845    109   20183
      845    156   20423
      845    125   20606
      845    157   21013

     Reads   CPU   Duration
     ------  ----  --------
p24 - 924    125   19970
      836    140   20030
      836    218   20220
      836    79    19890
      836    109   19813

     Reads   CPU   Duration
     ------  ----  --------
p25 - 743    78    11626
      659    32    11376
      659    62    11453
      659    63    12530
      659    62    11420
/* ------------------------------------------------------------------------- */
--이번에는 full index scan 속도를 비교해보자. : 프로필러에서 캡춰한 내용임
--시간이 너무많이 걸려서 1회씩만 테스트
dbcc dropcleanbuffers
dbcc freeproccache

select c1 from t
     Reads   CPU   Duration
     ------ ------ --------
     2997    984   223813

select c2 from t
     Reads   CPU   Duration
     ------ ------ --------
     3656    875   213906

select c3 from t
     Reads   CPU   Duration
     ------ ------ --------
     2994    875   211093

select c4 from t
     Reads   CPU   Duration
     ------ ------ --------
     2977    797   212706

select c5 from t
     Reads   CPU   Duration
     ------ ------ --------
     2087    641   214360

/* ------------------------------------------------------------------------- */
--마지막으로 물리적인 page size를 비교해보자.
set statistics io on

select c1 from t : 논리적 읽기 수 2600
select c2 from t : 논리적 읽기 수 3098
select c3 from t : 논리적 읽기 수 2600
select c4 from t : 논리적 읽기 수 2586
select c5 from t : 논리적 읽기 수 1858
/* ------------------------------------------------------------------------- */
역시나 int형이 모든경우에 효율이 가장좋네요.

그렇지만, 그동안 varchar보다는 char가 빠를것이다는 상식이 깨지는 순간이네요...

약간의 오차를 인정한다면 실제 Durarion만을 놓고 본다면 거의 차이가 없네요.
다만, 물리적인 page수에서는 고정길이의 경우 역시나 varchar보다는 char가 조금 적네요.
/* ------------------------------------------------------------------------- */
무엇을 사용할지는 개발자 맘이겠지요...................                @_@;
/* ------------------------------------------------------------------------- */
하지만 테스트 방법이 그다지 좋은것이 아닌것 같아 실무에서는 어떻게 달라질지 ?

혹시나, 이것과 관련된 테스트내용을 가지고 계시다면 이곳에 공개해주시면 고맙겠습니다.

또는 다른의견 이나 오타등이 있다면 리플달아주세요.

음, 근데 회차가 거듭될수록 심심풀이의 조회수가 엄청시리 떨어지는군요... ㅎㅎㅎ
역시 내용이 너무 부실해서 식상하신듯...
음. 먼가 쌈박한걸 하나 풀어봐야할텐데...  고민고민^^^^^^

'SQL' 카테고리의 다른 글

심심풀이 13탄(2)  (0) 2007/06/07
심심풀이 10탄  (0) 2007/06/07
심심풀이 12탄  (0) 2007/06/07
cross join 이용한 그룹 집계  (0) 2007/06/07
락 용어정리  (0) 2007/06/07
sp를 WITH ENCRYPTION 암호화 한거 풀기  (0) 2007/06/07
좀더 흥미로운 내용이 많이 있습니다.. HOME > SQL를 확인하세요
TAG ,   
0 Trackback, 0 Comment, :
1  ... 615 616 617 618 619 620 621 622 623  ... 769 
Statistics Graph
Total : 557,403 Today : 33