I'm doing a full-text search with a GIN index in Postgres (all localhost), and when I write my own query and run it in psql, I am getting great response times and EXPLAIN ANALYZE reports an index hit (woohoo), however when querying via the Django Admin search box with the same search terms, the index is not scanned and the query takes upwards of forever to complete.
My index is created thusly via the fully-awesome pg_trgm Postgres extension:
CREATE INDEX name_gin ON entity USING gin (name gin_trgm_ops);
This query hits the index and takes a blazing 84ms to search through 900k full-text records:
SELECT COUNT(*)
FROM entity
WHERE name LIKE UPPER('%dubteeeff%')
AND name LIKE UPPER('%django%');
The exact same query, as created by the Django Admin interface, takes 938ms:
SELECT COUNT(*)
FROM entity
WHERE UPPER("entity"."name"::text) LIKE UPPER('%dubteeeff%')
and UPPER("entity"."name"::text) LIKE UPPER('%django%');`
The only difference appears to be the way the columns are referenced--I'm fairly new with Django and Postgres--is there some Postgres config setting or Django admin QuerySet-something, or a RawQuery that I could override or modify to speed this up? I don't want my Admin page to at best be slow and at worst, drag the rest of the site down with it.
Thanks in advance.
upper(name)is used, which is not indexed. Araqnid is right that you probably need an index onupper(name)rather than "just"name. Or teach django to not apply theupperfunction (maybe tell it to not make the query case-insensitive)