Re: Query optimization to select rows instead of too many or conditions

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Arup Rakshit <aruprakshit(at)rocketmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Query optimization to select rows instead of too many or conditions
Date: 2015-02-21 11:42:03
Message-ID: B922A04F-27D4-475F-90F7-5126D32908A8@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On 21 Feb 2015, at 9:34, Arup Rakshit <aruprakshit(at)rocketmail(dot)com> wrote:
>
> Select * from Emp
> where (attr1 = val11 and attr2 = val12 and attr3 = val13) or (attr1 = val14and attr2 = val15 and attr3 = val16);
>
> Now suppose I got (x1, x2, x3) and (y1, y2, y3). Then I need to rewrite my query as :
>
> Select * from Emp
> where (attr1 = val11 and attr2 = val12 and attr3 = val13) or (attr1 = val14 and attr2 = val15 and attr3 = val16) or
> (attr1 = x1 and attr2 = x2 and attr3 = x3) or (attr1 = y1 and attr2 = y2 and attr3 = y3);
>
> So for each new set I need to add one more `or` conditions. It seems wired.
>
> Any better way to get it done ?

If the number of attributes to compare is always the same, you can write:

select * from Emp where (attr1, attr2, attr3) in ((val11, val12, val13), (val14, val15, val16), (x1, x2, x3), (y1, y2, y3));

Is that the sort of thing you're after?

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2015-02-21 12:38:08 Re: Query optimization to select rows instead of too many or conditions
Previous Message sridhar bamandlapally 2015-02-21 11:34:04 Re: Array string casts with SELECT but not SELECT DISTINCT