Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZPLs3-0003kN-GD for pgsql-performance@arkaria.postgresql.org; Wed, 12 Aug 2015 02:34:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZPLs3-0005ih-2R for pgsql-performance@arkaria.postgresql.org; Wed, 12 Aug 2015 02:34:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZPLs1-0005iU-Co for pgsql-performance@postgresql.org; Wed, 12 Aug 2015 02:34:25 +0000 Received: from mbob.nabble.com ([162.253.133.15]) by makus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZPLry-0007oZ-5a for pgsql-performance@postgresql.org; Wed, 12 Aug 2015 02:34:23 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mbob.nabble.com (Postfix) with ESMTP id CBAB112FD1CB for ; Tue, 11 Aug 2015 19:33:42 -0700 (PDT) Date: Tue, 11 Aug 2015 19:34:20 -0700 (MST) From: robbyc To: pgsql-performance@postgresql.org Message-ID: <1439346860122-5861835.post@n5.nabble.com> Subject: Slow Query MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.1 (/) 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, I am new to optimizing queries and i'm getting a slow running time (~1.5secs) with the following SQL: SELECT distinct("Vacancy"."ID"), "Vacancy"."JobTitle", "Vacancy"."DateCreated", "Vacancy"."CustomAccess" , "Department"."Name" as "Department", list("Occupation"."Name") as "Occupation", "Vacancy"."PositionNo" , "Vacancy"."Template" from "Vacancy" LEFT JOIN "CategoryOption_TableRow" as "c_22" ON ("c_22"."RowID" = "Vacancy"."ID" and "c_22"."Category_TableID" = 22) LEFT JOIN "CategoryOption" as "Occupation" ON ("Occupation"."ID" = "c_22"."CategoryOptionID") LEFT JOIN "TableRow_TableRow" as "t_33" ON ("t_33"."Table1RowID" = "Vacancy"."ID" and "t_33"."Table_TableID" = 33 ) LEFT JOIN "Department" ON ("Department"."ID" = "t_33"."Table2RowID" and "Department"."Active" = 't' and "Department" ."ClientID" = 263) JOIN "CategoryOption_TableRow" as "c_50" ON ("c_50"."Category_TableID"= 50 and "c_50"."RowID" = "Vacancy" ."ID" and "c_50"."CategoryOptionID"=19205) WHERE "Vacancy"."ClientID" = 263 AND "Vacancy"."ID" NOT IN(SELECT DISTINCT("Vacancy"."ID") FROM "Vacancy" join "CategoryOption_TableRow" "ct126" on ("ct126"."Category_TableID" = 126 and "RowID" = "Vacancy"."ID") left join "Workflow" on ("Workflow"."VacancyID" = "Vacancy"."ID" and "Workflow"."Level" = 1) left join "CategoryOption_TableRow" "c30" on ("c30"."RowID" = "Workflow"."ID" and "c30"."Category_TableID" = 30 and "c30"."CategoryOptionID" = 21923) WHERE "Template" AND "ct126"."CategoryOptionID" IN(34024,35254,35255,35256)) and "Vacancy" ."Template" = 't' GROUP BY "Vacancy"."ID", "Vacancy"."JobTitle", "Vacancy"."DateCreated", "Vacancy"."CustomAccess", "Department" ."Name", "Vacancy"."PositionNo", "Vacancy"."Template" UNION SELECT distinct("Vacancy"."ID"), "Vacancy"."JobTitle", "Vacancy"."DateCreated", "Vacancy"."CustomAccess" , "Department"."Name" as "Department", list("Occupation"."Name") as "Occupation", "Vacancy"."PositionNo" , "Vacancy"."Template" from "Vacancy" LEFT JOIN "CategoryOption_TableRow" as "c_22" ON ("c_22"."RowID" = "Vacancy"."ID" and "c_22"."Category_TableID" = 22) LEFT JOIN "CategoryOption" as "Occupation" ON ("Occupation"."ID" = "c_22"."CategoryOptionID") LEFT JOIN "TableRow_TableRow" as "t_33" ON ("t_33"."Table1RowID" = "Vacancy"."ID" and "t_33"."Table_TableID" = 33 ) LEFT JOIN "Department" ON ("Department"."ID" = "t_33"."Table2RowID" and "Department"."Active" = 't' and "Department" ."ClientID" = 263) JOIN "CategoryOption_TableRow" as "c_50" ON ("c_50"."Category_TableID"= 50 and "c_50"."RowID" = "Vacancy" ."ID" and "c_50"."CategoryOptionID"=19205) WHERE "Vacancy"."ClientID" = 263 AND "Vacancy"."ID" NOT IN(SELECT DISTINCT("Vacancy"."ID") FROM "Vacancy" join "CategoryOption_TableRow" "ct126" on ("ct126"."Category_TableID" = 126 and "RowID" = "Vacancy"."ID") left join "Workflow" on ("Workflow"."VacancyID" = "Vacancy"."ID" and "Workflow"."Level" = 1) left join "CategoryOption_TableRow" "c30" on ("c30"."RowID" = "Workflow"."ID" and "c30"."Category_TableID" = 30 and "c30"."CategoryOptionID" = 21923) WHERE "Template" AND "ct126"."CategoryOptionID" IN(34024,35254,35255,35256)) and "Vacancy" ."Template" <> 't' AND "Vacancy"."Level" = 1 GROUP BY "Vacancy"."ID", "Vacancy"."JobTitle", "Vacancy"."DateCreated", "Vacancy"."CustomAccess", "Department" ."Name", "Vacancy"."PositionNo", "Vacancy"."Template" ORDER BY "JobTitle" Running explain analyze gives me the following information: http://explain.depesz.com/s/pdC For a total runtime: 2877.157 ms If i remove the left joins on Department and TableRow_TableRow this reduces the run time by about a third. Additionally removing CategoryOption and CategoryOption_TableRow joins further reduces by a about a third. Given that i need both these joins for the information retrieved by them, what would be the best way to re-factor this query so it runs faster? Looking at the output of explain analyze the hash aggregates and sort seem to be the primary issue. Thanks in advance -- View this message in context: http://postgresql.nabble.com/Slow-Query-tp5861835.html Sent from the PostgreSQL - performance mailing list archive at Nabble.com. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance