How to optimize this simple query :-(

From: marten(at)feki(dot)toppoint(dot)de
To: pgsql-sql(at)postgreSQL(dot)org
Subject: How to optimize this simple query :-(
Date: 1999-11-21 11:12:00
Message-ID: 199911211112.MAA21246@feki.toppoint.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I've the follwoing two tables:

Table P3AT

AO CHAR(15)
AT Integer
AV VARCHAR(80)
AB LargeInteger

Table P3RL

RELS CHAR(15)
RELT CHAR(15)
SRCT Integer
and some other columns

Indices are on: P3AT.AO, P3AT.AT, P3RL.RELS, P3RL.RELT, P3RL.SRCT

Now I do the query:

a) SELECT AO,AT,AV FROM P3AT WHERE AO IN
(SELECT RELT FROM P3RL WHERE RELS='9#####3#####RW#' AND (SRCT=1004025))

or via

b) SELECT AO,AT,AV FROM P3AT WHERE EXISTS
(SELECT RELT FROM P3RL WHERE (RELT=AO) AND (RELS= ..) AND ...)

Both statements have the same explain result:

Seq Scan on p3at (cost=14458.84 rows=327480 width=28)
SubPlan
-> Index Scan using reltrgind om p3rl (cost=2.05 rows=1 width=12)

Both statements need about 3300 milliseconds to do the job ... and return the
three result rows ...

I gave it another sql-query and this works much better:

c) SELECT AO,AT,AV FROM P3AT,P3RL WHERE
(P3AT.AO=P3RL.RELT) AND (RELS= ...) AND (SRCT= ...)

This gives the following explain result (and the one I would have expected):

NestedLoop (cost=4.1 rows=5 width=40)
-> Index Scan using relsrcind on p3rl (cost=2.05 rows=1 width=12)
-> Index Scan using atrownind on p3at (cost=2.05 rows=327480 width=28)

and needs only 5 ms !

Marten

Browse pgsql-sql by date

  From Date Subject
Next Message Hiroshi Inoue 1999-11-22 01:53:28 RE: [SQL] Deleting indexes before vacuum?
Previous Message Oleg Bartunov 1999-11-20 19:44:24 Re: [SQL] Deleting indexes before vacuum?