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 > Optimized querry

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-05, 00:19
Padmanabhan Padmanabhan is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
Optimized querry

I am using this querry which works fine;

Please advise me as to get an optimized queery as this scans the entire table for every update of a row.

UPDATE SRCHUPDATE_MAIN A
SET A.FULL_NAME =(SELECT A.FIRST_NAME || B.MIDDLE_NAME || C.LAST_NAME
FROM SRCHUPDATE_MID B,
SRCHUPDATE_LST C
WHERE A.EMP_ID = B.EMP_ID
AND A.EMP_ID = C.EMP_ID);

Thank you very much in anticipation of a solution

Regards

Padmanabhan
Reply With Quote
  #2 (permalink)  
Old 01-20-05, 00:25
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
First, I assume that you have an index on EMP_ID in all three tables. If not, create one.

You will always do a tablespace scan on SRCHUPDATE_MAIN since you are updating every row in that table (with data from the other tables). So it is more efficient for DB2 to do a tablespace scan on this table.

If you have the indexes suggested above, there will be no tablespace scan on the subquery part of the SQL statement.

Make sure you do runstats command on all tables involved, and also on the indexes for these tables.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390

Last edited by Marcus_A; 01-20-05 at 00:27.
Reply With Quote
  #3 (permalink)  
Old 01-25-05, 01:31
Padmanabhan Padmanabhan is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
Optimized Query

Thanks for your prompt reply.

Does DB2 support Update based on table join like the one given below?
If no, correlated sub query is the only solution?

MS SQL Sever:

UPDATE SRCHUPDATE_MAIN
SET FULL_NAME = A.FIRST_NAME + B.MIDDLE_NAME + C.LAST_NAME
FROM SRCHUPDATE_MAIN A, SRCHUPDATE_MID B, SRCHUPDATE_LST C
WHERE A.EMP_ID = B.EMP_ID
AND A.EMP_ID = C.EMP_ID
Reply With Quote
  #4 (permalink)  
Old 01-25-05, 10:11
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
UPDATE SRCHUPDATE_MAIN E
SET FULL_NAME =
(SELECT A.FIRST_NAME || B.MIDDLE_NAME || C.LAST_NAME
FROM SRCHUPDATE_MAIN A, SRCHUPDATE_MID B, SRCHUPDATE_LST C
WHERE A.EMP_ID = B.EMP_ID
AND A.EMP_ID = C.EMP_ID
AND E.EMP_ID = A.EMP_ID)

The last line was added by me. Not sure if this fits your data correctly, but you need some way correlate the joined rows with the table you are updating.
__________________
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
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