pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Daniel Blanch Bataller <daniel.blanch.bataller@gmail.com>
To: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Eric Jiang <eric@doublemap.com>
Cc: pgsql-performance@postgresql.org, Eric Clark <eclark@doublemap.com>
Subject: Re: Querying with multicolumn index
Date: Sun, 11 Dec 2016 07:04:45 +0100
Message-ID: <36867AC4-DDD8-464E-A0F7-066B4C88300C@gmail.com> (raw)
In-Reply-To: <b92cec0d-c4b2-5e44-a6b9-949d2b93e935@2ndquadrant.com>
References: <CAOfJSTwzQ7Fx6Yjeg9mFkMsM5OVKPoa=EgkHceGdkr1TWg8vkA@mail.gmail.com>
	<5353.1481327501@sss.pgh.pa.us>
	<b92cec0d-c4b2-5e44-a6b9-949d2b93e935@2ndquadrant.com>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

Hi all,

Thomas is absolutely right, the distribution I synthetically made, had 6M records but very old, 9M old, as you can see it had to skip 9M records before finding a suitable record using time index. 

EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM updates WHERE driver_id = 100 ORDER BY "time" DESC LIMIT 1;
                                                                         QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.44..0.65 rows=1 width=36) (actual time=3827.807..3827.807 rows=1 loops=1)
   Buffers: shared hit=24592 read=99594 written=659
   ->  Index Scan Backward using updates_time_idx on updates  (cost=0.44..1284780.53 rows=6064800 width=36) (actual time=3827.805..3827.805 rows=1 loops=1)
         Filter: (driver_id = 100)
         Rows Removed by Filter: 9000000
         Buffers: shared hit=24592 read=99594 written=659
 Planning time: 0.159 ms
 Execution time: 3827.846 ms
(8 rows)


Here you have my tests where I was able to reproduce the problem using default settings on 9.6, 9.5 and 9.3. 9.6 and 9.5 choose the wrong index, while 9.3 didn’t. (update: 9.5 didn’t fail last time) 


However when I tried to add more than one value with this strange distribution ~ 30% of distribution to one value the index bad choice problem didn’t happen again in none of the different versions.

I Hope this helps. Regards,

Daniel Blanch.


> El 10 dic 2016, a las 21:34, Tomas Vondra <tomas.vondra@2ndquadrant.com> escribió:
> 
> Hi,
> 
> On 12/10/2016 12:51 AM, Tom Lane wrote:
>> Eric Jiang <eric@doublemap.com> writes:
>>> I have a query that I *think* should use a multicolumn index, but
>>> sometimes isn't, resulting in slow queries.
>> 
>> I tried to duplicate this behavior, without success.  Are you running
>> with nondefault planner parameters?
>> 
> 
> My guess is this is a case of LIMIT the matching rows are uniformly distributed in the input data. The planner likely concludes that for a driver with a lot of data we'll find the first row using ix_updates_time very quickly, and that it will be cheaper than inspecting the larger multi-column index. But imagine a driver with a lots of data long time ago. That breaks the LIMIT fairly quickly.
> 
> regards
> 
> -- 
> Tomas Vondra                  http://www.2ndQuadrant.com
> PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
> 
> 
> -- 
> Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-performance

-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

Attachments:

  test_bad_index_choice.sql (application/octet-stream, 1.4K) download

  bad_idx_choice.9.6.out (application/octet-stream, 4.7K) download

  bad_idx_choice.9.5.out (application/octet-stream, 5.1K) download

  bad_idx_choice.9.3.out (application/octet-stream, 5.0K) download

view thread (12+ messages)  latest in thread

Message-ID: <36867AC4-DDD8-464E-A0F7-066B4C88300C@gmail.com>
Permalink:  ../36867AC4-DDD8-464E-A0F7-066B4C88300C@gmail.com/
Also on:    postgresql.org/message-id/36867AC4-DDD8-464E-A0F7-066B4C88300C@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: daniel.blanch.bataller@gmail.com, tomas.vondra@2ndquadrant.com, tgl@sss.pgh.pa.us, eric@doublemap.com, eclark@doublemap.com
  Subject: Re: Querying with multicolumn index
  In-Reply-To: <36867AC4-DDD8-464E-A0F7-066B4C88300C@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox