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 > Efficient way to do this...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-15-06, 12:25
sordax sordax is offline
Registered User
 
Join Date: Feb 2005
Posts: 12
Efficient way to do this...

We use selects like this in embedded our programs:

select * from table where (field = :field or :field = ' ')

:field depends on a filter condition and we don't know if it's going to have a value in it or not. If :field has a value in it, we want to only show the rows with field = :field. If :field is blanks (' ') we want to show all rows.

It seems that to put several conditions like this in a query results in bad peformance. Is there a better, performance-friendly way to write this?

We're on DB2 for iSeries

Thanks in advance

Last edited by sordax; 03-15-06 at 12:31.
Reply With Quote
  #2 (permalink)  
Old 03-15-06, 13:52
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I would try this:

set fieldb to blanks

select col1, col2 from table where field in (:fielda, :fieldb)

I hope that you don't use "select *" in embeded SQL and that was only an example.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 03-16-06, 03:57
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
such a construct will most likely lead to a tablespace-scan.
Using the BIND parameter REOPT(VARS) might increase performance

consider using dynamic sql for that issue.
Reply With Quote
  #4 (permalink)  
Old 03-16-06, 11:14
sordax sordax is offline
Registered User
 
Join Date: Feb 2005
Posts: 12
Quote:
Originally Posted by Marcus_A
I would try this:

set fieldb to blanks

select col1, col2 from table where field in (:fielda, :fieldb)

I hope that you don't use "select *" in embeded SQL and that was only an example.
I think this won't give the desired result as it won't select all rows when host variable :fielda = ' ', it will only select rows having field = ' ' ! By the other hand I don't think field in (:fielda, :fieldb) is better than field = :fielda or field = fieldb

Don't worry we do not use "select *", it was only an example.
Reply With Quote
  #5 (permalink)  
Old 04-10-06, 09:02
ORSIPMP ORSIPMP is offline
Registered User
 
Join Date: Oct 2005
Posts: 12
You'd better run EXPLAIN with the different options to have all the info. Then choose the best one.
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