by 불혹 [DB 모델링/설계] postgresql plan casting [2021.06.01 14:44:46]
테이블 생성 데이터 타입(varchar, text)에 대한 고민입니다.
아래 2가지 테스트를 진행했습니다.
1. varchar 컬럼 생성,
2. text 컬럼 생성
각각 인덱스를 생성하고 plan 확인 내용입니다.
아래 결과만 보았을 때... varchar 컬럼 인덱스 scan시 ::text로 cast하는것으로 보여지는데....
==> 테이블 생성 시 컬럼을 varchar보다 text로 선언해야 할까요?
환경 : PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
-----1. varchar 컬럼 생성 create table xx1 ( col varchar(10) ) ; -- 인덱스 생성 create index xx1_ix01 on xx1 (col) ; -- 데이터 추가 insert into xx1(col) values ('a') ; insert into xx1(col) values ('b') ; insert into xx1(col) values ('c') ; insert into xx1(col) values ('d') ; -- 데이터 조회 select * from xx1 where col = 'a' ; -- plan Node Type Entity Cost Rows Time Condition Bitmap Index Scan xx1_ix01 0.00 - 4.20 6 [NULL] ((col)::text = 'a'::text) => 실행 계획의 "((col)::text = 'a'::text)" 부분은 col에 대한 text casting을 하는 건지 궁금합니다.
--2. text 컬럼 생성 create table xx2 ( col text ) ; --인덱스 생성 create index xx2_ix01 on xx2 (col) ; -- 데이터 추가 insert into xx2(col) values ('a') ; insert into xx2(col) values ('b') ; insert into xx2(col) values ('c') ; insert into xx2(col) values ('d') ; -- 데이터 조회 select * from xx2 where col = 'a' ; -- plan Node Type Entity Cost Rows Time Condition Bitmap Index Scan xx2_ix01 0.00 - 4.21 7 [NULL] (col = 'a'::text) => text 컬럼의 경우 "(col = 'a'::text)" casting 없는 plan을 보여집니다.
PostgreSQL은 VARCHAR(무제한) 와 TEXT 타입이 둘다 성능이 똑같다고 합니다.
( 원문내용 : https://www.postgresqltutorial.com/postgresql-char-varchar-text/
The performance of the VARCHAR (without the size n) and TEXT are the same. )
TEXT 타입으로 설정할 수도 있겠지만, 개인적으로는 크기를 좀 여유있게 잡더라도 VARCHAR(제한) 으로 설정하시는게 나을꺼 같아요.