2

I have a materialized view that has a few columns that I need to run a complex OR query on with similarity matching and the structure is like this

mymatview:

column a,
column b,
column c,
column d,
column e

and on column D (which is a TEXT field) I run a query like this

SELECT * FROM mymatview   
WHERE ((LOWER(d) = <<some value>> ) OR (LENGTH(d) > 4 AND d % <<some value>>))  
    OR ((LOWER(d) = <<some other value>> ) OR (LENGTH(d) > 4 AND d % <<some other value>>))

Currently the matview has 1 mil+ rows in it and it runs a sequence scan on that table using the filter above with the OR conditionals. Does anyone know what a good index would be on column d that would help with performance on running a query like this?

UPDATE: I am using the package pg_trgm for the '%' similarity operator here. Postgres 9.6 https://www.postgresql.org/docs/9.6/pgtrgm.html

  • Did you try an index on (lower(d), length(d))? What is d % ... supposed to be? – a_horse_with_no_name Mar 23 at 14:24
  • @a_horse_with_no_name thats a similarity with threshold set as pg_trgm.similarity_threshold = 0.8 – jestep Mar 23 at 14:32
  • @a_horse_with_no_name no dice on that index either – jestep Mar 23 at 15:28
0

Your only chance is to index all possible conditions, perhaps PostgreSQL will use bitmap operations to cover the ORs. Perhaps it will think that a sequential scan is cheaper and go for that.

CREATE INDEX ON mymatview (lower(d));

CREATE INDEX ON mymatview USING gin (d gin_trgm_ops) WHERE length(d) > 4;
| improve this answer | |
  • Thats probably the best bet right there. Thank you! – jestep Mar 23 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.