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 1gyu6v-0000h4-Vr for pgsql-performance@arkaria.postgresql.org; Wed, 27 Feb 2019 07:58:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gyu6u-0005s4-1Z for pgsql-performance@arkaria.postgresql.org; Wed, 27 Feb 2019 07:58:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyu6t-0005rx-MW for pgsql-performance@lists.postgresql.org; Wed, 27 Feb 2019 07:58:35 +0000 Received: from smtpout.aon.at ([195.3.96.77]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyu6r-0004j6-9c for pgsql-performance@postgresql.org; Wed, 27 Feb 2019 07:58:34 +0000 Received: (qmail 23169 invoked from network); 27 Feb 2019 07:58:29 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on WARSBL506.highway.telekom.at X-Spam-Level: Received: from 100-65-57-125.rfc6598.a1.net (HELO localhost.localdomain) ([100.65.57.125]) (envelope-sender ) by smarthub80.res.a1.net (qmail-ldap-1.03) with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 27 Feb 2019 07:58:27 -0000 X-A1Mail-Track-Id: 1551254306:23114:smarthub80:100.65.57.125:1 Message-ID: <38d1ac81dfb700d154290aa35f513320d9fd093e.camel@cybertec.at> Subject: Re: Query slow for new participants From: Laurenz Albe To: "support@mekong.be" , Justin Pryzby Cc: pgsql-performance@postgresql.org In-Reply-To: References: <1551091278468-0.post@n3.nabble.com> <20190225161607.GQ28750@telsasoft.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Wed, 27 Feb 2019 08:58:17 +0100 User-Agent: Evolution 3.30.5 (3.30.5-1.fc29) Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk support@mekong.be wrote: > EXPLAIN (ANALYZE, BUFFERS), not just EXPLAIN > "Index Scan using ix_companyarticledb_company on companyarticledb (cost=0.57..2.80 rows=1 width=193) (actual time=1011.335..1011.454 rows=1 loops=1)" > " Index Cond: (companyid = 77)" > " Filter: (articleid = 7869071)" > " Rows Removed by Filter: 2674361" > " Buffers: shared hit=30287" > "Planning time: 0.220 ms" > "Execution time: 1011.502 ms" Your problem are the "Rows Removed by Filter: 2674361". The first thing I would try is: ALTER TABLE public.companyarticledb ALTER companyid SET STATISTICS 1000; ALTER TABLE public.companyarticledb ALTER articleid SET STATISTICS 1000; ANALYZE public.companyarticledb; Then PostgreSQL has a better idea which condition is selective. You can set STATISTICS up to 10000, but don't forget that high values make ANALYZE and planning slower. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com