Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to update a table which has million of records.

I have used the below query.

1.I have put the pkeys to be updated in a temp table
SQL
select * into tempPkey  
  from Prd WHERE status<>'d' and pKey NOT IN (SELECT PrdPKey FROM PrdGroupRel where status <>'d')


2. Then i have use below update command to update.
SQL
SET NOCOUNT ON
DECLARE    @PKey  AS Varchar(16) ;

DECLARE UPDATE_CURSOR CURSOR FOR

SELECT pkey FROM tempPkey FOR UPDATE 

OPEN UPDATE_CURSOR
FETCH NEXT FROM UPDATE_CURSOR
INTO @PKey

WHILE (@@FETCH_STATUS = 0)
BEGIN
BEGIN Transaction



Update Cndcpmain set status ='d' ,Hostsource='INC7892752_30Oct2018'  
where mergedkey like '%'+@PKey+'%'  and CndcpMetapkey='00100000007gu6ib' and salesorg='MS01' and status<>'d'

Print 'Pkey Update completed :' + @Pkey+' ' + convert(varchar(50),Getdate() )

COMMIT Transaction  
    FETCH NEXT FROM UPDATE_CURSOR
    INTO @PKey
END

CLOSE UPDATE_CURSOR
DEALLOCATE UPDATE_CURSOR

SET NOCOUNT OFF

I have disabled triggers and indexes on Cndcpmain table. but only 14-15 pkeys are getting updated in 1 min. There are total 18916 pkeys to update and expected time is 20 hrs. Can we expedite it?

Also I unable to write a select query for the records which got already updated ,so that i can exclude the updated records in next run. could you please help

What I have tried:

[See code above]
Posted
Updated 4-Nov-18 20:25pm
v2

1 solution

CURSOR is probably the slowest way to do things in SQL and should be used only when there is a real need...
You case seems to me better handled with a simple UPDATE statement...
Something like this would do the trick:
SQL
UPDATE CNDCPMAIN 
SET 
	CNDCPMAIN.STATUS = 'D',
	CNDCPMAIN.HOSTSOURCE = 'INC7892752_30OCT2018'  
FROM PRD 
WHERE
	PRD.STATUS <> 'D' AND 
	PRD.PKEY NOT IN (SELECT PRDGROUPREL.PRDPKEY FROM PRDGROUPREL WHERE PRDGROUPREL.STATUS <> 'D') AND
	CNDCPMAIN.MERGEDKEY LIKE '%' + PRD.PKEY + '%'  AND 
	CNDCPMAIN.CNDCPMETAPKEY = '00100000007GU6IB' AND 
	CNDCPMAIN.SALESORG = 'MS01' AND 
	CNDCPMAIN.STATUS <> 'D'
 
Share this answer
 
v2
Comments
Member 12965461 5-Nov-18 5:42am    
the only problem in using the above query is that SQL server stops working after 20 min and will close automatically.
Richard Deeming 6-Nov-18 9:58am    
SQL Server itself, or the application you're using to execute the query?

If it's the application then it sounds like a timeout issue. You either need to increase the command timeout, or set it to not time out.

If it's SQL Server, then it sounds like a bug, and you should report it to Microsoft.
Richard Deeming 6-Nov-18 9:56am    
You're missing the WHERE keyword before the filters. :)
Kornfeld Eliyahu Peter 6-Nov-18 10:04am    
Good one!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900