Re: slow sort

From: Maximilian Tyrtania <lists(at)contactking(dot)de>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: slow sort
Date: 2013-09-12 08:00:32
Message-ID: 6E353E0A-1DA2-452A-BD91-DF6BE916F05D@contactking.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Am 11.09.2013 um 17:31 schrieb Andrew Dunstan <andrew(at)dunslane(dot)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 common table expression query (or possibly more than one, depends what you need) using the minimum non-aggregated columns to enable you to get correct results and then later decorate that with all the extra things you need such as constant columns 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 good generator should not just say "grouo by everything that's not aggregated" and think it's doing a good job. In your case it 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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Souquieres Adam 2013-09-12 10:14:21 Memory-olic query and Materialize
Previous Message Roberto Grandi 2013-09-12 06:14:11 Re: COPY TO and VACUUM