Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9d5F-0002Iv-3E for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 15:34:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V9d5D-0007PD-S4 for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 15:33:59 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9d5B-0007P4-Km for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 15:33:57 +0000 Received: from mail.gransy.com ([2001:1528:137::f1]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9d58-00082O-0P for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 15:33:57 +0000 Received: from antispam.gransy.com (antispam.gransy.com [77.78.104.124]) by mail.gransy.com (Postfix) with ESMTP id F36E1508CD0 for ; Wed, 14 Aug 2013 17:33:48 +0200 (CEST) X-Virus-Scanned: G-Hosting.cz + Station.cz + Klenot.cz + Subreg.cz amavisd-new at antispam.gransy.com Received: from mail.gransy.com ([82.208.44.189]) by antispam.gransy.com (antispam.gransy.com [77.78.104.124]) (amavisd-new, port 10024) with ESMTP id 2J0OmbS2SbJZ for ; Wed, 14 Aug 2013 17:33:58 +0200 (CEST) Received: by mail.gransy.com (Postfix, from userid 5000) id 9F0F0508CD8; Wed, 14 Aug 2013 17:33:48 +0200 (CEST) Received: from sq.gransy.com (elizabeth.gransy.com [89.187.132.199]) by mail.gransy.com (Postfix) with ESMTPA id 72EE7508CCE for ; Wed, 14 Aug 2013 17:33:48 +0200 (CEST) Received: from 212.47.19.46 (SquirrelMail authenticated user tv@fuzzy.cz) by sq.gransy.com with HTTP; Wed, 14 Aug 2013 17:33:53 +0200 Message-ID: <3a56c4d2c0b2daa11a625c2983823901.squirrel@sq.gransy.com> Date: Wed, 14 Aug 2013 17:33:53 +0200 Subject: queries with DISTINCT / GROUP BY giving different plans From: "Tomas Vondra" To: pgsql-performance@postgresql.org User-Agent: SquirrelMail/1.4.22 MIME-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Hi all, I've run into a strange plan difference on 9.1.9 - the first query does "DISTINCT" by doing a GROUP BY on the columns (both INT). SELECT "f_account"."name_id" AS "a_1550", "f_message"."text_id" AS "a_1562" FROM "f_accountmessagefact" INNER JOIN "f_message" ON ( "f_accountmessagefact"."message_id" = "f_message"."id" ) INNER JOIN "f_account" ON ( "f_accountmessagefact"."account_id" = "f_account"."id" ) GROUP BY 1, 2; QUERY PLAN ----------------------------------------------------------------------------------------- Group (cost=3575011.59..3721066.43 rows=19473978 width=8) -> Sort (cost=3575011.59..3623696.54 rows=19473978 width=8) Sort Key: f_account.name_id, f_message.text_id -> Hash Join (cost=51718.44..1217195.39 rows=19473978 width=8) Hash Cond: (f_accountmessagefact.account_id = f_account.id) -> Hash Join (cost=51699.42..949409.18 rows=19473978 width=8) Hash Cond: (f_accountmessagefact.message_id = f_message.id) -> Seq Scan on f_accountmessagefact (cost=0.00..435202.78 rows=19473978 width=8) -> Hash (cost=37002.52..37002.52 rows=1175752 width=8) -> Seq Scan on f_message (cost=0.00..37002.52 rows=1175752 width=8) -> Hash (cost=11.23..11.23 rows=623 width=8) -> Seq Scan on f_account (cost=0.00..11.23 rows=623 width=8) (12 rows) Now, this takes ~45 seconds to execute, but after rewriting the query to use the regular DISTINCT it suddenly switches to HashAggregate with ~1/3 the cost (although it produces the same output, AFAIK), and it executes in ~15 seconds. SELECT DISTINCT "f_account"."name_id" AS "a_1550", "f_message"."text_id" AS "a_1562" FROM "f_accountmessagefact" INNER JOIN "f_message" ON ( "f_accountmessagefact"."message_id" = "f_message"."id" ) INNER JOIN "f_account" ON ( "f_accountmessagefact"."account_id" = "f_account"."id" ); QUERY PLAN ------------------------------------------------------------------------------------------ HashAggregate (cost=1314565.28..1509305.06 rows=19473978 width=8) -> Hash Join (cost=51718.44..1217195.39 rows=19473978 width=8) Hash Cond: (f_accountmessagefact.account_id = f_account.id) -> Hash Join (cost=51699.42..949409.18 rows=19473978 width=8) Hash Cond: (f_accountmessagefact.message_id = f_message.id) -> Seq Scan on f_accountmessagefact (cost=0.00..435202.78 rows=19473978 width=8) -> Hash (cost=37002.52..37002.52 rows=1175752 width=8) -> Seq Scan on f_message (cost=0.00..37002.52 rows=1175752 width=8) -> Hash (cost=11.23..11.23 rows=623 width=8) -> Seq Scan on f_account (cost=0.00..11.23 rows=623 width=8) (10 rows) I've tested this with other queries and those actually behave as expected (both using HashAggregate), so I'm wondering what's wrong with this one and why it's discarding a plan with much lower cost. Any ideas? The estimates are quite exact (and exactly the same for both queries). BTW I can't test this on 9.2 or 9.3 easily, as this is our production environment and I can't just export the data. I've tried to simulate this but so far no luck. regards Tomas -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance