Re: slow sort - Mailing list pgsql-performance

From Maximilian Tyrtania
Subject Re: slow sort
Date
Msg-id 6E353E0A-1DA2-452A-BD91-DF6BE916F05D@contactking.de
Whole thread Raw
In response to Re: slow sort  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-performance
Am 11.09.2013 um 17:31 schrieb Andrew Dunstan <andrew@dunslane.net>:

> I recently had to diagnose and remedy a case such as this.
>
> The short answer is to rewrite your query so you don't have to group by so many things. Collect your aggregates in a
commontable expression query (or possibly more than one, depends what you need) using the minimum non-aggregated
columnsto enable you to get correct results and then later decorate that with all the extra things you need such as
constantcolumns and columns that are irrelevant to the aggregation. 
>
> This gets hard when queries are very complex, and harder still when the query is written by a query generator. But a
goodgenerator should not just say "grouo by everything that's not aggregated" and think it's doing a good job. In your
caseit should be relatively straightforward. 
>
> cheers
>
> andrew

Ah, yes, only now do I see that the query screams for a CTE. Thanks for the eye opener.

Maximilian Tyrtania
http://www.contactking.de

pgsql-performance by date:

Previous
From: Roberto Grandi
Date:
Subject: Re: COPY TO and VACUUM
Next
From: Souquieres Adam
Date:
Subject: Memory-olic query and Materialize