Background Image
조회 수 28483 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

테스트 버전: CUBRID 2008 R3.0 (8.3.0.0337)

 

요약
CUBRID에서는 Primary key를 생성하면 asc 순으로 생성됩니다. 따라서 desc로 order by를 하게 되면 index를 타지 못하게 되죠. Primary key 생성 시 desc로 별도로 생성하지는 못하고요, primary key를 desc 순으로 생성하려면, unique index를 desc로 생성하여야 합니다.

이렇게 하면 order by desc 시 추가된 unique index 를 사용할 수 있게 됩니다.

 

테스트

먼저 테이블을 생성하고 데이터를 넣습니다.

 

csql> create table member_tbl(id int primary key, name varchar(10));
csql> insert into member_tbl values (1, 'marry'), (2, 'john'), (3,'billy'), (4, 'willy');
csql> ;x
csql> ;plan simple
csql> select * from member_tbl order by id for orderby_num() between 1 and 2;
Query plan:
 Index scan(member_tbl member_tbl, pk_member_tbl_id, member_tbl.id range (-2147483648 ge_inf max))
           id  name
===================================
            1  'marry'
            2  'john'

 

여기서 desc로 조회한다면 (최신 회원가입 순으로 조회가 대부분) 다음과 같이 되는데, 인덱스를 타지 않고 순차 검색(sequential scan)을 하는 것을 확인할 수 있습니다.

 

csql> select * from member_tbl order by id desc for orderby_num() between 1 and 2;
Query plan:
 Sort(order by)
    Sequential scan(member_tbl member_tbl)
           id  name
===================================
            4  'willy'
            3  'billy'

 


이제 원하는 것은, desc로 unique key를 생성하여 desc 로 조회할 때도 인덱스를 타게 하는 것입니다.
primary key를 desc로 바로 생성하지는 않으므로, unique 인덱스를 추가로 생성합니다.

 

 

csql> create unique index idx_u_member_tbl_id on member_tbl(id desc);

 

이제 desc로 질의 수행 시 idx_u_member_tbl_id 인덱스를 타는 것을 확인할 수 있습니다.

 

csql> select * from member_tbl order by id desc for orderby_num() between 1 and 2;
Query plan:
 Index scan(member_tbl member_tbl, idx_u_member_tbl_id, member_tbl.id range (-2147483648 ge_inf max))
           id  name
===================================
            4  'willy'
            3  'billy'

TAG •

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
106 기타 데이터베이스 생성하기 file 남재우 2011.03.07 29106
105 Linux 복제 따라하기 admin 2008.11.21 29096
104 Java QuantumDB Eclipse Plugin을 사용하여 CUBRID연동하기 2 cubebridge 2009.10.06 28922
» 튜닝 [질의튜닝]order by desc가 인덱스 타게 하려면 admin 2011.07.14 28483
102 Linux CUBRID DB와 Broker 분리방안 정만영 2010.04.28 28412
101 기타 스칼라 서브쿼리 사용 가이드 웁쓰 2009.12.31 28275
100 기타 조인 방법(Join Method) 설명 file 웁쓰 2009.12.31 27995
99 Windows CUBRID2008R2.0 Windows BACKUP 자동화 설정하기 정만영 2009.08.17 27942
98 Install CUBRID R2.0 ISV(Independent Software Vendor)설치방법 file cubebridge 2009.11.20 27922
97 Linux CUBRID Tutorial (Unix/Linux) 정만영 2009.08.17 27887
96 Java JavaSP SampleCode(Pivot기능) cubebridge 2009.07.28 27554
95 Windows 윈도우즈 환경에서 배치화일을 이용하여 백업 또는 데이터베이스 정리 수행하기 남재우 2010.03.03 27487
94 Java CUBRID JDBC에서 유니코드 사용하기 1 손승일 2009.08.15 27435
93 Install CUBRID2008 R2.1 업그레이드시 serial 관련 문제 해결 방법 남재우 2010.01.28 27348
92 Windows CUBRID2008 R2.0 Tutorial (Windows) cubebridge 2009.08.17 27340
91 Windows eclipse에서 python 및 CUBRID broker_log_top 사용하기 cubebridge 2009.07.28 27272
90 Linux CUBRID2008R1.x to 2.0 마이그레이션 가이드 (32bit 기준) 정만영 2009.08.17 27205
89 ODBC/OLEDB VB에서 CUBRID ODBC/OLEDB 사용하는 방법 및 샘플코드. file seongjoon 2012.06.30 26927
88 Linux 리소스를 제한(limits.conf) 하여 DB서버를 관리하자 1 주현 2015.12.31 26635
87 기타 csql(SQL 실행기) 사용법 1 admin 2008.11.21 26557
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

Contact Cubrid

대표전화 070-4077-2110 / 기술문의 070-4077-2113 / 영업문의 070-4077-2112 / Email. contact_at_cubrid.com
Contact Sales