SQL Server Tips ----- EFFICIENTLY MANAGE LARGE DATA MODIFICATIONS

Didyou know that you can now use the TOP operator for Deleting, Inserting andUpdating data in SQL Server tables?

Using the TOP operator for DML operation can help you in executing verylarge data operations by breaking the process into smaller pieces. This canpotentially help with increased performance and also helps with improvingdatabase concurrency for larger and highly accessed tables. This is consideredas one of the best techniques for managing data modifications on large dataloads for reporting or data warehouse applications.

When you perform an update on large number of records using single set updates,it can cause the Transaction Log to grow considerably. However, when processingthe same operation in chunks or pieces, each chunk is committed aftercompletion allowing SQL Server to potentially re-use the T-Log space. Anotherclassic issue many of us have experienced is when you are performing very largedata updates and you cancel the query for some reason, you would have to waitfor a long time while the transaction completely rolls back.

 

Withthis technique you can perform data modifications in smaller chunks and you cancontinue with your updates more quickly. Also, chunking allows more concurrencyagainst the modified table, allowing user queries to jump in, instead ofwaiting for several minutes for a large modifications to finish.

Let’s take an example of deleting 1000 rows of records in a chunk. Assume atable called LARGETABLE table that has millions of records and you wantdelete 1000 records in chunk:

--CREATE A DEMO TABLE CALLED LARGETABLE
CREATE TABLE LARGETABLE (ID_COL INT IDENTITY(1,1), COL_A VARCHAR(10),COL_B VARCHAR(10))
GO

--INSERT THE DATA IN LARGETABLE.. NOTICE THE USE OF ‘GO 10000’
INSERT INTO LARGETABLE VALUES ('A','B')
GO 10000 

--QUERY THE TABLE 
SELECT COUNT(*) FROM LARGETABLE;

--PERFORM DELETION OF 1000 ROWS FROM LARGETABLE
WHILE (SELECT COUNT(*) FROM LARGETABLE) > 0
BEGIN
            DELETE TOP (1000) FROM LARGETABLE
            SELECT LTRIM(STR(COUNT(*)))+' RECORDS TO BE DELETED' FROM LARGETABLE --THIS IS JUST A COMMENT.
END

The above technique can also be used with INSERT and UPDATE commands.  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值