indexing for left join

From: T E Schmitz <mailreg(at)numerixtechnology(dot)de>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: indexing for left join
Date: 2006-01-19 15:41:25
Message-ID: 43CFB325.9060306@numerixtechnology.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have two tables:

TABLE ITEM
(
ITEM_PK serial,
RETAIL_PRICE numeric (7,2) NOT NULL,
...
PRIMARY KEY (ITEM_PK)
)

TABLE SERIAL_NO
(
SERIAL_NO_PK serial,
NO varchar (20) NOT NULL,
NAME varchar (20),
ITEM_FK integer NOT NULL,
PRIMARY KEY (SERIAL_NO_PK)
);

common query:

SELECT ITEM.ITEM_PK FROM ITEM
LEFT JOIN SERIAL_NO ON SERIAL_NO.ITEM_FK = ITEM.ITEM_PK
WHERE SERIAL_NO.NO ='WX1234'
GROUP BY ITEM.ITEM_PK

Table ITEM will eventually grow very big and SERIAL_NO will grow with
it. There will normally be zero or one SERIAL_NO per ITEM; few ITEMs
will have more than one SERIAL_NO.

I have created an index for SERIAL_NO.NO and one for SERIAL_NO.ITEM_FK
for the above query.

I ran an EXPLAIN:
HashAggregate (cost=1.06..1.06 rows=1 width=4)
-> Nested Loop (cost=0.00..1.06 rows=1 width=4)
Join Filter: ("inner".item_fk = "outer".item_pk)
-> Seq Scan on item (cost=0.00..0.00 rows=1 width=4)
-> Seq Scan on serial_no (cost=0.00..1.05 rows=1 width=4)
Filter: (("no")::text = 'WX1234'::text)

Sequential despite the indices? Or is this because the tables of my test
DB are virtually empty?

Many thanks in advance.
--

Regards,

Tarlika Elisabeth Schmitz

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2006-01-19 15:52:45 Re: indexing for left join
Previous Message Juris 2006-01-19 13:06:05 Re: Error calling self-made plpgsql function "function XYZ(bigint) does not exist"