Created
March 22, 2016 08:33
-
-
Save ejrh/6e9c74bd65220fe656f8 to your computer and use it in GitHub Desktop.
Btree vs trigram index for short prefixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filesys=# \d file | |
Table "public.file" | |
Column | Type | Modifiers | |
----------+-----------------------------+--------------------------------------------------- | |
id | integer | not null default nextval('file_id_seq'::regclass) | |
name | character varying | not null | |
size | bigint | not null | |
modified | timestamp without time zone | | |
md5 | character(32) | | |
Indexes: | |
"file_ix_btree_name" btree (name) | |
"file_ix_name" gist (name gist_trgm_ops) INVALID | |
Using the btree index (straight after creating the index): | |
filesys=# explain (analyze, buffers) select * from file where name like '/%'; | |
QUERY PLAN | |
------------------------------------------------------------------------------------------------------------------------ | |
--------- | |
Bitmap Heap Scan on file (cost=546.39..37279.47 rows=404 width=72) (actual time=0.021..0.021 rows=0 loops=1) | |
Filter: ((name)::text ~~ '/%'::text) | |
Buffers: shared hit=3 read=1 | |
-> Bitmap Index Scan on file_ix_btree_name (cost=0.00..546.29 rows=19923 width=0) (actual time=0.018..0.018 rows=0 | |
loops=1) | |
Index Cond: (((name)::text >= '/'::text) AND ((name)::text < '0'::text)) | |
Buffers: shared hit=3 read=1 | |
Planning time: 0.543 ms | |
Execution time: 0.039 ms | |
(8 rows) | |
Using the trigram index (straight after reindexing): | |
filesys=# explain (analyze, buffers) select * from file where name like '/%'; | |
QUERY PLAN | |
------------------------------------------------------------------------------------------------------------------------ | |
------------ | |
Bitmap Heap Scan on file (cost=24.55..1356.08 rows=404 width=72) (actual time=2371.035..2371.035 rows=0 loops=1) | |
Recheck Cond: ((name)::text ~~ '/%'::text) | |
Rows Removed by Index Recheck: 4204725 | |
Heap Blocks: exact=57608 | |
Buffers: shared hit=27869 read=90491 written=883 | |
-> Bitmap Index Scan on file_ix_name (cost=0.00..24.45 rows=404 width=0) (actual time=1198.344..1198.344 rows=42047 | |
25 loops=1) | |
Index Cond: ((name)::text ~~ '/%'::text) | |
Buffers: shared hit=27868 read=32884 | |
Planning time: 0.317 ms | |
Execution time: 2371.490 ms | |
(10 rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment