Results 1 to 7 of 7
  1. #1
    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);

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,461
    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.

  3. #3
    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.

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,461
    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.

  5. #5
    Join Date
    Nov 2011
    Posts
    319
    Maybe add a unique index on KELLYP.INSMFHTEST(SPART ,SFRAN)
    will speed your query。

  6. #6
    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.

  7. #7
    Join Date
    Nov 2009
    Posts
    14
    *by rewriting the sql ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •