database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 9288 게시물 읽기
No. 9288
index컬럼 type casting
작성자
김민석
작성일
2013-06-04 17:59ⓒ
2013-06-04 18:41ⓜ
조회수
8,829

Table "public.table"

Column | Type | Modifiers

----------+-----------------------+-----------------------------------------

col1 | character(3) | not null

col2 | character varying(20) | not null

col3 | character varying(20) |

col4 | character(1) | default 0

Indexes:

"table_pkey" PRIMARY KEY, btree (col1, col2)

 

 

postgres=# explain analyze select * from table where col1=right('10000081',3);

QUERY PLAN

----------------------------------------------------------------------------------------------------------------------

Seq Scan on table (cost=0.00..31053.24 rows=5650 width=286) (actual time=3.221..429.950 rows=110008 loops=1)

Filter: ((col1)::text = '081'::text)

Total runtime: 435.904 ms

(3 rows)

 

postgres=# explain analyze select * from table where col1=right('10000081',3)::char(3);

QUERY PLAN

-----------------------------------------------------------------------------------------------------------------------------------------

Bitmap Heap Scan on table (cost=3097.81..18602.98 rows=112173 width=286) (actual time=18.125..32.707 rows=110008 loops=1)

Recheck Cond: (col1 = '081'::character(3))

-> Bitmap Index Scan on table_pkey (cost=0.00..3069.77 rows=112173 width=0) (actual time=17.846..17.846 rows=110008 loops=1)

Index Cond: (col1 = '081'::character(3))

Total runtime: 38.640 ms

(5 rows)

 

성능때문에 살펴보던중에 두번째 쿼리처럼 함수리턴 타입을 인덱스 컬럼 타입으로 캐스팅 하니까 full scan 이 index scan 으로 교정되는데요...

결국, string 함수는 대부분 text type으로 리턴하는 함수엔 죄다 캐스팅을 해줘야 되는건지요...???

이 글에 대한 댓글이 총 1건 있습니다.

 이런 경우에, 함수기반 인덱스를 만듭니다. 

create index table_col1_char_i on table (mychar(col1));

mychar 함수의 반환값이 char(3)이면 되겠죠.

 

잘 풀어보시길 바랍니다.

 

김상기(ioseph)님이 2013-06-05 10:35에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
9291postgresql-9.2.3-2-windows 설치시 에러 [3]
작은종
2013-06-07
10366
9290다중 테이블 내용으로 업데이트를 할 경우! [3]
souler
2013-06-06
9094
9289제약조건 검사와 입력 실패시 리턴되는게 있을까요? [1]
VQ
2013-06-05
9970
9288index컬럼 type casting [1]
김민석
2013-06-04
8829
9286bytea를 PQescapeByteaConn함수 사용한 예제 없나요? [1]
심상호
2013-05-31
10493
9285완전초보의 pg_connect 질문 [2]
정성모
2013-05-29
8997
9284불완전한 테이블 제약조건에 대해서 [8]
souler
2013-05-27
9124
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.020초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다