Re: I'd like to learn a bit more about how indexes work

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mike Christensen <mike(at)kitchenpc(dot)com>
Cc: chris(at)chriscurvey(dot)com, pgsql-general(at)postgresql(dot)org
Subject: Re: I'd like to learn a bit more about how indexes work
Date: 2012-06-06 14:05:55
Message-ID: 10247.1338991555@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mike Christensen <mike(at)kitchenpc(dot)com> writes:
> Thanks! One thing that still confuses me is the difference between IN
> and OR. With this query:

> explain analyze
> select *
> from foobar
> where d in (500, 750);

> It scans the d index only once:

> 'Bitmap Heap Scan on foobar (cost=10.03..400.63 rows=196 width=16)
> (actual time=0.128..0.489 rows=200 loops=1)'
> ' Recheck Cond: (d = ANY ('{500,750}'::integer[]))'
> ' -> Bitmap Index Scan on d_idx (cost=0.00..9.98 rows=196 width=0)
> (actual time=0.086..0.086 rows=200 loops=1)'
> ' Index Cond: (d = ANY ('{500,750}'::integer[]))'
> 'Total runtime: 0.592 ms'

Actually, that's two indexscans --- the btree code is aware that it has
to perform an indexscan for each element of the =ANY array, and add all
the resulting bits to the output bitmap. (The bitmap takes care of
eliminating any duplicate hits, which is why this type of index
condition is only supported for bitmap scans and not plain indexscans.)

So this isn't really any different from the OR case in terms of what
happens while probing the index. It's marginally more efficient in
that we save an explicit BitmapOr step, but only marginally.

As for why these cases are treated differently, yeah that's historical
to some extent, but it's also true that trying to convert one notation
to the other would be expensive and often fruitless.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Frank Lanitz 2012-06-06 14:33:53 pg_database_size differs from df -s
Previous Message Peter Geoghegan 2012-06-06 13:37:14 Re: Problem while restoring a database from SQL_ASCII to UTF-8