I'm using Postgresql 9.6 and I'm running into a performance issue. I have a relatively simple query:
explain analyze
SELECT * FROM (
SELECT A.person_id, O.session_id, timestamp_ AS step_ts_min ,rank() over (partition by A.person_id order by timestamp_) as rn
FROM event_page O
JOIN alias A ON (O.person_alias = A.alias)
WHERE O.timestamp_ between '2017-02-18 00:00:00.000' AND '2017-03-13 00:00:00.000'
and O.location_host like '2016.myhost.ca'
) as innerstep WHERE rn=1
The event_page
table is actually a view over monthly tables union-ed together, you can see that in the plan.
This query takes over 2.5 minutes to run and you can see the lions share of that time is used by quicksort, along with 1.8G of ram!!!
Here is the execution plan: https://explain.depesz.com/s/v51h
Is there a way to improve performance and optimize the query?