Re: subquery vs join on 7.4.5 - Mailing list pgsql-performance

From Tom Lane
Subject Re: subquery vs join on 7.4.5
Date
Msg-id 20117.1109177692@sss.pgh.pa.us
Whole thread Raw
In response to subquery vs join on 7.4.5  (David Haas <dave@modelpredictivesystems.com>)
List pgsql-performance
David Haas <dave@modelpredictivesystems.com> writes:
> I'm comparing the speeds of the following two queries on 7.4.5.  I was
> curious why query 1 was faster than query 2:

> query 1:
> Select layer_number
> FROM batch_report_index
> WHERE device_id = (SELECT device_id FROM device_index WHERE device_name
> ='CP8M')
> AND technology_id = (SELECT technology_id FROM technology_index WHERE
> technology_name = 'G12');

> query 2:
> Select b.layer_number
> FROM batch_report_index b, device_index d, technology_index t
> WHERE b.device_id = d.device_id
> AND b.technology_id = t.technology_id
> AND d.device_name = 'CP8M'
> AND t.technology_name = 'G12';

Why didn't you try a two-column index on batch_report_index(device_id,
technology_id) ?

Whether this would actually be better than a seqscan I'm not sure, given
the large number of matching rows.  But the planner would surely try it
given that it's drastically underestimating that number :-(

            regards, tom lane

pgsql-performance by date:

Previous
From: Tom Lane
Date:
Subject: Re: Inefficient Query Plans
Next
From: G u i d o B a r o s i o
Date:
Subject: Re: Problem with 7.4.5 and webmin 1.8 in grant function