mydb=> create temp table t1 (t text);
CREATE TABLE
mydb=> create index t1_i on t1(t);
CREATE INDEX
mydb=> insert into t1 values('aa');
INSERT 55205187 1
mydb=> insert into t1 values('ab');
INSERT 55205188 1
mydb=> insert into t1 values('ba');
INSERT 55205189 1
mydb=> insert into t1 values('bb');
INSERT 55205190 1
mydb=> explain select * from t1 where t like 'a%';
QUERY PLAN
-----------------------------------------------------------------
Index Scan using t1_i on t1 (cost=0.00..17.07 rows=6 width=32)
Index Cond: ((t >= 'a'::text) AND (t < 'b'::text))
Filter: (t ~~ 'a%'::text)
(3 rows)
mydb=>
보시다시피 인덱스를 잘 탑니다.
mydb=> create temp table t2 (t text);
CREATE TABLE
mydb=> insert into t2 values('aa');
INSERT 55205196 1
mydb=> insert into t2 values('ab');
INSERT 55205197 1
mydb=> insert into t2 values('ba');
INSERT 55205198 1
mydb=> insert into t2 values('bb');
INSERT 55205199 1
mydb=> create index t2_i on t2(t);
CREATE INDEX
mydb=> explain select * from t2 where t like 'a%';
----------------------------------------
Seq Scan on t2 (cost=0.00..1.05 rows=1 width=32)
Filter: (t ~~ 'a%'::text)
(2 rows)
mydb=> insert into t2 values('cc');
INSERT 55205201 1
mydb=> explain select * from t2 where t like 'c%';
QUERY PLAN
---------------------------------------------------
Seq Scan on t2 (cost=0.00..1.05 rows=1 width=32)
Filter: (t ~~ 'c%'::text)
(2 rows)
mydb=>
이처럼 index 가 만들어지기 전에 어떤 데이타라도 들어가 있다면
인덱스를 만들더라도 인덱스를 타지 못합니다.
vacuum, reindex, analyze 등등을 해봐도 인덱스를 타지 않습니다.
전자야 말할 것도 없는 것이고,
후자인경우 인덱스를 타게 할려면 어떻게 해야 할까요..
t2 를 데이타만 백업 받은후 t2 테이블을 새로 만들고 다시 백업받은 t2 를 집어 넣으면 되겠지만
데이타량이 많다면..? 아..난감함이 밀려옵니다.
데이타를 백업받고 테이블 새로 만든후 리스토어 하는 방식이 아닌
현재의 상태를 두고 쿼리나 여타 명령어를 이용하여 인덱스를 탈 수 있는 방법이 필요합니다.
mydb=> create temp table t3 (i int);
CREATE TABLE
mydb=> insert into t3 values('1');
INSERT 55205204 1
mydb=> insert into t3 values('3');
INSERT 55205205 1
mydb=> insert into t3 values('4');
INSERT 55205206 1
mydb=> create INDEX t3_i on t3(i);
CREATE INDEX
mydb=> explain select * from t3 where i = 3;
QUERY PLAN
--------------------------------------------------
Seq Scan on t3 (cost=0.00..1.04 rows=1 width=4)
Filter: (i = 3)
(2 rows)
mydb=> create temp table t4 (i int);
CREATE TABLE
mydb=> create index t4_i on t4(i);
CREATE INDEX
mydb=> insert into t4 values('2');
INSERT 55205211 1
mydb=> insert into t4 values('5');
INSERT 55205212 1
mydb=> explain select * from t4 where i = 5;
QUERY PLAN
----------------------------------------------------------------
Index Scan using t4_i on t4 (cost=0.00..17.07 rows=6 width=4)
Index Cond: (i = 5)
(2 rows)
mydb=> mydb=> \di
List of relations
Schema | Name | Type | Owner | Table
-----------+------+-------+-------+-------
pg_temp_1 | t1_i | index | root | t1
pg_temp_1 | t2_i | index | root | t2
pg_temp_1 | t3_i | index | root | t3
pg_temp_1 | t4_i | index | root | t4
(4 rows)
integer일 경우도 마찬가지군요 ...
|