pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using the correct index
2+ messages / 2 participants
[nested] [flat]

* Using the correct index
@ 2001-02-22 00:05  Jeremy Buchmann <jeremy@wellsgaming.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Jeremy Buchmann @ 2001-02-22 00:05 UTC (permalink / raw)
  To: pgsql-admin

I have a table I'll call SomeTable that has columns called id, date, and
name along with a few others.  I created two indexes for this table, one was
on id (called SomeTable_id) and the other was on id and date (called
SomeTable_id_date).  When I do:

EXPLAIN SELECT * FROM SomeTable WHERE id = '0101'

it tells me:

Index Scan using SomeTable_id_date on Sometable ...

Even when I do:

EXPLAIN SELECT id FROM SomeTable WHERE id = '0101'

it still says it's using the SomeTable_id_date index.  Why is it using the
multicolumn index when the date column isn't involved?

-- Jeremy  [jeremy@wellsgaming.com]





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Using the correct index
@ 2001-02-22 04:28  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jeremy Buchmann <jeremy@wellsgaming.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2001-02-22 04:28 UTC (permalink / raw)
  To: Jeremy Buchmann <jeremy@wellsgaming.com>; +Cc: pgsql-admin

Jeremy Buchmann <jeremy@wellsgaming.com> writes:
> I have a table I'll call SomeTable that has columns called id, date, and
> name along with a few others.  I created two indexes for this table, one was
> on id (called SomeTable_id) and the other was on id and date (called
> SomeTable_id_date).  When I do:
> EXPLAIN SELECT * FROM SomeTable WHERE id = '0101'
> it tells me:
> Index Scan using SomeTable_id_date on Sometable ...
> Even when I do:
> EXPLAIN SELECT id FROM SomeTable WHERE id = '0101'
> it still says it's using the SomeTable_id_date index.  Why is it using the
> multicolumn index when the date column isn't involved?

If the indexes are small then the cost estimates for scanning them will
be the same, and it's a random matter which one gets picked (in fact,
I suspect 7.0 may deliberately prefer the "more sorted" one).

Once the indexes get larger --- more than 100 pages or so --- the system
will notice that scanning the double-column index is more expensive, and
will avoid using it unless there's good reason.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-02-22 04:28 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-22 00:05 Using the correct index Jeremy Buchmann <jeremy@wellsgaming.com>
2001-02-22 04:28 ` Tom Lane <tgl@sss.pgh.pa.us>

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