Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About Us
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
English (EN-US)English (EN-GB)Español (ES)हिंदी (HI)italiano (IT)日本語 (JA)

Updating, inserting and deleting from large data sets with minimal locking

Using SQL Server to update, insert and delete from a large data set with minimal locking by using loops

Use smaller transactions with systems that continually lock

Using SQL Server Data

When working with particularly large data sets, there can be issues with locking, however it doesn't need to be like this. If you can, break out the transactions into smaller pieces.

Lets look at some of the code below. First of all open SSMS and three query windows, and the Activity Monitor, set the update period to one second. 

In the first window, we can create a table for use in testing.

This will take a while to run and will require a couple of windows open.

Create test table

CREATE TABLE (ID BIGINT IDENTITY(1,1) CONSTRAINT PK_ID PRIMARY KEY,GUID UNIQUEIDENTIFIER)CREATE INDEX IDX_GUID ON (GUID)

Create some records

Execute this code and then replace the code with below, which will insert 5 million random GUID's into the table one at a time, this then simulates constant updating and inserting for us. You can push this further if you like. It will also print the number of records inserted every 1000 rows, so you can keep track of it.

SQL

SET NOCOUNT ONDECLARE @ID BIGINT=1WHILE @ID<5000000 BEGININSERT INTO (GUID) SELECT NEWID()IF @ID%1000=0 PRINT @IDSET @ID=@ID+1END

Select a few records

If we open a second window, add the code below, which will select 10000 records that begin with 20.

SQL

SELECT TOP 10000 * FROM WHERE GUID Like '%20'

Loop a delete

Now open a third window, you can run the code below, which will loop through the GUID's and delete where they begin with a 0.

SQL

WHILE EXISTS (SELECT TOP 1 * FROM WHERE GUID LIKE '0%') BEGINDELETE TOP (1000) FROM WHERE GUID LIKE '0%'END

Testing the theory

Once you get above 500000 records in your fist window, run either or both of the statements in windows two and three, and check your activity monitor for what happens.

You should see fewer locks, and the lock that you do get should be for shorter periods of time, not preventing users from carrying on doing the work they need to, although it may slow down to a certain extent.

Further Reading

Combine the code above with the automatic re-indexing, and it should keep your system in perfect working order.
A complete maintenance plan for SQL Server 2008

Author

Was this helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader