Обсуждение: Equivalent of or condition in where clause

Поиск
Список
Период
Сортировка

Equivalent of or condition in where clause

От
Firthouse banu
Дата:
Hi all, 

Need a suggestion. I have a update query in Postgres which is equivalent to below example. Since query is having multiple or conditions and our tables are very large it’s taking forever to run. Can you guys suggest me what can be replaced with or condition in below example.

Update table_name IA set 
Column_1 = SC. Column_1,
Column_2 = SC. Column_2,
Column_3 = SC. Column_3
From (select statement ) SC
Where 
IA. Column4=! SC. Column4 or 
IA.column5  = ! SC. Column5 or 
IA.column6 = ! SC. Column6 or
IA.column7 = ! SC. Column7
And IA. Column8 = SC. Column8 
And IA. Column9 = SC. Column9;

Thanks 





Re: Equivalent of or condition in where clause

От
Julien Rouhaud
Дата:
On Thu, Apr 01, 2021 at 02:11:12PM +0530, Firthouse banu wrote:
> Hi all,
> 
> Need a suggestion. I have a update query in Postgres which is equivalent to
> below example. Since query is having multiple or conditions and our tables
> are very large it’s taking forever to run. Can you guys suggest me what can
> be replaced with or condition in below example.
> 
> Update table_name IA set
> Column_1 = SC. Column_1,
> Column_2 = SC. Column_2,
> Column_3 = SC. Column_3
> From (select statement ) SC
> Where
> IA. Column4=! SC. Column4 or
> IA.column5  = ! SC. Column5 or
> IA.column6 = ! SC. Column6 or
> IA.column7 = ! SC. Column7
> And IA. Column8 = SC. Column8
> And IA. Column9 = SC. Column9;

I don't think this query is doing what you think it's doing.  You should
probably put parenthesis around the OR-ed predicates if you don't want this
behavior:

 select 1 != 2 or 2 != 3 and 3 = 4 and 4 = 5;
 ?column?
----------
 t



Re: Equivalent of or condition in where clause

От
Firthouse banu
Дата:
I agree with you.
Thanks Julien. 

More suggestions are welcome.  
Thanks 

On Thu, 1 Apr 2021 at 2:32 PM, Julien Rouhaud <rjuju123@gmail.com> wrote:
On Thu, Apr 01, 2021 at 02:11:12PM +0530, Firthouse banu wrote:
> Hi all,
>
> Need a suggestion. I have a update query in Postgres which is equivalent to
> below example. Since query is having multiple or conditions and our tables
> are very large it’s taking forever to run. Can you guys suggest me what can
> be replaced with or condition in below example.
>
> Update table_name IA set
> Column_1 = SC. Column_1,
> Column_2 = SC. Column_2,
> Column_3 = SC. Column_3
> From (select statement ) SC
> Where
> IA. Column4=! SC. Column4 or
> IA.column5  = ! SC. Column5 or
> IA.column6 = ! SC. Column6 or
> IA.column7 = ! SC. Column7
> And IA. Column8 = SC. Column8
> And IA. Column9 = SC. Column9;

I don't think this query is doing what you think it's doing.  You should
probably put parenthesis around the OR-ed predicates if you don't want this
behavior:

 select 1 != 2 or 2 != 3 and 3 = 4 and 4 = 5;
 ?column?
----------
 t