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 1mpbgj-0002Li-VP for pgsql-performance@arkaria.postgresql.org; Tue, 23 Nov 2021 19:42:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mpbgi-0005DF-11 for pgsql-performance@arkaria.postgresql.org; Tue, 23 Nov 2021 19:42:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mpbgh-0005Ca-Ho for pgsql-performance@lists.postgresql.org; Tue, 23 Nov 2021 19:42:43 +0000 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mpbgd-0005sm-70 for pgsql-performance@lists.postgresql.org; Tue, 23 Nov 2021 19:42:42 +0000 Received: by mail-io1-xd32.google.com with SMTP id z26so57780iod.10 for ; Tue, 23 Nov 2021 11:42:38 -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=xJITBcQCp3BuHYOwZaZz6JknXk7PjrL4otKRsVFEisE=; b=DArulMSw0n6AUXwIsOu+tVtrpv59dEmD1E5gMFpe7F8HUSs8meqElEZVHtmR5kgSaD ZkFtTVi/bN5IBF2/LSR/4AdCAKdeNumNxohuVpLYMIEbTQCQoRuNztYebl9njBX/1HTB t5sUM0zHnvToVkptucz5YTUm4Dozrl0FPsMey6ZS5CdF+gcrOx1hvW0Ft4g4R/5SRwm5 IaSx+zjbTk03phQFdwlvN0Z6226V4jMZzQgmYxG/fHAkDW+nkowG8pLebExPdBrVoyjb h+Ow8vuJbiKMTkmpTTPbas4WKHChpX23fDxgF1xjETCrawW1uOcrzoqg9lIlBIZiigTv idWA== 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=xJITBcQCp3BuHYOwZaZz6JknXk7PjrL4otKRsVFEisE=; b=nJFcvtPoBLGku320EaNAYX0IjCXQouiGoy3ZfJGYGZYf+ledfs5KGFzeywpVbwlCH9 YwS7y4OeOtnCy555hyL51g7nQ3RsDI3KcIgfNaXYCahKH9HNS3YEjjXUADs2FaabOxIU rn/Qb0IBcS48SfHC1+5Nx/Z0IHW9x8CJ8GQvI5yadLljESxJpWsLcWF1wZQcjLmwG2Mh qp0rI3x7UUVK1+LyWHyKrpIZJIUdrWUY4vxkdAq8AcUQKTvZExAbbWsZ19s/lZvSUqkR 3WGacSebh/YyKNKzXjZkZHLjQ9jKeT+fpLEtNRyZ9Xxo1G+dXMk7q4FL/teeeSCaXzYd EoCg== X-Gm-Message-State: AOAM5304pxrhn/pdcEZkCIUKfJvCoEFEwknVlOmGQBFwxudewmD9bWIQ BeVcZLOZWbSGVD5U13wrbLUyTQ== X-Google-Smtp-Source: ABdhPJz3JNPg8HqroV/rtcWfTT3M6NLXYlPDkrWQmVrETr2TxgXFq8IYax7NnggieN5aSTNHSRZBow== X-Received: by 2002:a5d:8451:: with SMTP id w17mr9210174ior.139.1637696557102; Tue, 23 Nov 2021 11:42:37 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id l12sm439681iow.23.2021.11.23.11.42.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Nov 2021 11:42:36 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 38A1B800B45; Tue, 23 Nov 2021 13:42:35 -0600 (CST) Date: Tue, 23 Nov 2021 13:42:35 -0600 From: Justin Pryzby To: =?utf-8?B?SmnFmcOt?= Fejfar Cc: pgsql-performance@lists.postgresql.org Subject: Re: performance of analytical query Message-ID: <20211123194234.GV17618@telsasoft.com> References: <20211112024151.GF17618@telsasoft.com> 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 Fri, Nov 12, 2021 at 09:12:38PM +0100, Jiří Fejfar wrote: > * I know that PG is focused on OLTP rather then analytics, but we are happy > with it at all and do not wish to use another engine for analytical > queries... isn't somewhere some "PG analytical best practice" available? It's a good question. Here's some ideas: I don't think we know what version you're using - that's important, and there's other ideas here: https://wiki.postgresql.org/wiki/Slow_Query_Questions You said that your query was slow "probably after VACUUM ANALYZE". Is it really faster without stats ? You can do this to see if there was really a better plan "before": | begin; DELETE FROM pg_statistic WHERE starelid='thetable'::regclass; explain analyze ...; rollback; Try enable_nestloop=off for analytic queries; Test whether jit=off helps you or hurts you (you said that it's already disabled); You can do other things that can improve estimates, by sacrificing planning time (which for an analytic query is a small component of the total query time, and pays off at runtime if you can get a btter plan): - FKs can help with estimates since pg9.6; - CREATE STATISTICS; - ALTER SET STATISTICS or increase default_statistics_target; - increase from_collapse_limit and join_collapse_limit. But I don't think it will help your current query plan. - partitioning data increases planning time, and (if done well) can allow improved execution plans; You can REINDEX or maybe CLUSTER during "off hours" to optimize indexes/tables. BRIN indexes (WITH autoanalyze) are very successful for us, here. You can monitor your slow queries using auto_explain and/or pg_stat_statements. You can reduce autovacuum_analyze_threshold to analyze more often. I'd be interested to hear if others have more suggestions. -- Justin