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 > Optimize Update Statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Nov 2012
Posts: 7
Optimize Update Statement

Hi,

I have this update statement and I am sure there has to be a way to make it more efficient. It currently works but I would like to find a way to do it with only 1 select statement rather than 2. Is this possible?

UPDATE KELLYP.LOSTSALE A
SET A.UNFMT = (SELECT SUNFMT FROM KELLYP.INSMFHTEST B
WHERE A.LSPART = B.SPART AND A.LSLNCD = B.SFRAN)
where exists (select sunfmt
from kellyp.insmfhtest b
where a.lspart = b.spart and a.lslncd = b.sfran);
Reply With Quote
  #2 (permalink)  
Old
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 5,399
I'm not sure I understand your criteria of "efficiency", but consider using the MERGE statement.
__________________
---
"It does not work" is not a valid problem statement.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Nov 2012
Posts: 7
Quote:
Originally Posted by n_i View Post
I'm not sure I understand your criteria of "efficiency", but consider using the MERGE statement.
Well it just seems like there should be a better way. Using 2 select statements is basically having to go through the whole table twice. I will look into MERGE but I am on IBM i and I am not sure that MERGE is supported but I will do some more research. Thanks for the response.
Reply With Quote
  #4 (permalink)  
Old
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 5,399
Quote:
Originally Posted by kelly89 View Post
is basically having to go through the whole table twice.
This isn't necessarily the case, generally speaking, but then I don't know how the DB2 query optimizer works. You may want to look at the access plan.
__________________
---
"It does not work" is not a valid problem statement.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Nov 2011
Posts: 314
Maybe add a unique index on KELLYP.INSMFHTEST(SPART ,SFRAN)
will speed your query。
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
Join Date: Nov 2009
Posts: 14
Can you provide the cost of this query and also the access plan.. from db2expln. Only then it will be easier to tune this sql... either by adding indexes.. or checking the nlevels of existing indexes... or my rewriting the sql.
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: Nov 2009
Posts: 14
*by rewriting the sql ..
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