Avoiding sequential scans with OR join condition - Mailing list pgsql-general

From Mike Mascari
Subject Avoiding sequential scans with OR join condition
Date
Msg-id 4170B03D.3060906@mascari.com
Whole thread Raw
Responses Re: Avoiding sequential scans with OR join condition  (Janning Vygen <vygen@gmx.de>)
Re: Avoiding sequential scans with OR join condition  (Martijn van Oosterhout <kleptog@svana.org>)
Re: Avoiding sequential scans with OR join condition  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Avoiding sequential scans with OR join condition  (Sim Zacks <sim@compulab.co.il>)
List pgsql-general
Hello. I have a query like:

SELECT big_table.*
FROM little_table, big_table
WHERE little_table.x = 10 AND
little_table.y IN (big_table.y1, big_table.y2);

I have indexes on both big_table.y1 and big_table.y2 and on
little_table.x and little_table.y. The result is a sequential scan of
big_table. In order to prevent this, I've rewritten the query as:

SELECT big_table.*
FROM little_table, big_table
WHERE little_table.x = 10 AND
little_table.y = big_table.y1
  UNION
SELECT big_table.*
FROM little_table, big_table
WHERE little_table.x = 10 AND
little_table.y = big_table.y2

which does allow an index scan, but suffers from two separate queries
along with a unique sort, which, from the data, represents 90% of the
tuples returned by both queries.

Is there any way to write the first query such that indexes will be used?

Mike Mascari

pgsql-general by date:

Previous
From: Michael Glaesemann
Date:
Subject: Re: Has anyone tried Date/Darwen/Lorentzos's model for temporal data?
Next
From: William Yu
Date:
Subject: Re: creating audit tables