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 > Updating from two tables with large data

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-11, 11:39
prashant.gadekar prashant.gadekar is offline
Registered User
 
Join Date: Mar 2011
Posts: 8
Updating from two tables with large data

How to do bulk upload and delete

There are two tables

Code:
TX 
------------------------------
HIT_ID  USERID AUTHENTICATED
Code:
USERACCESS 
--------------------------------
HIT_ID USERID
I Want to update all the rows from USERACCESS to
TX such that USERID is updated in TX table where
TX.HIT_ID equals USERACCESS.HIT_ID


I have used following query:
Code:
update 
    TX 
set 
   USERNAME = (select USERID from USERACCESS where 
USERACCESS.HIT_ID = TX.HIT_ID FETCH FIRST 1 ROW ONLY) ,
   ISAUTHENTICATED = 'Y'
WHERE 
   HIT_ID IN (
          select WEBHIT_ID FROM USERACCESS.HIT_ID 
   )
There crores of records in TX table and lacks of records in USERACCESS TABLE. Please suggest the update query I am running is ok or not. It is taking long time to update.

I have to delete the HIT_ID from USERACCESS table that are updated to TX table. Please suggest how can I achieve this tasks.

For now I have written a trigger that that does delete on Update of TX table.

Any help appreciated.

Last edited by prashant.gadekar; 09-20-11 at 03:09. Reason: Coulmn name mistakes
Reply With Quote
  #2 (permalink)  
Old 09-19-11, 21:45
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I thought there were many use of incorrect column names.

For example:
Quote:
TX.WEBHIT_ID equals USERACCESS.HIT_ID
Quote:
select WEBHIT_ID FROM ...
they may be TX.HIT_ID

Quote:
set USERNAME = ...
it may be USERID

Quote:
USERACCESS.HIT_ID = TX.USER_ID
it may be
USERACCESS.HIT_ID = TX.HIT_ID
or
USERACCESS.USERID = TX.USERID
?

Quote:
ISAUTHENTICATED = 'Y'
it may be AUTHENTICATED

Quote:
FROM USERACCESS.HIT_ID
may be syntax error??


Anyhow, here is my idea including some guesses, but not tested.
Code:
WITH
 update_tx AS (
SELECT hit_id
 FROM  FINAL TABLE
       (UPDATE tx
         SET   userid
             = (SELECT userid
                 FROM  useraccess ua
                 WHERE ua.hit_id = tx.hit_id
               )
             , authenticated
             = 'Y'
         WHERE EXISTS
               (SELECT 0
                 FROM  useraccess ua
                 WHERE ua.hit_id = tx.hit_id
               )
       ) tx
)
SELECT COUNT(*) rows_deleted
 FROM  OLD TABLE
       (DELETE FROM
               useraccess ua
         WHERE ua.hit_id
               IN (SELECT hit_id
                    FROM  update_tx
                  )
       ) ua
;

Last edited by tonkuma; 09-20-11 at 04:12. Reason: Move authenticated to SET clause. Replace from "FINAL" to "OLD" in "FROM FINAL TABLE (DELETE "
Reply With Quote
  #3 (permalink)  
Old 09-20-11, 04:11
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Yes, too much incorrect info.

To reduce the number of rows updated per transaction (instead of crores and lacks(sic))

Code:
FROM  FINAL TABLE
       (UPDATE (select * from tx where hit_id between <a> and <b>) tx
           SET userid
the between clause should restrict the number of records in tx that will be updated.

hth
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
Reply

Tags
db2, update query

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