Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XgcLD-0000al-Q6 for pgsql-performance@arkaria.postgresql.org; Tue, 21 Oct 2014 16:31:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XgcLC-0003js-CS for pgsql-performance@arkaria.postgresql.org; Tue, 21 Oct 2014 16:31:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XgcLB-0003jj-7o for pgsql-performance@postgresql.org; Tue, 21 Oct 2014 16:31:21 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XgcL4-00037G-TL for pgsql-performance@postgresql.org; Tue, 21 Oct 2014 16:31:19 +0000 Received: from mfilter4-d.gandi.net (mfilter4-d.gandi.net [217.70.178.134]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id B7F471720BF; Tue, 21 Oct 2014 18:31:11 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter4-d.gandi.net Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by mfilter4-d.gandi.net (mfilter4-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id G5STBUYd22zA; Tue, 21 Oct 2014 18:31:10 +0200 (CEST) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 2493517208C; Tue, 21 Oct 2014 18:31:08 +0200 (CEST) Message-ID: <54468A4A.8090908@dunslane.net> Date: Tue, 21 Oct 2014 12:31:06 -0400 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Marco Di Cesare , Merlin Moncure CC: "pgsql-performance@postgresql.org" Subject: Re: Query with large number of joins References: <0a72826fe7d04b71b9ab47d24992d898@CO2PR0701MB759.namprd07.prod.outlook.com> In-Reply-To: <0a72826fe7d04b71b9ab47d24992d898@CO2PR0701MB759.namprd07.prod.outlook.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) 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 On 10/21/2014 12:09 PM, Marco Di Cesare wrote: > I did not mean to imply this works any better on SQL Server. We never tried. I just meant to say this is the first time we are using Postgres so we don't have much experience with it. > > We tried with work_mem set to 1GB (even as high as 3GB) but it didn't change the GroupAggregate and Sort or query run time. > > Sorry, I had to sanitize the query and a few of the relevant tables so hopefully I got it all right. > > SELECT > "foxtrot_india"."juliet_alpha", > "foxtrot_india"."foxtrot_yankee", > "foxtrot_india"."hotel_sierra", > "foxtrot_india"."juliet_alpha", > "foxtrot_india"."bravo_romeo", > "oscar_bravo"."golf_foxtrot", > "seven_kilo"."november_lima", > "foxtrot_india"."echo_six", > "uniform_six"."seven_six", > "oscar_charlie"."foxtrot_charlie", > COUNT(DISTINCT "foxtrot_india"."bravo_romeo") > FROM > "public"."seven_kilo" "seven_kilo" > INNER JOIN "public"."papa_sierra" "papa_sierra" ON ("seven_kilo"."golf_bravo" = "papa_sierra"."golf_bravo") > LEFT JOIN "public"."golf_two" "golf_two" ON ("seven_kilo"."lima" = "golf_two"."lima") > LEFT JOIN "public"."bravo_xray" "bravo_xray" ON ("seven_kilo"."lima" = "bravo_xray"."lima") > LEFT JOIN "public"."foo1" "foo1" ON (("seven_kilo"."bar1" = "foo1"."bar1") AND ("seven_kilo"."golf_bravo" = "foo1"."golf_bravo")) > INNER JOIN "public"."oscar_charlie" "oscar_charlie" ON ("seven_kilo"."lima" = "oscar_charlie"."lima") > INNER JOIN "public"."oscar_bravo" "oscar_bravo" ON ("oscar_charlie"."foxtrot_four" = "oscar_bravo"."foxtrot_four") > INNER JOIN "public"."foxtrot_india" "foxtrot_india" ON ("oscar_bravo"."sierra" = "foxtrot_india"."sierra") > INNER JOIN "public"."hotel_romeo" "hotel_romeo" ON ("oscar_charlie"."foxtrot_charlie" = "hotel_romeo"."foxtrot_charlie") > INNER JOIN "public"."uniform_six" "uniform_six" ON ("hotel_romeo"."hotel_lima" = "uniform_six"."hotel_lima") > LEFT JOIN "public"."lookup" "foo2" ON ("foxtrot_india"."bar2" = "foo2"."lookup_id") > LEFT JOIN "public"."uniform_two" "uniform_two" ON ("foxtrot_india"."sierra" = "uniform_two"."sierra") > INNER JOIN "public"."lookup" "four_xray" ON ("uniform_two"."quebec" = "four_xray"."quebec") > LEFT JOIN "public"."papa_four" "papa_four" ON ("foxtrot_india"."sierra" = "papa_four"."sierra") > INNER JOIN "public"."lookup" "romeo_bravo" ON ("papa_four"."quebec" = "romeo_bravo"."quebec") > LEFT JOIN "public"."juliet_two" "juliet_two" ON ("foxtrot_india"."sierra" = "juliet_two"."sierra") > INNER JOIN "public"."lookup" "four_delta" ON ("juliet_two"."quebec" = "four_delta"."quebec") > LEFT JOIN "public"."foo3" "foo3" ON ("foxtrot_india"."bar3" = "foo3"."bar3") > INNER JOIN "public"."xray" "xray" ON ("seven_kilo"."lima" = "xray"."lima") > INNER JOIN "public"."romeo_echo" "romeo_echo" ON ("xray"."echo_sierra" = "romeo_echo"."echo_sierra") > WHERE > ((("xray"."echo_sierra" = 'november_foxtrot') > AND ("romeo_echo"."hotel_oscar" = 'zulu') > AND ("oscar_charlie"."five" = 6) > AND ("oscar_charlie"."whiskey" = 'four_romeo') > AND ("oscar_charlie"."charlie_romeo" = 2014))) > GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Please don't top-post on the PostgreSQL lists. See Have you tried a) either turning off geqo or setting geqo_threshold fairly high b) setting join_collapse_limit fairly high (assuming all the above join targets are tables and not views, setting it to something like 25 should do the trick. You also haven't told us what settings you have for things like effective_cache_size, which can dramatically affect query plans. cheers andrew -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance