Re: Re: Unable to understand index only scan as it is not happening for one table while it happens for other

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "'rajan *EXTERN*'" <vgmonnet(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Re: Unable to understand index only scan as it is not happening for one table while it happens for other
Date: 2017-06-27 15:36:20
Message-ID: A737B7A37273E048B164557ADEF4A58B53A5F2CF@ntex2010i.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

rajan wrote:
> why the index-only scan *works only* with an *order by*?
> localdb=# explain analyse verbose select uid from mm where uid>100 *order
> by* uid;
> QUERY
> PLAN
> --------------------------------------------------------------------------------
> Index Only Scan using mm_pkey on public.mm (cost=0.27..22.47 rows=354 width=8)
> (actual time=0.023..0.079 rows=354 loops=1)
> Output: uid
> Index Cond: (mm.uid > 100)
> Heap Fetches: 0
> Planning time: 0.096 ms
> Execution time: 0.131 ms
> (6 rows)

I'd guess that it would work fine, but PostgreSQL chooses to use a sequential
scan instead, because too many rows meet the condition "uid > 100".

If you add the ORDER BY, the plan with the sequential scan also has to
sort the data, which makes it much more expensive, while the index only scan
returns the data in sorted order anyway and does not have to sort,
which makes it cheaper.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Westermann 2017-06-27 18:19:21 Re: Which process is actually doing the WAL writes/calls XLogFlush?
Previous Message Andrew Sullivan 2017-06-27 13:28:43 Re: Config for fast huge cascaded updates