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 1jW5EP-0003wO-TW for pgsql-performance@arkaria.postgresql.org; Tue, 05 May 2020 21:36:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jW5EO-0005Qg-Nk for pgsql-performance@arkaria.postgresql.org; Tue, 05 May 2020 21:36:00 +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 1jW5EO-0005OE-Ax for pgsql-performance@lists.postgresql.org; Tue, 05 May 2020 21:36:00 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jW5EL-0002TZ-M8 for pgsql-performance@postgresql.org; Tue, 05 May 2020 21:35:59 +0000 Received: by mail-qk1-x741.google.com with SMTP id t3so436122qkg.1 for ; Tue, 05 May 2020 14:35:57 -0700 (PDT) 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:in-reply-to:user-agent; bh=J1Pq6ddNXku+rCva46k/NTz709kgEF4Mk1m2cSZoWGQ=; b=JH+3e3RqvSup+80IigqAWpetM9jhmwDb275RYwFCcwuCTRzJR2MgixrOa3TC4GHDfV cPqygCv3U8uqXd01Mn1LQ6v6sx9c5kN8996m8v4mKXyACVvr5QLLYdRYdEv6s3JVkiul FalW/TAIXAjb7+EeRWnx6gk/btfCH4Sjz4n6wMwS/iAQ6Vv7U7XwyiWFJGzSpwj98oIn 1aH38FJwKBeA7QVbGKFwVd/lq7wxBcBdzE5KBOq+JjTXOMAQFISl8oHH6eBrNiYgUNWC gWrRgjvacxz28B70byAE+zHhgY1DH9BCPRP0x8aIWOuljv6EgHvrk+a8bua0PcCwliJc z+Lg== 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:in-reply-to:user-agent; bh=J1Pq6ddNXku+rCva46k/NTz709kgEF4Mk1m2cSZoWGQ=; b=rCd+VYtrTREUw4gN4pllKqg6CW+/XHdpsFvB3GAA6FMEv+c4xS1NRhT8UHoTebHIU+ H4um8v8paBEumE6TxzXsLEqvbnil5VodJbRR6YKHEPX89Go2DTaqLXjQeHtHBwrS+uwq mvUY8Gq8s27dDaqY1EK/uu/aXu4h0yUGMv9AwnMrNWZXxPxDl11ISDPAqHl7eLkS6Tes MVSN/ppYo5VwHH7jnVPe5HZ7YHfOcc4OsLXLPL+ez2amP3KZnnu9PoMKyY/Kh1UYyDbw 89cmTIDboBCNKjcu7ObS/e7B5ew+o1Vu1KsFAZZlj1RvMC9ylKSQe0+HLaD7BGNHmCIh XnwQ== X-Gm-Message-State: AGi0PuaPdxsZl9t1e2qGZRTNvb+o+9RKrYGcdP7UwflBbfy5MB6y1/DF gSyXg0fC1pbMU5Mjy2SMy6jFew== X-Google-Smtp-Source: APiQypKPZN7MBxQqyM1x0oI94hidptDce29538Gh7KBhS1YMK6jGdkUoyFWZGxO5x00T2QbDjMTWEA== X-Received: by 2002:a37:7002:: with SMTP id l2mr5705211qkc.372.1588714556596; Tue, 05 May 2020 14:35:56 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id x24sm2865959qth.80.2020.05.05.14.35.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 May 2020 14:35:55 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 1D877800C7F; Tue, 5 May 2020 16:35:54 -0500 (CDT) Date: Tue, 5 May 2020 16:35:54 -0500 From: Justin Pryzby To: James Thompson Cc: pgsql-performance@postgresql.org, laurenz.albe@cybertec.at, dgrowleyml@gmail.com Subject: Re: Please help! Query jumps from 1s -> 4m Message-ID: <20200505213553.GF28974@telsasoft.com> References: <20200503153825.GK28974@telsasoft.com> <20200504191201.GU28974@telsasoft.com> <20200505192836.GD28974@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, May 05, 2020 at 10:10:18PM +0100, James Thompson wrote: > I've managed to replicate this now with prepared statements. Thanks for all > the guidance so far. > > The slowness occurs when the prepared statement changes to a generic plan. > > Initial plan: > -> Index Only Scan using table1_typea_include_uniqueid_col16_idx on table1 table1alias2 (cost=0.56..2549.70 rows=70 width=36) (actual time=1.901..45.256 rows=65000 loops=1) > Output: table1alias2.uniqueid > Index Cond: ((table1alias2.col20 = '12345'::bigint) AND (table1alias2.col8 = ANY ('{c5986b02-3a02-4639-8147-f286972413ba,...98ed24b1-76f5-4b0e-bb94-86cf13a4809c}'::text[]))) The rowcount is off by a factor of 1000x. > after 5 executions of the statement: > -> Index Only Scan using table1_typea_include_uniqueid_col16_idx on table1 table1alias2 (cost=0.56..17.23 rows=1 width=36) (actual time=125.344..126877.822 rows=65000 loops=1) > Output: table1alias2.uniqueid > Index Cond: (table1alias2.col20 = $1001) > Filter: ((table1alias2.col8)::text = ANY ((ARRAY[$1, ...,$1000])::text[])) > Rows Removed by Filter: 2670023 And the generic plan is cheaper than the previous, custom plan; but slower, undoubtedly due to rowcount mis-estimate. > The second plan looks worse to me as it's applying a filter rather than > using an index condition? I don't understand why it's not part of the > condition and also why this is so much slower though. > If I force a retrieval of all index rows for col20 = '12345' using an > ad-hoc query (below, which in my mind is what the 'bad' plan is doing), > that only takes 2s (2.7 mil rows). Where's the difference? > > EXPLAIN (ANALYZE, BUFFERS, TIMING) SELECT COUNT(DISTINCT id) FROM table1 > WHERE datatype='TypeA' AND col20 = 12345; > -> Index Only Scan using table1_typea_include_uniqueid_col16_idx on table1 > (cost=0.56..2762.95 rows=55337 width=8) (actual time=3.976..1655.645 rows=2735023 loops=1) I see you're querying on datetype, which I think isn't in the original query (but I'm not sure if it's just renamed?). Underestimate usually means that the conditions are redundant or correlated. You could mitigate that either by creating an index on both columns (or add datatype to the existing index), or CREATE STATISTICS ndistinct on those columns. Or maybe you just need to increase the stats target for col20 (?). Then re-analyze. -- Justin