Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mlMVv-0001VG-98 for pgsql-performance@arkaria.postgresql.org; Fri, 12 Nov 2021 02:42:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mlMVu-0004DB-0L for pgsql-performance@arkaria.postgresql.org; Fri, 12 Nov 2021 02:42:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mlMVt-0004D0-J8 for pgsql-performance@lists.postgresql.org; Fri, 12 Nov 2021 02:42:01 +0000 Received: from mail-io1-xd2d.google.com ([2607:f8b0:4864:20::d2d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mlMVm-00061w-62 for pgsql-performance@lists.postgresql.org; Fri, 12 Nov 2021 02:42:00 +0000 Received: by mail-io1-xd2d.google.com with SMTP id v65so9444175ioe.5 for ; Thu, 11 Nov 2021 18:41:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=i537SYtOWr0xW8R0Qu2uyEjEK9QvWRLrybBKHI/VlQs=; b=dJDP0Yco/qPVfbrhObQW+tbtIirOGPmSuAr9wYURcU3ROB14aZFuRZsB/ICfSiWv09 5rp6yYWeioR6oiHR3sJjjMMB0d01il9z+suKHPUWMz8z4BmeZOg+eHZQuW/QogKcG/gs Wl7TCME+5vn8o4ZYWdg1igFrSodMoXkFlLzmhvzmKbZfvBh3qRcLUBBMt752Az/mXq0i RLh2XzCKcskueMrkiUi24WL+4H2LxB31+ACOiRgFxm43bDQY6fh2qf+4Fw7SKTeVWyxd hLhAOAYklbw91fLv+8cd3eK9xJDesSzbsKA7BWNu7nIC4Dvlk7AhhVMz95bHF4FaGcc4 Prng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=i537SYtOWr0xW8R0Qu2uyEjEK9QvWRLrybBKHI/VlQs=; b=jWyLCcYANC7r97JwcXy8YYMVOeI/qzDFKK87oWjVz3dYPydkuqTXiAYTGrK7EePC0t UeU2lbwN80+8PE6R27NYD422ioKVfpHR7fVTw5hk1teXWqUBxg12qg52SrdMXpk9wBl9 au3sjbao7aAjEVcLOIQMCVs8qbEzirSFCoTS0MmBsKYW8y6VczoJ5BLUZHVkcTkSJRMz 8Gxc4J6kC7/N+UqPSZ7SXg9W2KkcTY2FBR73f0Nq2tYsjdGTJvLwZfPnx3pmhKyot8Jl CYTgNFSyt8xJQ1vgnmfVsFFtVPpT6ecxajZXpBDn2O9oQeNRjF7Q5hqCWWoAPS0U9Nsf CQaw== X-Gm-Message-State: AOAM531QsWUQZ0RQl/AL+0dbGXv2WycvgyaD+B9Adp2fgxBElxi78Jzi 0XPkFVA1X1JvFxNzdYcGmJB8CA== X-Google-Smtp-Source: ABdhPJxWf9AVumaDXvm59Q6obAzTKtsY2y63kTIrLDrJO4VyZk0ql6QFdqMgx4dIY9X1MSubioksKQ== X-Received: by 2002:a6b:620a:: with SMTP id f10mr8083719iog.156.1636684913295; Thu, 11 Nov 2021 18:41:53 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id c4sm2479576ioo.48.2021.11.11.18.41.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Nov 2021 18:41:52 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 9EFB280073B; Thu, 11 Nov 2021 20:41:51 -0600 (CST) Date: Thu, 11 Nov 2021 20:41:51 -0600 From: Justin Pryzby To: =?utf-8?B?SmnFmcOt?= Fejfar Cc: pgsql-performance@lists.postgresql.org Subject: Re: performance of analytical query Message-ID: <20211112024151.GF17618@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Nov 11, 2021 at 08:20:57PM +0100, Jiří Fejfar wrote: > Hi folks, > > we have found that (probably after VACUUM ANALYZE) one analytical query > starts to be slow on our production DB. Moreover, more or less the same > plan is used on our testing data (how to restore our testing data is > described at the end of this email), or better to say the same problem > exists in both (production vs testing data) scenarios: nested loop scanning > CTE several thousand times is used due to the bad estimates: > https://explain.dalibo.com/plan/sER#plan/node/87 (query is included on > dalibo). > Do you have any idea how to get HASH JOINS in the CTE w_1p_data instead of > NESTED LOOPs? > * Add some statistics to not get bad estimates on "lower-level" CTEs? Do you know why the estimates are bad ? Index Scan using t_map_plot_cell__cell_gid__idx on cm_plot2cell_mapping cm_plot2cell_mapping (cost=0.29..18.59 rows=381 width=12) (actual time=0.015..2.373 rows=3,898 loops=1) Index Cond: (cm_plot2cell_mapping.estimation_cell = f_a_cell.estimation_cell) Buffers: shared hit=110 I don't know, but is the estimate for this portion of the plan improved by doing: | ALTER TABLE f_a_cell ALTER estimation_cell SET STATISTICS 500; ANALYZE f_a_cell; > * In a slightly more complicated function I used temporary tables to be > able to narrow statistics [2] but I am afraid of system table bloating > because of the huge amount of usage of this function on the production > (hundred thousand of calls by day when data are to be analyzed). I would try this for sure - I think hundreds of calls per day would be no problem. If you're concerned, you could add manual calls to do (for example) VACUUM pg_attribute; after dropping the temp tables. BTW, we disable nested loops for the our analytic report queries. I have never been able to avoid pathological plans any other way.