intarray는 PostgreSQL 소스트리의 contrib 디렉토리 내에 있다. 예의상이라도 README파일을 읽어준다.
PostgreSQL은 이미 설치되어 있어야 한다.
# make
# make install
# psql DBNAME < _int.sql
끝이다 -_-;;;; 이제 저 좋은 함수들과 연산자를 사용 할 수 있게 되었다. int형에 대해서지만..
GiST 인덱스 사용해보기
nonun=# CREATE TABLE test(
nonun(# a int,
nonun(# b int[]
nonun(# );
CREATE TABLE
nonun=# CREATE INDEX index_test_gist ON test using gist (b);
CREATE INDEX
nonun=# COPY test from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1 {1,2,3}
>> 2 {8,6,3}
>> 3 {5,6,1}
>> 4 {9,11,13}
>> 5 {7,3,4}
>> 6 {1,6,1,7,9}
>> 7 {1,7,8,8,6,1}
>> \.
nonun=# SELECT * from test ;
a | b
---+---------------
1 | {1,2,3}
2 | {8,6,3}
3 | {5,6,1}
4 | {9,11,13}
5 | {7,3,4}
6 | {1,6,1,7,9}
7 | {1,7,8,8,6,1}
(7 rows)
nonun=# SELECT * from test where b @@ '3';
a | b
---+---------
1 | {1,2,3}
2 | {8,6,3}
5 | {7,3,4}
(3 rows)
nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.074..0.089 rows=3 loops=1)
Index Cond: (b @@ '3'::query_int)
Total runtime: 0.569 ms
(3 rows)
nonun=# SELECT * from test where b @@ '3|2';
a | b
---+---------
1 | {1,2,3}
2 | {8,6,3}
5 | {7,3,4}
(3 rows)
nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3|2';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.181..0.198 rows=3 loops=1)
Index Cond: (b @@ '3 | 2'::query_int)
Total runtime: 0.675 ms
(3 rows)
nonun=# SELECT * from test where b @@ '3|1';
a | b
---+---------------
1 | {1,2,3}
2 | {8,6,3}
3 | {5,6,1}
5 | {7,3,4}
6 | {1,6,1,7,9}
7 | {1,7,8,8,6,1}
(6 rows)
nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3|1';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.079..0.102 rows=6 loops=1)
Index Cond: (b @@ '3 | 1'::query_int)
Total runtime: 0.612 ms
(3 rows)
nonun=# SELECT * from test where 3 = any(b);
a | b
---+---------
1 | {1,2,3}
2 | {8,6,3}
5 | {7,3,4}
(3 rows)
nonun=# EXPLAIN ANALYZE SELECT * from test where 3 = any(b);
QUERY PLAN
--------------------------------------------------------------------------------------------------
Seq Scan on test (cost=0.00..45.00 rows=500 width=36) (actual time=0.049..0.068 rows=3 loops=1)
Filter: (3 = ANY (b))
Total runtime: 0.182 ms
(3 rows)
보시라... 인덱스 아주 잘 타고 계시다! (지금 시퀀셜 스캔이 시간이 덜 걸리는 이유는 데이터가 적기 때문이다. 데이터가 적은 상황에서는 인덱스를 사용하는 것이 비용이 더 들 때도 있다.)
Contents
GiST 인덱스
intarray?
함수
icount
test=# select icount('{1,2,3}'::int[]); icount -------- 3 (1 row)sort
test=# select sort('{1,2,3}'::int[],'desc'); sort --------- {3,2,1} (1 row)uniq
test=# select uniq(sort('{1,2,3,2,1}'::int[])); uniq --------- {1,2,3} (1 row)idx
test=# select idx('{1,2,3,2,1}'::int[],2); idx ----- 2 (1 row)subarray
test=# select subarray('{1,2,3,2,1}'::int[],2,3); subarray ---------- {2,3,2} (1 row)intarray_push_elem
intarray_push_array
intarray_del_elem
intset_union_elem
intset_subtract
연산자
intarray 설치
GiST 인덱스 사용해보기
nonun=# CREATE TABLE test( nonun(# a int, nonun(# b int[] nonun(# ); CREATE TABLE nonun=# CREATE INDEX index_test_gist ON test using gist (b); CREATE INDEX nonun=# COPY test from stdin; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> 1 {1,2,3} >> 2 {8,6,3} >> 3 {5,6,1} >> 4 {9,11,13} >> 5 {7,3,4} >> 6 {1,6,1,7,9} >> 7 {1,7,8,8,6,1} >> \. nonun=# SELECT * from test ; a | b ---+--------------- 1 | {1,2,3} 2 | {8,6,3} 3 | {5,6,1} 4 | {9,11,13} 5 | {7,3,4} 6 | {1,6,1,7,9} 7 | {1,7,8,8,6,1} (7 rows) nonun=# SELECT * from test where b @@ '3'; a | b ---+--------- 1 | {1,2,3} 2 | {8,6,3} 5 | {7,3,4} (3 rows) nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.074..0.089 rows=3 loops=1) Index Cond: (b @@ '3'::query_int) Total runtime: 0.569 ms (3 rows) nonun=# SELECT * from test where b @@ '3|2'; a | b ---+--------- 1 | {1,2,3} 2 | {8,6,3} 5 | {7,3,4} (3 rows) nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3|2'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.181..0.198 rows=3 loops=1) Index Cond: (b @@ '3 | 2'::query_int) Total runtime: 0.675 ms (3 rows) nonun=# SELECT * from test where b @@ '3|1'; a | b ---+--------------- 1 | {1,2,3} 2 | {8,6,3} 3 | {5,6,1} 5 | {7,3,4} 6 | {1,6,1,7,9} 7 | {1,7,8,8,6,1} (6 rows) nonun=# EXPLAIN ANALYZE SELECT * from test where b @@ '3|1'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Scan using index_test_gist on test (cost=0.00..4.82 rows=1 width=36) (actual time=0.079..0.102 rows=6 loops=1) Index Cond: (b @@ '3 | 1'::query_int) Total runtime: 0.612 ms (3 rows) nonun=# SELECT * from test where 3 = any(b); a | b ---+--------- 1 | {1,2,3} 2 | {8,6,3} 5 | {7,3,4} (3 rows) nonun=# EXPLAIN ANALYZE SELECT * from test where 3 = any(b); QUERY PLAN -------------------------------------------------------------------------------------------------- Seq Scan on test (cost=0.00..45.00 rows=500 width=36) (actual time=0.049..0.068 rows=3 loops=1) Filter: (3 = ANY (b)) Total runtime: 0.182 ms (3 rows)Recent Posts
Archive Posts
Tags