7. Index
인덱스는 지정된 컬럼에 대한 매핑 정보를 가지고 있습니다.
Ex) CREATE INDEX test1_id_index ON test1 (id);
8. Index
PostgreSQL에서는 다음과 같은 Index type을 지원합니다.
• B-Tree : numbers, text, dates, etc..
• Generalized Inverted Index (GIN)
• Generalized Inverted Search Tree (GiST)
• Space partitioned GiST (SP-GiST)
• Block Range Indexes (BRIN)
• Hash
20. GIN
1. Text retrival
postgres=# -- create a table with a text column
postgres=# CREATE TABLE t1 (id serial, t text);
CREATE TABLE
postgres=# CREATE INDEX t1_idx ON t1 USING gin (to_tsvector('english', t));
CREATE INDEX
postgres=# INSERT INTO t1 VALUES (1, 'a fat cat sat on a mat and ate a fat rat');
INSERT 0 1
postgres=# INSERT INTO t1 VALUES (2, 'a fat dog sat on a mat and ate a fat chop');
INSERT 0 1
postgres=# -- is there a row where column t contains the two words? (syntax contains some magic
to hit index)
postgres=# SELECT * FROM t1 WHERE to_tsvector('english', t) @@ to_tsquery('fat & rat');
id | t
----+------------------------------------------
1 | a fat cat sat on a mat and ate a fat rat
(1 row)
postgres=# CREATE INDEX indexname ON tablename USING GIN (columnname);
21. GIN
2. Array
postgres=# -- create a table where one column exists of an integer array
postgres=# --
postgres=# CREATE TABLE t2 (id serial, temperatures INTEGER[]);
CREATE TABLE
postgres=# CREATE INDEX t2_idx ON t2 USING gin (temperatures);
CREATE INDEX
postgres=# INSERT INTO t2 VALUES (1, '{11, 12, 13, 14}');
INSERT 0 1
postgres=# INSERT INTO t2 VALUES (2, '{21, 22, 23, 24}');
INSERT 0 1
postgres=# -- Is there a row with the two array elements 12 and 11?
postgres=# SELECT * FROM t2 WHERE temperatures @> '{12, 11}';
id | temperatures
----+---------------
1 | {11,12,13,14}
(1 row)
22. GiST
• “contains”, “left of”, “overlaps”, 등을 지원한다.
• Full Text Search, Geometric operations (PostGIS, etc. ), Handling ranges (tiem, etc.)
• KNN-search, BRTree를 바탕으로 구성되어 있다.
27. GiST
postgres=# CREATE INDEX indexname ON tablename USING GIST
(columnname);
postgres=# -- create a table with a column of non-trivial type
postgres=# --
postgres=# CREATE TABLE t3 (id serial, c circle);
CREATE TABLE
postgres=# CREATE INDEX t3_idx ON t3 USING gist(c);
CREATE INDEX
postgres=# INSERT INTO t3 VALUES (1, circle '((0, 0), 0.5)');
INSERT 0 1
postgres=# INSERT INTO t3 VALUES (2, circle '((1, 0), 0.5)');
INSERT 0 1
postgres=# INSERT INTO t3 VALUES (3, circle '((0.3, 0.3), 0.3)');
INSERT 0 1
postgres=# -- which circles lie in the bounds of the unit circle?
postgres=# SELECT * FROM t3 WHERE circle '((0, 0), 1)' @> c;
id | c
----+-----------------
1 | <(0,0),0.5>
3 | <(0.3,0.3),0.3>
(2 rows)
31. summary
• B-tree is ideal for unique values
• GIN is ideal for indexes with many duplicates
• GIST for everything else
Experiments lead to the following observations:
creation time - GIN takes 3x time to build than GiST
size of index - GIN is 2-3 times bigger than GiST
search time - GIN is 3 times faster than GiST
update time - GIN is about 10 times slower than GiST