I'm looking to optimise a PostgreSQL query of mine and couldn't find anything on the internal workings of the query planner when it comes to ORDER BY.
Consider the following PostgreSQL query:
SELECT *
FROM mytable
ORDER BY rank, ST_Distance(geom1, geom2)
Is ST_Distance(geom1, geom2) calculated for all rows in mytable, even where rank is unique?
This is not a postgis question. ST_Distance(geom1, geom2) could be any expression that requires (expensive) computations.