Re: Which index can i use ?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Abandoned <besturk(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Which index can i use ?
Date: 2007-10-30 07:41:09
Message-ID: 4726E015.8080804@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Abandoned wrote:
> Hi..
> I want to do index in postgresql & python.
> My table:
> id(int) | id2(int) | w(int) | d(int)
>
> My query:
> select id, w where id=x and id2=y (sometimes and d=z)
>
> I have too many insert and select operation on this table.
> And which index type can i use ? Btree, Rtree, Gist or Hash ?
> Also I want to unique (id, id2)..

OK, well a UNIQUE constraint automatically gives you an index, so
(id,id2) are already indexed.

If you add your own indexes, just use btree (or don't specify anything
and let PostgreSQL choose btree for you). The others are all for
specialised uses.

> Now this is my index. is it give me good performance ?
> CREATE UNIQUE INDEX ind1 ON test USING btree (id, id2)
> CREATE INDEX ind2 ON test USING btree (id)
> CREATE INDEX ind3 ON test USING btree (id2)
> CREATE INDEX ind4 ON test USING btree (w)
> CREATE INDEX ind5 ON test USING btree (d)

This is a lot of indexes. Every index will slow down inserts and updates
(the index will need to be updated).

So - start with the unique index and see how that works for you. If you
find problems with some queries look at using EXPLAIN ANALYZE to see the
plan your slow query is using, then we can try again.

Oh, and make sure autovacuum is running to keep the planner informed of
changes in your database.

HTH

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2007-10-30 07:44:57 Re: PostgreSQL .msi Installation Failure
Previous Message Scott Marlowe 2007-10-30 07:38:44 Re: postgres bash prompt error while user creation