Skip to content

Instantly share code, notes, and snippets.

@misablaha
Created May 27, 2014 06:27
Show Gist options
  • Save misablaha/cc1aaa7e764c46f02d10 to your computer and use it in GitHub Desktop.
Save misablaha/cc1aaa7e764c46f02d10 to your computer and use it in GitHub Desktop.
Fulltext index on fact__profile
-- Column: vectors type of tsvector
ALTER TABLE profiles.fact__profile ADD COLUMN vectors tsvector;
-- Fill column: vectors (fulltext index)
UPDATE profiles.fact__profile
SET tsvector = to_tsvector('english', identifier || ' ' || title);
-- Trigger: ts_vectors on profiles.fact__profile (keep fulltext index)
CREATE TRIGGER ts_vectors
BEFORE INSERT OR UPDATE
ON profiles.fact__profile
FOR EACH ROW
EXECUTE PROCEDURE tsvector_update_trigger('vectors', 'pg_catalog.english', 'identifier', 'title');
-- Index: profiles.fact__profile_vectors_idx
CREATE INDEX fact__profile_vectors_idx
ON profiles.fact__profile
USING gin
(vectors);
-- Example: WHERE through 2 columns + LIKE - cca 5000 ms
SELECT *
FROM profiles.fact__profile
WHERE identifier = 'Red Bulletin' OR title ILIKE '%Red Bulletin%'
-- Example: fulltext search - cca 250 ms
SELECT *
FROM profiles.fact__profile
WHERE vectors @@ to_tsquery('english', 'Red+Bulletin:*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment