Re: database not using indexes

From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: "Ruslan A(dot) Bondar" <fsat(at)list(dot)ru>
Cc: Silvio Brandani <silvio(dot)brandani(at)tech(dot)sdb(dot)it>, pgsql-admin(at)postgresql(dot)org
Subject: Re: database not using indexes
Date: 2011-11-10 02:40:58
Message-ID: 4EBB39BA.5090102@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On 09/11/11 23:58, Ruslan A. Bondar wrote:
>
> Why have you decided it isn't using indexes?
> If index exists - postgres will use it.

Actually that's not necessarily the case.

PostgreSQL will only use an index if (a) the index can be used for that
kind of query and (b) using the index will be faster than doing a
sequential scan.

If a query requires all the data in a table, PostgreSQL is quite likely
to do a sequential scan of the table, because it'll need to read every
block anyway. Reading just the table (without reading the index) in
order is much faster than reading the index then doing semi-random reads
of the table.

Additionally, not all indexes can be used for all operations. For
example, a LIKE query with a prefix wildcard eg "%FRED" cannot use a
btree index, so any btree index on the searched field will be ignored.

--
Craig Ringer

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Craig Ringer 2011-11-10 02:43:08 Re: database not using indexes
Previous Message Craig Ringer 2011-11-10 02:36:55 Re: How to deal with corrupted database?