Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eAIVn-0002tT-Hs for pgsql-performance@arkaria.postgresql.org; Thu, 02 Nov 2017 16:38:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eAIVn-0003lw-4i for pgsql-performance@arkaria.postgresql.org; Thu, 02 Nov 2017 16:38:35 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eAIU1-00009g-IR for pgsql-performance@postgresql.org; Thu, 02 Nov 2017 16:36:45 +0000 Received: from gproxy9-pub.mail.unifiedlayer.com ([69.89.20.122]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eAITw-0004oJ-15 for pgsql-performance@postgresql.org; Thu, 02 Nov 2017 16:36:44 +0000 Received: from cmgw3 (unknown [10.0.90.84]) by gproxy9.mail.unifiedlayer.com (Postfix) with ESMTP id 883C61E0678 for ; Thu, 2 Nov 2017 10:36:30 -0600 (MDT) Received: from host214.hostmonster.com ([74.220.215.214]) by cmgw3 with id V4cS1w00K4e7MuJ014cVwr; Thu, 02 Nov 2017 10:36:30 -0600 X-Authority-Analysis: v=2.2 cv=H76r+6Qi c=1 sm=1 tr=0 a=E7lA7DdVRVhKYBNJxJVZOg==:117 a=E7lA7DdVRVhKYBNJxJVZOg==:17 a=IkcTkHD0fZMA:10 a=sC3jslCIGhcA:10 a=_7kE2Dd2qvdIwnCIYHoA:9 a=QEXdDO2ut3YA:10 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gusw.net; s=default; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:MIME-Version :Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=5mZnnVLnJxn4M4ExLnKbhtrd89scfIIBsvHLO84rgiw=; b=aNYuJi8OpSYvuaCXmCKssSyj1g 82Rc7SKrAubFABt6xPn82xZB1K8f0CoL5KuQXFiTBChkEGLgcuAQNBTrqZrXBULV/XEsCGpiQAHOJ f9OiGQKCOs6g2OdyH4eAMsSZy; Received: from [191.7.145.23] (port=51608 helo=[192.168.9.5]) by host214.hostmonster.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.87) (envelope-from ) id 1eAITh-0002ru-E1 for pgsql-performance@postgresql.org; Thu, 02 Nov 2017 10:36:26 -0600 Subject: Re: OLAP/reporting queries fall into nested loops over seq scans or other horrible planner choices To: pgsql-performance@postgresql.org References: From: Gunther Message-ID: Date: Thu, 2 Nov 2017 12:36:13 -0400 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - host214.hostmonster.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - gusw.net X-BWhitelist: no X-Source-IP: 191.7.145.23 X-Exim-ID: 1eAITh-0002ru-E1 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.9.5]) [191.7.145.23]:51608 X-Source-Auth: gunther+pragmaticdata.com X-Email-Count: 1 X-Source-Cap: cHJhZ21hdDE7cHJhZ21hdDE7aG9zdDIxNC5ob3N0bW9uc3Rlci5jb20= X-Local-Domain: yes 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 Thanks for your support Laurent. I have an idea on one thing you said: > Just adding to your voice. I recently experienced the same issue with a complex multi-table view, including pivots, and was surprised to see all the nested loops everywhere and here is the clue for me: > in spite of indices being available. I would say that sometimes indexes are detrimental. If you don't need them for other reasons, you might want to not have them. And without the index, the Nested Loop strategy might not be chosen. But that is a side-issue, because it can often not be avoided. Just saying in case it might help. I also found the opposite now. In the query that made me "blow the lid" and "complain" here, my team decided to add an index and that did not get rid of Nested Loops but at least made the inner table access indexed rather than a table scan and the performance ended up OK. But it's not always predictable, and these indexes could trap the planner into sub-optimal solutions still. I think there is an opportunity for a PgSQL query plan extension, especially wen dealing with CTE (WITH-clauses), PgSQL could make them a temporary table and add indexes that it needs for it on the fly, because after it has done one pass over the inner loop sequential scan it knows perfectly well how many rows it has, and knowing how many more iterations are coming from the sub-query that's driving the Nested Loop, it could decide that it's much faster to put an index on the nested relation, temporarily materialized. Or it could even decide to change it's plan mid-way and do the Hash Join. This is why I had always dreamed that the PgSQL optimizer had some easy API where one could plug in experimental strategies. I personally am extremely efficient with XSLT for complex intelligent algorithms, and I dream of a PgSQL query plan structure exposed as XML which an XSLT plugin could then process to edit the plan. People could experiment with awesome intelligent new strategies based on statistics gathered along the way of the execution. regards, -Gunther -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance