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 1l2mID-0000rd-Mh for pgsql-performance@arkaria.postgresql.org; Fri, 22 Jan 2021 02:35:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l2mIC-0005LK-Lf for pgsql-performance@arkaria.postgresql.org; Fri, 22 Jan 2021 02:35:20 +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 1l2mIC-0005LC-El for pgsql-performance@lists.postgresql.org; Fri, 22 Jan 2021 02:35:20 +0000 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l2mI9-0006Cc-P9 for pgsql-performance@lists.postgresql.org; Fri, 22 Jan 2021 02:35:19 +0000 Received: by mail-io1-xd30.google.com with SMTP id h11so8229656ioh.11 for ; Thu, 21 Jan 2021 18:35:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=XBBCeI8mw87kbaUnrjxJyZv48o/zagj+ufFDRvNuMaI=; b=07GVACH1uq1qUDLCJ4sDtnIz23zbxEQqC5+aB5baLMMEAuuw9vvrBcaIisPH/83cir qePuz6JV3KqL34GnBbtcv6iaCxeGyWovDw9BoCqAUwIgaxQU9AQWIWPCnhfNNAQWKsFb 54ot32ijulprtbGpzdT6LEbx3ys6hJhVTlaHDfiaHFmZqmTdXgsfwcTJnVEkhM4mNNCX j4b71t1zgZ7ko/nzOfUdDNpjPcQ3leYemOsFuaDnToOj80dDOXVIOb25ps1Xvi2v38Fx eq2VBjkCPwEGGMVGfx1eNG1YdGi3PMRnnkd/p4XxguPGJHo9cpokYsKZ3nGLKisAw2Yu B44Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=XBBCeI8mw87kbaUnrjxJyZv48o/zagj+ufFDRvNuMaI=; b=S9B6u77oM0Q1ojClxPZRQYzMD5mYgKxFgumT+buSjP1/GP/sB6+FlF+B/YtPjPJ6g7 5HoKaNvSZZtpn3T3a8IDZExSpErmf5fhNNUpXTdob7qcV4M0nFDI/8FbNsZpB3LfAVRv ml/XcV6Qb+OZEGduydimsP6wS2GqchEOH/fa4dNmOW9yAbZpPu/CXvtxPhvSuzRVPeYM +RbKrtI+eMAK0KrbXd79VD04lgjaEhM0ZANBAJ8x9c9QlVMCsv9J4Ha/qpum6ntrPK4t K2A25F9x9o2w92DDYhzKqd3UonWFrFcj99ZGEdz10accp5tReFddXiCzG3Ms+b9fU4q0 qhvQ== X-Gm-Message-State: AOAM532dbD4m+JbQ9mVLoHEr4wACS/XP3oANqgbhYEWQdgxB1le+Zh9T yRdL5a032d/cHfLqcKyrbgbP5PSkSzDvOg== X-Google-Smtp-Source: ABdhPJzyABkKmWmmKxVvgA33VHQdFVY6vaUuUsKyBuQNlfn1tQa/TgykwIuor1cooBr1avK678Bu2Q== X-Received: by 2002:a5e:8d15:: with SMTP id m21mr1921855ioj.114.1611282916698; Thu, 21 Jan 2021 18:35:16 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id e9sm4506057ilc.73.2021.01.21.18.35.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Jan 2021 18:35:15 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 9B55B800864; Thu, 21 Jan 2021 20:35:14 -0600 (CST) Date: Thu, 21 Jan 2021 20:35:14 -0600 From: Justin Pryzby To: Nagaraj Raj Cc: pgsql-performance@lists.postgresql.org Subject: Re: Query performance issue Message-ID: <20210122023514.GD27167@telsasoft.com> References: <135856010.59446.1611280406074.ref@mail.yahoo.com> <135856010.59446.1611280406074@mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <135856010.59446.1611280406074@mail.yahoo.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jan 22, 2021 at 01:53:26AM +0000, Nagaraj Raj wrote: > Tables ddl are attached in dbfiddle -- Postgres 11 | db<>fiddle > Postgres 11 | db<>fiddle > Server configuration is: Version: 10.11RAM - 320GBvCPU - 32 "maintenance_work_mem" 256MB"work_mem"             1GB"shared_buffers" 64GB > Aggregate (cost=31.54..31.55 rows=1 width=8) (actual time=0.010..0.012 rows=1 loops=1) > -> Nested Loop (cost=0.00..31.54 rows=1 width=8) (actual time=0.007..0.008 rows=0 loops=1) > Join Filter: (a.household_entity_proxy_id = c.household_entity_proxy_id) > -> Nested Loop (cost=0.00..21.36 rows=1 width=16) (actual time=0.006..0.007 rows=0 loops=1) > Join Filter: (a.individual_entity_proxy_id = b.individual_entity_proxy_id) > -> Seq Scan on prospect a (cost=0.00..10.82 rows=1 width=16) (actual time=0.006..0.006 rows=0 loops=1) > Filter: (((last_contacted_anychannel_dttm IS NULL) OR (last_contacted_anychannel_dttm < '2020-11-23 00:00:00'::timestamp without time zone)) AND (shared_paddr_with_customer_ind = 'N'::bpchar) AND (profane_wrd_ind = 'N'::bpchar) AND (tmo_ofnsv_name_ind = 'N'::bpchar) AND (has_individual_address = 'Y'::bpchar) AND (has_last_name = 'Y'::bpchar) AND (has_first_name = 'Y'::bpchar)) > -> Seq Scan on individual_demographic b (cost=0.00..10.53 rows=1 width=8) (never executed) > Filter: ((tax_bnkrpt_dcsd_ind = 'N'::bpchar) AND (govt_prison_ind = 'N'::bpchar) AND ((cstmr_prspct_ind)::text = 'Prospect'::text)) > -> Seq Scan on household_demographic c (cost=0.00..10.14 rows=3 width=8) (never executed) > Filter: (((hspnc_lang_prfrnc_cval)::text = ANY ('{B,E,X}'::text[])) OR (hspnc_lang_prfrnc_cval IS NULL)) > Planning Time: 1.384 ms > Execution Time: 0.206 ms > 13 rows It's doing nested loops with estimated rowcount=1, which indicates a bad underestimate, and suggests that the conditions are redundant or correlated. Maybe you can handle this with MV stats on the correlated columns: CREATE STATISTICS prospect_stats (dependencies) ON shared_paddr_with_customer_ind, profane_wrd_ind, tmo_ofnsv_name_ind, has_individual_address, has_last_name, has_first_name FROM prospect; CREATE STATISTICS individual_demographic_stats (dependencies) ON tax_bnkrpt_dcsd_ind, govt_prison_ind, cstmr_prspct_ind FROM individual_demographic_stats ANALYZE prospect, individual_demographic_stats ; Since it's expensive to compute stats on large number of columns, I'd then check *which* are correlated and then only compute MV stats on those. This will show col1=>col2: X where X approaches 1, the conditions are highly correlated: SELECT * FROM pg_statistic_ext; -- pg_statistic_ext_data since v12 Also, as a diagnostic tool to get "explain analyze" to finish, you can SET enable_nestloop=off; -- Justin