Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0pTB-0003TF-19 for pgsql-performance@arkaria.postgresql.org; Thu, 22 Aug 2019 15:57:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i0pT7-0001CI-Ve for pgsql-performance@arkaria.postgresql.org; Thu, 22 Aug 2019 15:57:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0pT7-000168-IJ for pgsql-performance@lists.postgresql.org; Thu, 22 Aug 2019 15:57:45 +0000 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0pT4-0003n5-8P for pgsql-performance@lists.postgresql.org; Thu, 22 Aug 2019 15:57:44 +0000 Received: by mail-io1-xd41.google.com with SMTP id q22so12883107iog.4 for ; Thu, 22 Aug 2019 08:57:41 -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=3BSR/Al0SIK9pHMJpQQextbWvuOQcsbRON4mnofbNA4=; b=pDDB//7btocFJNLzeKiBRB1yuCM1U0JpJnhhTW0mNofqPhaDv48wmb6DkIOz+Jl+Zl qz1Neh6oM0Uk0lRPK/0HLaz9fGlBr5ukjcwIW6KTgnBz5ETlgB3fDb5d87u6QJ4Hfewz XPvoNdw00Eypqm9XojQLdGEgRbhlaGVCrk7q6ncD+g5fUz5SpGR4w2ZEn3nxKsjwyiWr Ib5AFi1XNjk4X4a9VTmpXdMCRe92W6MQ31LSAi30LNGrafRMt7TrgprZ3subMPug+NRk pOivK84rbDAjT2X/IASyhsp10FbYtoajCzjfbapVxBCRLKvRbykZZ1gQPtZze+I53IfT SJcw== 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=3BSR/Al0SIK9pHMJpQQextbWvuOQcsbRON4mnofbNA4=; b=MzU8ltPfgSg/vxyBxhKW0TIkyMNtJR2nm6Iu5YXR8BPeK+DPMv5u7UcveBXE+ibf08 SycKdtu+lBbTAh2Gz+v+Sl9V808h8C6/cwq0wCRv5J0p64WX1rLg+OnR45BbuiD4HXLu taKUZqMlzUBLE6WcptIkuJVQifN459m9g8jLLLkS9CxERIs5Sxqu/pdO7b/dIBXTie86 x3z7mz85JUW2oSBE03eNGTAafLFDXJU+wzOOAZElqIYP4wOZbiiOglW19VBJx4EO7qzJ Q0/LnCQ6oMPlmoiq+q3nAnz9HPbDofS5L0J25/qjI0w+d5355YGouU4y9hhisqQz7hjJ ZnLQ== X-Gm-Message-State: APjAAAVvS5PtCdn9DqZedqhhF4z8CRSJYcy3XbOFTopG3mmSmSc2LCuM /+rJ3lQcN1dT/H7zKrWVpAgodc54mSc= X-Google-Smtp-Source: APXvYqwrkKvjAl47V5p91Zeh2FsJzYqEY22gM8JoIOdMcjZVxn9K5sIofH8ZUwiWe9O8sd+syG9VmA== X-Received: by 2002:a5e:9515:: with SMTP id r21mr19987037ioj.257.1566479952331; Thu, 22 Aug 2019 06:19:12 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id a7sm17056875iok.41.2019.08.22.06.19.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Aug 2019 06:19:11 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 97CD0800969; Thu, 22 Aug 2019 08:19:10 -0500 (CDT) Date: Thu, 22 Aug 2019 08:19:10 -0500 From: Justin Pryzby To: Marco Colli Cc: pgsql-performance@lists.postgresql.org Subject: Re: Extremely slow count (simple query, with index) Message-ID: <20190822131910.GC15332@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Aug 22, 2019 at 02:44:15PM +0200, Marco Colli wrote: > SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123; > SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123 AND "subscriptions"."trashed_at" IS NULL; > -> Bitmap Heap Scan on subscriptions (cost=199573.94..2055635.23 rows=4996823 width=0) (actual time=1666.409..16855.610 rows=4994254 loops=1) > Recheck Cond: (project_id = 123) > Rows Removed by Index Recheck: 23746378 > Heap Blocks: exact=131205 lossy=1480411 > -> Bitmap Index Scan on index_subscriptions_on_project_id_and_trashed_at (cost=0.00..198324.74 rows=4996823 width=0) (actual time=1582.717..1582.717 rows=4994877 loops=1) > -> Bitmap Heap Scan on subscriptions (cost=187953.70..2036810.19 rows=4428599 width=0) (actual time=1644.966..17078.378 rows=4994130 loops=1) > Recheck Cond: ((project_id = 123) AND (trashed_at IS NULL)) > Rows Removed by Index Recheck: 23746273 > Heap Blocks: exact=131144 lossy=1480409 > -> Bitmap Index Scan on index_subscriptions_on_project_id_and_trashed_at (cost=0.00..186846.55 rows=4428599 width=0) (actual time=1566.163..1566.163 rows=4994749 loops=1) You can see it used the same index in both cases, and the index scan was reasonably fast (compared to your goal), but the heap component was slow. I suggest to run VACUUM FREEZE on the table, to try to encourage index only scan. If that works, you should condider setting aggressive autovacuum parameter, at least for the table: ALTER TABLE subscriptions SET (AUTOVACUUM_VACUUM_SCALE_FACTOR=0.005); -- And possibly lower value of autovacuum_freeze_max_age Or, running manual vacuum possibly during quiet hours (possibly setting vacuum_freeze_table_age to encourage aggressive vacuum). > Even an approximate count would be enough. You can SELECT reltuples FROM pg_class WHERE oid='subscriptions'::oid, but its accuracy depends on frequency of vacuum (and if a large delete/insert happened since the most recent vacuum/analyze). Justin