If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL: Replace OR-Operator with IN-Operator

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-03, 07:02
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
SQL: Replace OR-Operator with IN-Operator

Hi

I have the following sql:
Select count(*) from ADU.UTADU_ADRESSE where (wvrd_wlgs, wvagentur) = (1,1) or (wvrd_wlgs, wvagentur) = (2,2)

Can the OR-Operator be replaced with the IN-Operator for this query ?

Marc
Reply With Quote
  #2 (permalink)  
Old 10-03-03, 10:46
blom0344 blom0344 is offline
Registered User
 
Join Date: Jan 2003
Location: Zutphen,Netherlands
Posts: 256
Which DB2 version supports this type of syntax?
__________________
Ties Blom
Senior Application Developer BI
Getronics Healthcare
DB2,ORACLE,Powercenter,BusObj,Access,
SQL, SQL server
Reply With Quote
  #3 (permalink)  
Old 10-03-03, 12:59
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
I work with udb 7.1 on Windows / Unix
Reply With Quote
  #4 (permalink)  
Old 10-03-03, 13:05
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
Re: SQL: Replace OR-Operator with IN-Operator

You can do something like (or use with to generate "temp" table)

Select count(*) from ADU.UTADU_ADRESSE where
(wvrd_wlgs, wvagentur) in ( values (1,1), (2,2))
Reply With Quote
  #5 (permalink)  
Old 10-06-03, 15:35
M_RAS M_RAS is offline
Registered User
 
Join Date: Sep 2003
Location: canada
Posts: 230
Re: SQL: Replace OR-Operator with IN-Operator

Quote:
Originally posted by chuzhoi
You can do something like (or use with to generate "temp" table)

Select count(*) from ADU.UTADU_ADRESSE where
(wvrd_wlgs, wvagentur) in ( values (1,1), (2,2))
Could you please let us know in performance point of view, which one is better?

Thanks
Reply With Quote
  #6 (permalink)  
Old 10-09-03, 07:11
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
Re: SQL: Replace OR-Operator with IN-Operator

Performance:

Test-Statements:
- OR:
Select count(*)
from ADU.UTADU_ADRESSE
where (wvrd_wlgs, wvagentur) = (94,640) or (wvrd_wlgs, wvagentur) = (88,137)

IN:
Select count(*)
from ADU.UTADU_ADRESSE
where (wvrd_wlgs, wvagentur) in ( values (94,640), (88,137))

Table ADU.UTADU_ADRESSE : 5'281'216 records
Index on columns: wvrd_wlgs, wvagentur

--------------------------------------------------------------

I explained these statements by visual explain and got:
Costs:
- OR: 58'000
- IN: 150

I issued the SQL-statement and got time in seconds:
- OR: 10s
- IN: 0,5s

I knew that IN is better than OR, but I thought that UDB does SQL-Rewrite in this case, this difference really surprises me !!!
(if I have only 1 column, then I get the same costs for OR and IN, but with 2 columns like above results a big difference)


Marc
Reply With Quote
  #7 (permalink)  
Old 10-09-03, 12:28
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I would make sure you have the latest fixpak installed. If that does not fix the problem, I would change the query optimization for this query:
SET CURRENT QUERY OPTIMIZATION 7 (the default is usually set to 5).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On