Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iFICL-0005ox-Qo for pgsql-performance@arkaria.postgresql.org; Tue, 01 Oct 2019 13:28:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iFICK-0006qZ-A1 for pgsql-performance@arkaria.postgresql.org; Tue, 01 Oct 2019 13:28:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iFICK-0006qS-0u for pgsql-performance@lists.postgresql.org; Tue, 01 Oct 2019 13:28:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iFICH-0006KN-PC for pgsql-performance@lists.postgresql.org; Tue, 01 Oct 2019 13:28:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x91DR674009625; Tue, 1 Oct 2019 09:27:06 -0400 From: Tom Lane To: Behrang Saeedzadeh cc: pgsql-performance@lists.postgresql.org Subject: Re: Slow PostgreSQL 10.6 query In-reply-to: References: Comments: In-reply-to Behrang Saeedzadeh message dated "Tue, 01 Oct 2019 22:37:03 +1000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9623.1569936426.1@sss.pgh.pa.us> Date: Tue, 01 Oct 2019 09:27:06 -0400 Message-ID: <9624.1569936426@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Behrang Saeedzadeh writes: > On my machine, this query that is generated by Hibernate runs in about 57 > ms on MySQL 8 but it takes more than 1 second to run on PostgreSQL: > SELECT bills.id AS bill_id, > bills.bill_date AS bill_date, > bills.bill_number AS bill_number, > branch_bills.branch_id AS branch_id, > company_bills.company_id AS company_id > FROM tbl_bills bills > LEFT OUTER JOIN tbl_branch_bills branch_bills ON bills.id = > branch_bills.bill_id > LEFT OUTER JOIN tbl_company_bills company_bills ON bills.id = > company_bills.bill_id > INNER JOIN tbl_branches ON branch_bills.branch_id = > tbl_branches.id > WHERE branch_bills.branch_id IN ( > SELECT b.id > FROM tbl_branches b > INNER JOIN tbl_rules r ON b.id = r.branch_id > INNER JOIN tbl_groups g ON r.group_id = g.id > INNER JOIN (tbl_group_permissions gp INNER JOIN > tbl_permissions p ON gp.permission_id = p.id) > ON g.id = gp.group_id > INNER JOIN tbl_users u ON r.user_id = u.id > WHERE u.id = 1 > AND r.rule_type = 'BRANCH' > AND p.name = 'Permission W' > ); [ counts the JOINs... ] You might try raising join_collapse_limit and from_collapse_limit to be 12 or so. regards, tom lane