Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rexFR-00EVMx-GU for pgsql-performance@arkaria.postgresql.org; Tue, 27 Feb 2024 13:11:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rexFP-002yZ0-O5 for pgsql-performance@arkaria.postgresql.org; Tue, 27 Feb 2024 13:11:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rexFO-002yYr-SE for pgsql-performance@lists.postgresql.org; Tue, 27 Feb 2024 13:11:51 +0000 Received: from fout2-smtp.messagingengine.com ([103.168.172.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rexFM-001Sgj-AG for pgsql-performance@lists.postgresql.org; Tue, 27 Feb 2024 13:11:50 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfout.nyi.internal (Postfix) with ESMTP id 5C01C13800B0; Tue, 27 Feb 2024 08:11:46 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 27 Feb 2024 08:11:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1709039506; x= 1709125906; bh=GvMi2ThtmNB8as8mth4X84dv6NV1lPUBiIH/DGaTNcU=; b=O uhg84LqRXh/I3Zl5SCDSTJ767LWgZxJqDv+RrR19tvPo7AAF/aY1Rw+qIyQk0wjs 09vGdKeYprCmh1+Fn+lhtYZgpSO7/ZMe5S43bPPZGdZPBF49zdLj3JlyZVMCW40r 8h4stNBm23MC2viQ3FsCjVM0S4ZqfWRF+D9gr0To5ncV5etmTAN6rV7bFyK9NMnV O15x4ADDRSlf31m5/k70xH3pRXtpbjX37wa0MeB9PasT92ttbfB5AugwIz8gF+Af bt+jhGfrhjdp+UBzW5aa8ziKiMutCSFaNwiWtvqemmB59KKsAp+7T7t0vOhEooLA vBWE3deTqMWCMFpORZiaQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrgeehgddvgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepfffhvfevuffkgggtugfgjgesthekre dttddtjeenucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgv segrlhhvhhdrnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhephfektedvveethf fghfdtgeefteekueefhfehteehhedtvdduffekfeehheettddvnecuffhomhgrihhnpehp ohhsthhgrhgvshhqlhdrvghupdgvnhhtvghrphhrihhsvggusgdrtghomhenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvges rghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 27 Feb 2024 08:11:45 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1709039502; bh=CCQTmch9j1o8hBvFJhVI1+AINRAh6z9ZthQreLJBLAY=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=QbU3Ea50x82sqcD6gIwlnqSHitYP3WxX13IbaFdRW0arTOe/rTtrFCi1q1Fk34G3r vHiCIcano2G+q1W0748smXi4tZTT6k5ENEXoQdjfQMwD4/ZinsQDnSF349UhZgd4yd sRB56kAlZWLme9Ya513VQ5T+znl7gKkIgK5CTjkwIjL+lwhHVYNtzJsLwj8+l9KJWu OVESWm5UXSTx4dgFIRd2kInhFLL1tSGdIRqM4+E/IcsISU3OWWAJq/7TPtN/WZQRb2 YZYd+UN/aRHphhJObXbTOT7L4da7J3Io+VURbkDTHpQMZoqU8MHJr/2RpgEgkYCErM EIM9+o3y7XHrw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id B26941298; Tue, 27 Feb 2024 14:11:42 +0100 (CET) Date: Tue, 27 Feb 2024 14:11:42 +0100 From: Alvaro Herrera To: Chema Cc: pgsql-performance@lists.postgresql.org Subject: Re: Optimizing count(), but Explain estimates wildly off Message-ID: <202402271311.qd7z46bod64u@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Chema, On 2024-Feb-26, Chema wrote: > Dear pgsqlers, > > I'm trying to optimize simple queries on two tables (tenders & items) with > a couple million records. Besides the resulting records, the app also > displays the count of total results. Doing count() takes as much time as > the other query (which can be 30+ secs), so it's an obvious target for > optimization. I'm already caching count() results for the most common > conditions (country & year) in a material table, which practically halves > response time. The tables are updated sparingly, and only with bulk > COPYs. Now I'm looking for ways to optimize queries with other conditions. It sounds like this approach might serve your purposes: https://www.postgresql.eu/events/pgconfeu2023/schedule/session/4762-counting-things-at-the-speed-of-light-with-roaring-bitmaps/ > I already raised default_statistics_target up to 2k (the planner wasn't > using indexes at all with low values). Gotta get it even higher? These are > my custom settings: I would recommend to put the default_statistics_target back to its original value and modify the value with ALTER TABLE .. SET STATISTICS only for columns that need it, only on tables that need it; then ANALYZE everything. The planner gets too slow if you have too many stats for everything. > shared_buffers = 256MB # min 128kB This sounds far too low, unless your server is a Raspberry Pi or something. See "explain (buffers, analyze)" of your queries to see how much buffer traffic is happening for them. > Functions: 33 > Options: Inlining true, Optimization true, Expressions true, Deforming true > Timing: Generation 14.675 ms, Inlining 383.349 ms, Optimization 1023.521 > ms, Emission 651.442 ms, Total 2072.987 ms > Execution Time: 63378.033 ms Also maybe experiment with turning JIT off. Sometimes it brings no benefit and slows down execution pointlessly. Here you spent two seconds JIT-compiling the query; were they worth it? Cheers -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ Syntax error: function hell() needs an argument. Please choose what hell you want to involve.