30 pts.
 slow update Query(Primary Key)
dear Friends when i run below query its take more than 10 minutes and still running.... update auditdata set TATCallType='12',TATCallUnit='1' from auditdata auditdata inner join Auditdata_sms_12 a_sns on auditdata.ID = a_sns.id auditdata is my original table in above table ID is primary key Auditdata_sms_12 is my temp table whats wrong in aobe SQL Query plz help me thanx in advance..

Software/Hardware used:
ASKED: July 14, 2009  4:22 PM
UPDATED: July 15, 2009  6:16 PM

Answer Wiki:
Both auditdata and a_sns should generally have an index on their id column if your are joining based on these columns. There are exceptions. If there is an index on both columns, the next best thing to do would be to run an explain plan on the sql query to find out what the optimizer is doing. At that point you can pinpoint the problem. [kccrosser] How many rows are in each of the tables? Particularly the temp table. First, why not use the following? update auditdata set TATCallType='12',TATCallUnit='1' where ID in (select id from Auditdata_sms_12 ); If there are a lot of rows in your temp table (like hundreds of thousands or millions?), you may need to break this into smaller chunks to avoid creating excessive rollback segments. It has been my experience that as rollback segments start getting really large, the transaction performance degrades exponentially. Best is to break into smaller transactions, like: declare cur cursor for select id from Auditdata_sms_12; declare @id int declare @i int set @i = 0 open cur fetch next from cur into @id begin transaction while @@fetch_status = 0 begin update auditdata set TATCallType='12',TATCallUnit='1' where auditdata.ID = @id; set @i = @i + 1 if (@i % 1000) = 0 begin -- every 1000 updates, commit to database commit transaction -- close current transaction begin transaction -- start a new one end fetch next from cur into @id end commit transaction This approach also has the benefit that we aren't trying to create the join product table of Auditdata and Auditdata_sms_12.
Last Wiki Answer Submitted:  July 15, 2009  4:18 pm  by  Djeepp   340 pts.
All Answer Wiki Contributors:  Djeepp   340 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

How about something like this:

UPDATE auditdata
SET TATCallType=’12′,TATCallUnit=’1′
FROM auditdata a
WHERE EXISTS ( SELECT 1 FROM Auditdata_sms_12 WHERE id = a.id);

With an index on Auditdata_sms_12.id the full table scan of the subselect (if you use where ID in (select…) ) could be avoided.

 63,535 pts.