Guest User

Untitled

a guest
Apr 24th, 2012
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. Update on Varchar column of a table that has > 10 millions rows
  2. declare @var varchar(max) = (select metrcikey from metricdim where metrciname ='XYZ')
  3.  
  4. Update
  5. fopty
  6. set
  7. Metrickey = metrickey+','+@var
  8. from
  9. optyfact opty
  10. inner join
  11. optydim dim on opty.optyid = dim.optyid
  12. inner join
  13. geodim geo on geo.atukey = opty.atukey
  14. inner join
  15. agreementdim ag on opty.optyid = ag.optyid
  16. inner join
  17. account acc on acc.optyid = acc.optyid
  18. where
  19. dim.optytype= 'ABC'
  20. and geo.atukey =145
  21. and ag.agreementtype ='Sold'
  22. and acc.accountteamManager ='XXX'
  23.  
  24. Metrickey = Metrickey + @var
  25.  
  26. CREATE TABLE #q(rn INT IDENTITY(1,1) PRIMARY KEY, optyid INT);
  27.  
  28. DECLARE
  29. @rc INT,
  30. @step INT = 1,
  31. @chunk INT = 100000;
  32.  
  33. INSERT #q(optyid) SELECT DISTINCT opty.optyid
  34. FROM dbo.optyfact AS opty
  35. INNER JOIN dbo.optydim AS dim ON opty.optyid = dim.optyid
  36. INNER JOIN dbo.geodim AS geo ON geo.atukey = opty.atukey
  37. INNER JOIN dbo.agreementdim AS ag ON opty.optyid = ag.optyid
  38. INNER JOIN dbo.account AS acc ON acc.optyid = acc.optyid
  39. WHERE
  40. dim.optytype = 'ABC';
  41. AND geo.atukey = 145
  42. AND ag.agreementtype = 'Sold'
  43. AND acc.accountteamManager = 'XXX';
  44.  
  45. SET @rc = @@ROWCOUNT;
  46.  
  47. WHILE @step <= ((@rc / @chunk) + 1)
  48. BEGIN
  49. BEGIN TRANSACTION;
  50.  
  51. UPDATE o SET MetricKey += ',' + @var
  52. FROM dbo.optyfact AS o
  53. INNER JOIN #q AS q ON o.optyid = q.optyid
  54. WHERE q.rn BETWEEN (((@step-1)*@chunk)+1) AND (@step*@chunk);
  55.  
  56. COMMIT TRANSACTION;
  57. CHECKPOINT;
  58. SET @step += 1;
  59. END
  60.  
  61. DROP TABLE #q;
Advertisement
Add Comment
Please, Sign In to add comment