Answer the question
In order to leave comments, you need to log in
How to speed up query with DISTINCT in PostgreSQL?
Hello comrades! Please help me figure it out.
There is a pretty simple script. It looks like this:
SELECT DISTINCT("CITY" || ' | ' || "AREA" || ' | ' || "REGION") AS LOCATION FROM youtube
CREATE INDEX location_index ON youtube ("CITY", "AREA", "REGION")
Answer the question
In order to leave comments, you need to log in
The Postgresql scheduler does not currently have a loose index scan. Therefore, the planner, quite rightly from its point of view, chooses not to use any indexes.
Therefore, if it is distinct that is needed for a not very selective field, then it is more efficient to explain to the planner what they want from it with their hands: https://wiki.postgresql.org/wiki/Loose_indexscan
You rather need to change the data storage scheme.
No way. The PostgreSQL community deliberately refuses to add any scheduler hints, preferring to report scheduler errors as bugs and fix them if possible.
A composite index on fields is not the same as an index on the concatenated value of those fields. The corresponding index should be created like this
But it is even better to reconsider the approach to the structure of the table and queries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question