Optimal indicies

From: Oleg Broytmann <phd(at)sun(dot)med(dot)ru>
To: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Optimal indicies
Date: 1999-04-16 13:05:07
Message-ID: Pine.SOL2.3.96.SK.990416164741.5074A-100000@sun.med.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello!

It is definitely FAQ, but I haven't found an answer in docs or mailing
list.
How do I create indicies? (No, no, I know CREATE INDEX syntax :) But how
do I plan my indicies that optimizer will use? I expected a topic in
postgres guides, but there is no one. Any general advice?

For example, here is a table (I am not showing here other tables, I
think they are not important):

-- Position's rating for today for every city
CREATE TABLE pos_rating (
pos_id int4 not null references positions (pos_id),
date_i date default current_date,
city_id int2 not null references cities (city_id),
rating float default 5 check (rating >= 1 and rating <= 10),
primary key (pos_id, date_i, city_id)
);

Currently, there is only one index for primary key.

And here is a query cost. Index is not used. What index I need to add?
Do I need to rewrite queries (using EXIST instaed of IN, as it once
suggested)?

EXPLAIN SELECT pos_id, rating FROM pos_rating
WHERE pos_id IN
(SELECT pos_id FROM positions WHERE subsec_id = 1 AND status = 'A')
AND date_i = current_date AND city_id = 2
ORDER BY rating ;

NOTICE: QUERY PLAN:

Sort (cost=236.43 size=0 width=0)
-> Seq Scan on pos_rating (cost=236.43 size=5 width=12)
SubPlan
-> Seq Scan on positions (cost=3.11 size=3 width=4)

Oleg.
----
Oleg Broytmann http://members.xoom.com/phd2/ phd2(at)earthling(dot)net
Programmers don't die, they just GOSUB without RETURN.

Browse pgsql-general by date

  From Date Subject
Next Message Oleg Broytmann 1999-04-16 13:14:30 PostgreSQL WWW: Software
Previous Message José Soares 1999-04-16 12:27:19 Re: [GENERAL] Does Postgres supoort this type?