I have an expensive, periodic PostgreSQL 9.3 query of the form:
SELECT * from mytable where name NOT LIKE '%foo%';
foo
is actually a constant here that never changes. The query is expensive because it requires a table scan. I expect that only a few rows will actually match the query, so I want to use a partial index to speed up the query.
CREATE INDEX foo_idx ON mytable ((name NOT LIKE '%foo%')) WHERE name NOT LIKE '%foo%';
However, when I do an EXPLAIN
on my query, it still is using a sequential scan instead of relying on the new index. What am I doing wrong?
I have tried several different expressions inside the parenthesized expression clause of the CREATE INDEX
statement, but nothing helps:
CREATE INDEX foo_idx ON mytable (name) WHERE name NOT LIKE '%foo%';
CREATE INDEX foo_idx ON mytable ((name LIKE '%foo%')) WHERE name NOT LIKE '%foo%';
I have also tried removing the WHERE
clause for the index, and that doesn't help either.
NOT LIKE '%foo%'
in a simple SQL function that you use in both the index and query, does that then use the index?like '%foo%'
, not sure aboutnot like
though: postgresonline.com/journal/archives/…