![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
PROCEDURE [pSelectTmpTable] AS BEGIN SET NOCOUNT ON; BEGIN TRANSACTION; SELECT TOP 10 * FROM [tmp_table] WITH (XLOCK,ROWLOCK) WHERE status=0; UPDATE [tmp_table] SET status=1 WHERE id IN (SELECT TOP 10 id FROM [tmp_table] WHERE status=0); COMMIT TRANSACTION; COMMIT; END Does it the right way to manipulate records in high concurrency context ? Do particularly the clauses (XLOCK,ROWLOCK) properly work under those requirements ? |
#3
| |||
| |||
|
|
Then again you could use the TOP and OUTPUT clauses to your advantage: UPDATE TOP (10) tmp_table SET status = 1 OUTPUT inserted.col1, inserted.col2, ... WHERE status = 0 |
#4
| |||
| |||
|
|
I didn't know this clause and it is very useful for my purposes. Due to the "order by" issue which may cause performance lacks (even if there's an index), I've decided to modify the architecture as: 1) @my_id = @@SPID 2) UPDATE SET status = @my_id 3) retreive updated rows 4) DELETE WHERE status = @my_id (in the same stored) In the previous architecture the DELETE was delayed after each job run, but I've seen there's no really any reason to do it. |
#5
| |||
| |||
|
|
Just make sure that you the OUTPUT clause, so that you operate on the same rows all the way through. |
![]() |
| Thread Tools | |
| Display Modes | |
| |