Creates a GiST (Generalized Search Tree)-based index. The column can be of tsvector or tsquery type.
CREATE INDEX name ON table USING gin(column);Creates a GIN (Generalized Inverted Index)-based index. The column must be of tsvector type.
postgres=# create extension pg_trgm;
CREATE EXTENSION
postgres=# \dx+ pg_trgm
Objects in extension "pg_trgm"
Object Description
--------------------------------------------------------------------------------------------------
function gin_extract_query_trgm(text,internal,smallint,internal,internal,internal,internal)
function gin_extract_value_trgm(text,internal)
function gin_trgm_consistent(internal,smallint,text,integer,internal,internal,internal,internal)
function gtrgm_compress(internal)
function gtrgm_consistent(internal,text,integer,oid,internal)
function gtrgm_decompress(internal)
function gtrgm_distance(internal,text,integer,oid)
function gtrgm_in(cstring)
function gtrgm_out(gtrgm)
function gtrgm_penalty(internal,internal,internal)
function gtrgm_picksplit(internal,internal)
function gtrgm_same(gtrgm,gtrgm,internal)
function gtrgm_union(bytea,internal)
function set_limit(real)
function show_limit()
function show_trgm(text)
function similarity(text,text)
function similarity_dist(text,text)
function similarity_op(text,text)
operator %(text,text)
operator <->(text,text)
operator class gin_trgm_ops for access method gin
operator class gist_trgm_ops for access method gist
operator family gin_trgm_ops for access method gin
operator family gist_trgm_ops for access method gist
type gtrgm
(26 rows)
postgres=# select similarity('entries','entry');
similarity
------------
0.4
postgres=# select show_limit();
show_limit
------------
0.3
postgres=# select set_limit(0.3);
set_limit
-----------
0.3
postgres=# select similarity_op('entries','ries');
similarity_op
---------------
t
(1 row)
postgres=# select similarity_op('entries','ri');
similarity_op
---------------
f
postgres=# select 'entries' % 'ries';
?column?
----------
t
postgres=# select 'entries' % 'ri';
?column?
----------
f
postgres=# select similarity('entries' ,'entri');
similarity
------------
0.555556
postgres=# select 'entries' <-> 'entri';
?column?
----------
0.444444
(1 row)
postgres=# create table test_pg_trgm (id serial primary key,info text);
CREATE TABLE
postgres=# insert into test_pg_trgm (info) select generate_series(1,1000000)||'digoal';
LOG: checkpoints are occurring too frequently (7 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
LOG: checkpoints are occurring too frequently (5 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
INSERT 0 1000000
postgres=# create index idx_test_pg_trgm_1 on test_pg_trgm using gist (info gist_trgm_ops);
LOG: checkpoints are occurring too frequently (6 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
LOG: checkpoints are occurring too frequently (8 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
LOG: checkpoints are occurring too frequently (8 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
CREATE INDEX
Time: 35253.859 ms
建立gin索引:
postgres=# create index idx_test_pg_trgm_1 on test_pg_trgm using gin (info gin_trgm_ops);
LOG: checkpoints are occurring too frequently (3 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
LOG: checkpoints are occurring too frequently (4 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
LOG: checkpoints are occurring too frequently (3 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
CREATE INDEX
Time: 11286.458 ms
postgres=# select count(*) from test_pg_trgm where info ilike '%123444%al%';
count
-------
1
(1 row)
Time: 373.475 ms
走gist索引:
postgres=# select count(*) from test_pg_trgm where info ilike '%123444%al%';
count
-------
1
(1 row)
Time: 16.443 ms
走gin索引:
postgres=# select count(*) from test_pg_trgm where info ilike '%123444%al%';
count
-------
1
(1 row)
Time: 3.065 ms
postgres=# select count(*) from test_pg_trgm where info % '%123%oal%';
count
-------
1132
(1 row)
Time: 1532.612 ms
走gist索引:
postgres=# select count(*) from test_pg_trgm where info % '%123%oal%';
count
-------
1132
(1 row)
Time: 166.685 ms
postgres=# select count(*) from test_pg_trgm where info % '%123%oal%';
count
-------
1132
(1 row)
Time: 2478.780 ms
postgres=# select * from test_pg_trgm order by info <-> '1234digoal' limit 1;
id | info
------+------------
1234 | 1234digoal
(1 row)
Time: 1759.215 ms
走gist索引:
postgres=# select * from test_pg_trgm order by info <-> '1234digoal' limit 1;
id | info
------+------------
1234 | 1234digoal
(1 row)
Time: 126.471 ms
走gin索引:
postgres=# select * from test_pg_trgm order by info <-> '1234digoal' limit 1;
id | info
------+------------
1234 | 1234digoal
(1 row)
Time: 1690.692 ms