Friday, August 21, 2015

similarity() and trigram indexes

Continuing our adventures with watchlists...

Lets say we create a trigram index on our watchlist table:

CREATE INDEX watchlist_entry_gin_trigram_index
  ON watch_list_entry
  USING gin
  (entry COLLATE pg_catalog."default" gin_trgm_ops);

And now we want to use it.  This query will NOT use the index:

select *
from watch_list_entry
where similarity(entry, 'Tony Blair') > 0.5;

(221 secs)

But this pair of queries (which is functionally the same) WILL use the index:

select set_limit(0.5);

select *
from watch_list_entry
where similarity(entry, 'Tony Blair') > 0.5;

(34 secs)


No comments: