On Thu, Jun 20, 2013 at 6:24 PM, Maciek Sakrejda <m.sakrejda@xxxxxxxxx> wrote:
SELECTDISTINCT ON (type) ts, type, detailsFROMobservationsWHEREsubject = '...'ORDER BYtype, ts DESC;
First thing: What is your "work_mem" set to, and how much RAM is in the machine? If you look at the plan, you'll immediately notice the "external merge Disk" line where it spills to disk on the sort. Try setting your work_mem to 120MB or so (depending on how much RAM you have, # concurrent sessions, complexity of queries etc)