Unnecessary repeat condition for a self inner join

From: "Robins Tharakan" <tharakan(at)gmail(dot)com>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Unnecessary repeat condition for a self inner join
Date: 2008-07-11 16:26:26
Message-ID: 36af4bed0807110926w1fe444b3i5b2bd12effb958e3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I am not sure if this is a simple (... stupid) question but I just wasted
two hours optimizing a query, so I thought I should drop in to ask.

The only difference between query1 and query2 (below) is that despite an
explicit INNER JOIN, I have repeated the same condition for n2 (as given for
n1) and this makes a whole lot of difference in performance (since it now
uses the same index for n2 that it is using for n1).

In case of an INNER JOIN, shouldn't the second condition (in Query2) be
unnecessary ?
Or am I being unreasonable in this expectation ?

Regards,
*Robins Tharakan*

p.s.: The query below is just a simplification, and provides only EXPLAIN,
but I think an EXPLAIN ANALYSE should be unnecessary here. In case anyone
still needs it, please do tell.

*Query 1*:

SELECT n1.scheme_code
FROM nav n1
INNER JOIN nav n2 ON n1.scheme_code = n2.scheme_code
WHERE n1.scheme_code BETWEEN 100 AND 200

"Merge Join (cost=903471.23..10248343.37 rows=622920912 width=4)"
" Merge Cond: (n1.scheme_code = n2.scheme_code)"
" -> Sort (cost=110929.32..111458.60 rows=211712 width=4)"
" Sort Key: n1.scheme_code"
" -> Bitmap Heap Scan on nav n1 (cost=8623.86..92201.54 rows=211712
width=4)"
" Recheck Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"
" -> Bitmap Index Scan on pk_fs_nav (cost=0.00..8570.94
rows=211712 width=0)"
" Index Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"
" -> Sort (cost=792541.91..805391.17 rows=5139702 width=4)"
" Sort Key: n2.scheme_code"
" -> Seq Scan on nav n2 (cost=0.00..131799.02 rows=5139702
width=4)"

*Query 2*:

SELECT n1.scheme_code
FROM nav n1
INNER JOIN nav n2 ON n1.scheme_code = n2.scheme_code
WHERE n1.scheme_code BETWEEN 100 AND 200
AND n2.scheme_code BETWEEN 100 AND 200

"Merge Join (cost=221858.63..607790.72 rows=25659043 width=4)"
" Merge Cond: (n2.scheme_code = n1.scheme_code)"
" -> Sort (cost=110929.32..111458.60 rows=211712 width=4)"
" Sort Key: n2.scheme_code"
" -> Bitmap Heap Scan on nav n2 (cost=8623.86..92201.54 rows=211712
width=4)"
" Recheck Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"
" -> Bitmap Index Scan on pk_fs_nav (cost=0.00..8570.94
rows=211712 width=0)"
" Index Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"
" -> Sort (cost=110929.32..111458.60 rows=211712 width=4)"
" Sort Key: n1.scheme_code"
" -> Bitmap Heap Scan on nav n1 (cost=8623.86..92201.54 rows=211712
width=4)"
" Recheck Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"
" -> Bitmap Index Scan on pk_fs_nav (cost=0.00..8570.94
rows=211712 width=0)"
" Index Cond: ((scheme_code >= 100) AND (scheme_code <=
200))"

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Roberts 2008-07-11 16:27:58 Re: Rollback in Postgres
Previous Message samantha mahindrakar 2008-07-11 15:43:13 Rollback in Postgres