Mark Brown's Blog

TFS, Visual Studio, SQL Server, BizTalk, SharePoint, .Net, and more ...

How to write SQL to get a record to process in a cluster of processing computers

Working with a friend at work, we were discussing how to create some SQL to go after records in a table and process in a farm of computers (processing cluster).   After few minutes of thinking I came up with the following. 

BEGIN TRAN MyTran

UPDATE TOP 1 [dbo].[SomeTable] WITH (READPAST,UPDLOCK)
   SET [IsCompleted] = 1, @RowId = ID
   WHERE [IsCompleted] = 0

-- Do som manual processing on the record @RowId

COMMIT TRAN MyTran

The transaction issues the locks and keeps everything ACID.   The update writes or holds a lock on the 1st available record due to the READPAST hint and the UPDLOCK hint holds the lock until the commit tran.   If you have 1 machine it works fine and if you add 1000 machines then each machine grabs 1 record and proesses it.   It helps with multi-concurrency and blocking that might occur without the hints.

Now this is just a form of load balancing using SQL (smile).

 

Published Saturday, March 01, 2008 2:59 PM by mab
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker