Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bp2yb-0000Vf-UZ for pgsql-performance@arkaria.postgresql.org; Wed, 28 Sep 2016 00:43:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bp2yb-0007UH-Gr for pgsql-performance@arkaria.postgresql.org; Wed, 28 Sep 2016 00:43:57 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bp2ww-0005QJ-0i for pgsql-performance@postgresql.org; Wed, 28 Sep 2016 00:42:14 +0000 Received: from aibo.runbox.com ([91.220.196.211]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bp2ws-0005D4-Ie for pgsql-performance@postgresql.org; Wed, 28 Sep 2016 00:42:13 +0000 Received: from [10.9.9.212] (helo=mailfront12.runbox.com) by bars.runbox.com with esmtp (Exim 4.71) (envelope-from ) id 1bp2wr-0001DS-FH for pgsql-performance@postgresql.org; Wed, 28 Sep 2016 02:42:09 +0200 Received: from cpe-66-27-125-35.san.res.rr.com ([66.27.125.35] helo=seasyslap4) by mailfront12.runbox.com with esmtpsa (uid:561468 ) (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.82) id 1bp2wp-00076l-5Y for pgsql-performance@postgresql.org; Wed, 28 Sep 2016 02:42:07 +0200 From: "Mike Sofen" To: References: In-Reply-To: Subject: Re: Unexpected expensive index scan Date: Tue, 27 Sep 2016 17:41:55 -0700 Message-ID: <00e901d21921$1e647180$5b2d5480$@runbox.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00EA_01D218E6.72074730" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQJqaizE6AYNkKf1PF1s+az2031ZTwFZAL7Cn1KhGwA= Content-Language: en-us X-Pg-Spam-Score: -4.6 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org This is a multipart message in MIME format. ------=_NextPart_000_00EA_01D218E6.72074730 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: Jake Nielsen Sent: Tuesday, September 27, 2016 5:22 PM the query SELECT * FROM SyncerEvent WHERE ID > 12468 AND propogatorId NOT IN = ('"d8130ab9!-66d0!-4f13!-acec!-a9556362f0ad"') AND conflicted !=3D 1 AND = userId =3D '57dc984f1c87461c0967e228' ORDER BY ID LIMIT 4000;^ =20 On Tue, Sep 27, 2016 at 5:02 PM, Jake Nielsen > wrote: I've got a query that takes a surprisingly long time to run, and I'm = having a really rough time trying to figure it out. =20 Before I get started, here are the specifics of the situation: =20 Here is the table that I'm working with (apologies for spammy indices, = I've been throwing shit at the wall) Table "public.syncerevent" Column | Type | Modifiers = =20 --------------+---------+------------------------------------------------= ---------- id | bigint | not null default = nextval('syncerevent_id_seq'::regclass) userid | text |=20 event | text |=20 eventid | text |=20 originatorid | text |=20 propogatorid | text |=20 kwargs | text |=20 conflicted | integer |=20 Indexes: "syncerevent_pkey" PRIMARY KEY, btree (id) "syncereventidindex" UNIQUE, btree (eventid) "anothersyncereventidindex" btree (userid) "anothersyncereventidindexwithascending" btree (userid, id) "asdfasdgasdf" btree (userid, id DESC) "syncereventuseridhashindex" hash (userid) =20 To provide some context, as per the wiki,=20 there are 3,290,600 rows in this table.=20 It gets added to frequently, but never deleted from.=20 The "kwargs" column often contains mid-size JSON strings (roughly 30K = characters on average) As of right now, the table has 53 users in it. About 20% of those have a = negligible number of events, but the rest of the users have a fairly = even smattering. =20 EXPLAIN (ANALYZE, BUFFERS) says: = QUERY PLAN = =20 -------------------------------------------------------------------------= -------------------------------------------------------------------------= ------------ Limit (cost=3D0.43..1218.57 rows=3D4000 width=3D615) (actual = time=3D3352.390..3403.572 rows=3D4000 loops=3D1) Buffers: shared = hit=3D120244 read=3D160198 -> Index Scan using syncerevent_pkey on syncerevent = (cost=3D0.43..388147.29 rows=3D1274560 width=3D615) (actual = time=3D3352.386..3383.100 rows=3D4000 loops=3D1) Index Cond: (id > 12468) Filter: ((propogatorid <> = '"d8130ab9!-66d0!-4f13!-acec!-a9556362f0ad"'::text) AND (conflicted <> = 1) AND (userid =3D '57dc984f1c87461c0967e228'::text)) Rows Removed by Filter: 1685801 Buffers: shared hit=3D120244 read=3D160198 Planning time: 0.833 ms Execution time: 3407.633 ms (9 rows) If it matters/interests you, here is my underlying confusion: