Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Implementing Queuing Mechanism


Implementing Queuing Mechanism

Author
Message
harsha.majety
harsha.majety
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 448
Comments posted to this topic are about the item Implementing Queuing Mechanism
paul_ramster
paul_ramster
Old Hand
Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)

Group: General Forum Members
Points: 308 Visits: 534
That logic can be done in one single update (which doesn't require any particular locking strategies or isolation levels):
DECLARE @nextMergeID INT ;
DECLARE @customerID INT;
DECLARE @mergeDate DATETIME;
DECLARE @reset BIT;

UPDATE dbo.merge_standard_queue
SET @nextMergeID = merge_id
, picked = 1
, lub = 'v2'
WHERE merge_id = (
select top 1 merge_id
from dbo.merge_standard_queue
where picked = 0
order by merge_date asc
);
GO 255



testing your method against the above single update, I created a merge_standard_queue table:
drop table dbo.merge_Standard_Queue
GO
create table dbo.merge_standard_queue
( merge_id int identity(1,1) not null primary key
, merge_date date not null
, picked bit not null
, lub varchar(10) null
)
GO
INSERT INTO dbo.merge_Standard_Queue (picked, merge_Date)
select 0
, DateAdd(day,t.Number, '2010-01-01')
from tally t
GO


(my tally table has 32,767 rows, so that's a reasonable size to test against).

Then I opened two separate windows in SSMS, one with your script (amended to set lub to 'v1'), and one with the single update version above, and ran them both 255 times concurrently (with a GO 255).

The results were interesting...
Both reported "Batch execution completed 255 times.", but your script never updated 255 rows, whereas the single update statement always updated 255 rows every time.

select lub, count(*) from dbo.merge_standard_Queue group by lub


returned (as an example, the actual number of updated rows varied between iterations)


lub (No column name)
V1 238
NULL 32275
v2 255


paul_ramster
paul_ramster
Old Hand
Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)

Group: General Forum Members
Points: 308 Visits: 534
Trying the test again, but with 2 windows both running your original script concurrently, then both updated 255 rows every time.

I can't explain this, but it must be something to do with the locking (??!)
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12376 Visits: 885
Thanks for another good script Harsha.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search