Re: rewrite in to exists? - Mailing list pgsql-performance

From Josh Berkus
Subject Re: rewrite in to exists?
Date
Msg-id 200309181034.13414.josh@agliodbs.com
Whole thread Raw
In response to Re: rewrite in to exists?  (LN Cisneros <lnsea@earthlink.net>)
Responses Find one record
List pgsql-performance
Laurette,

> >SELECT t1.code, t1.id, t1.date_of_service
> > FROM tbl t1 INNER JOIN
> >       (SELECT DISTINCT date_of_service
> >         FROM tbl
> >         WHERE xxx >= '29800' AND xxx <= '29909'
> >          AND code = 'XX'
> >      ) AS t2 ON (t1.date_of_service = t2.date_of_service)
> > WHERE t1.client_code = 'XX'
> > ORDER BY id, date_of_service;
>
> A question I have is is the "DISTINCT" really going to help or is it just
> going to throw another sort into the mix making it slower?

It's required if you expect the subquery to return multiple rows for each
date_of_service match.  Of course, you can also put the DISTINCT in the main
query instead; it depends on how many results you expect the subquery to
have.

Still, I'd suggest trying the EXISTS version first .... under most
circumstances, DISTINCT is pretty slow.

--
Josh Berkus
Aglio Database Solutions
San Francisco

pgsql-performance by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Is there a reason _not_ to vacuum continuously?
Next
From: Joseph Bove
Date:
Subject: Find one record