I am new to PostgreSQL but isn't this query the same as doing an INNER
JIN?
For a true LEFT JIN should it not be as follows?
SELECT ITEM.ITEM_PK
FRM ITEM
LEFT JIN SERIAL_N N SERIAL_NITEM_FK = ITEM.ITEM_PK
AND SERIAL_NN ='WX1234'
GRUP BY ITEM.ITEM_PK
Using an AND instead of WHERE for the predicate on SERIAL_NN results
in very different plans despite the immature statistics. The following
plan is for the true LEFT JIN.
QUERY PLAN
HashAggregate (cost=2.102.13 rows=3 width=4)
-Hash Left Join (cost=1.042.10 rows=3 width=4)
Hash Cond: ("outer".item_pk = "inner".item_fk)
-Seq Scan on item (cost=0.001.03 rows=3 width=4)
-Hash (cost=1.041.04 rows=1 width=4)
-Seq Scan on serial_no (cost=0.001.04 rows=1 width=4)
Filter: (("no")::text = 'WX1234'::text)
(7 rows)
The next plan, which is very similary to your original plan, is for the
INNER JIN you described.
QUERY PLAN
HashAggregate (cost=2.112.12 rows=1 width=4)
-Nested Loop (cost=0.002.11 rows=1 width=4)
Join Filter: ("outer".item_fk = "inner".item_pk)
-Seq Scan on serial_no (cost=0.001.04 rows=1 width=4)
Filter: (("no")::text = 'WX1234'::text)
-Seq Scan on item (cost=0.001.03 rows=3 width=4)
(6 rows)
I wont speculate on how these plans would converge or diverge as the
tables grew and the statistics matured.
- Zulq Alam
T E Schmitz wrote:
SELECT ITEM.ITEM_PK FRM ITEM
LEFT JIN SERIAL_N N SERIAL_NITEM_FK = ITEM.ITEM_PK
WHERE SERIAL_NN ='WX1234'
GRUP BY ITEM.ITEM_PK
I ran an EXPLAIN:
HashAggregate (cost=1.061.06 rows=1 width=4)
-Nested Loop (cost=0.001.06 rows=1 width=4)
Join Filter: ("inner".item_fk = "outer".item_pk)
-Seq Scan on item (cost=0.000.00 rows=1 width=4)
-Seq Scan on serial_no (cost=0.001.05 rows=1 width=4)
Filter: (("no")::text = 'WX1234'::text)
(end of broadcast)
TIP 6: explain analyze is your friend