Queue Processing Using Updlock, Readpast Locking Hints

Apr 25, 2008

This article instructed me on how to process rows from a table used as a data queue for multiple processes.

http://www.mssqltips.com/tip.asp?tip=1257

I tested this against the AdventureWorks DB (SQL 2005) and multiple SQL connections inside of Sql Mgmt. Studio).

Connection1:


BEGIN TRANSACTION


SELECT TOP 1 * FROM Production.WorkOrder WITH (updlock, readpast) --skips over locked rows
--COMMIT TRANSACTION

Connection2:


BEGIN TRANSACTION


SELECT TOP 1 * FROM Production.WorkOrder WITH (updlock, readpast) --skips over locked rows

COMMIT TRANSACTION



This works like I want where connection 2 skips over the locked row from connection 1 and gets the next available record from the table / queue. However, when I add ORDER BY tsql to each sql statement, connection 2 is now blocked waiting for Connection 1 to commit. (This is not what I want)

Connection1:


BEGIN TRANSACTION


SELECT TOP 1 * FROM Production.WorkOrder WITH (updlock, readpast) order by DueDate

--COMMIT TRANSACTION

Connection2:


BEGIN TRANSACTION


SELECT TOP 1 * FROM Production.WorkOrder WITH (updlock, readpast) order by DueDate --is blocked until connection 1 commits transaction

COMMIT TRANSACTION




How do I prevent blocking when using these locking hints with ORDER BY?


thanks

View 6 Replies


ADVERTISEMENT

SQL Seems To Ignore UPDLOCK && ROWLOCK Hints

Dec 3, 2007



I've got a SELECT WITH (UPDLOCK, ROWLOCK) WHERE followed by an UPDATE WHERE statement. The results of the SELECT statement are deserialized in C# and updates are made to the deserialized object. Then the object is serialized back into the table with the UPDATE statement. I've got this code running within a transaction scope with the ReadCommited isolation level.

My service receives requests to update data and the requests can come in on different threads. What I'm seeing, is that once in a while, the log messages from my application indicate that two different threads are able to issue the above SELECT statement and both are receiving results. This is a problem since the thread that issues the last UPDATE will overwrite the changes made by the first. Each thread has its own connection and transaction scope.

I've researched all over the place and have tried a few different things, but all things point to the fact that query hints are just hints and that SQL may or may not pay attention to them. If that's the case, how am I suppose to perform a SELECT with the intention of updating so that no one else can do the same? I haven't tried table level locking, but I'd really like to avoid that if possible.

-Mike

View 4 Replies View Related

Locking Hints

May 1, 2007

Hi

My VB.Net (with SQL Server backend) application currently allows more than one user to look at a particular record at the same time. This is not a problem unless both those users also try to update that record as well. One user's changes then overwrite the other's.

I've been reading up on locking hints but my database knowledge is a little scant and I'm also rather dense and need things spelling out for me!! So I have a few questions that I hope someone can help with:

If I add an updlock to my update SQL statement, this would allow both users to view the record but would only allow one user's changes through. Is that correct?

For the other user, would SQL Server return an error message that I can use to tell the user that their update has not worked?

Would I have to get my VB.NET application to re-get the record information so that the user who's update failed can see the changes made by the other user and reapply their own changes?

Does the updlock become unlocked once the record is updated or do I need to specifically unlock it somehow?

Thanks!

View 20 Replies View Related

(readpast Readcommitted) Vs (readpast Repeatableread) Vs Readpast

Sep 25, 2007



What is the difference among them ? who can tell ?

select * from table (readpast readcommitted)
select * from table (readpast repeatableread)
select * from table (readpast)


I suppose they are the same as readpast.
I cannot tell the differece.

thank you

View 8 Replies View Related

Join Filter Causes Conflicting Locking Hints

Jul 14, 2006

Hi,

Using Merge replication, I have a table that is filtered using the HOST_NAME() function. The filter also makes use of a function (as the HOST_NAME() is overriden to return some complex data).

Everything replicates and filters just fine. but when I add a join filter on a different table (the join filter is a simple foreign key join) I get the following error when the snapshot agent is run:



Message: Conflicting locking hints are specified for table "fn_GetIDList". This may be caused by a conflicting hint specified for a view.
Command Text: sp_MSsetup_partition_groups
Parameters: @publication = test1



fn_GetIDList is the function used in the original filter.

Thanks for any help



Graham

View 6 Replies View Related

Pauzing SQL Queue Processing

Jan 19, 2007

Hello,

I'm currently looking for a way to decently pauze the processing of messages by activation procedures. When I pauze the processing I want to be sure that the running activation procedures terminated.
I thought I could do this using ALTER QUEUE WITH ACTIVATION (STATUS = OFF) and sys.dm_broker_activated_tasks.
This is what I've tested: I set up one queue with an activation procedure attached to it that receives one message, then waits for one minute, then writes that message into a table. The queues max queue readers is set to 2. Now, I put 5 messages in the queue, 2 procs pop up in sys.dm_broker_activated_tasks. I run the alter queue statement shown above and check sys.dm_broker_activated_tasks again. It is empty so allegedly there are no tasks running. I check the table in which the messages are written by the activation procs and after some time I can see the results of both activation procedures which were according to sys.dm_broker_activated_tasks not running (but of course they were). The rest of the process works as expected, the procs reactivate upon reenabling the activation status and all is well.
Is there another way of checking the running activation procedures that checking sys.dm_broker_activated_tasks?
Is this intended behaviour or just a bug?

View 4 Replies View Related

How To Delay Queue Processing?

Jul 15, 2005

I am looking for some suggestions on how to implement a delay in processing

View 1 Replies View Related

Row Level Locking For Queue Table To Get Rid Of Concurrency Problem

Apr 7, 2004

Hi all

We are writing a web-based multi-user call centre application application.

we are getting concurrency problems as you would expect with a multiuser application.

the application is made for callers who will bring up a different contact to call based on some predefined priority. now because the algorithm that prioritises the contacts takes a good 2 seconds to run, if 2 different caller request for the next prioritised contact, they will retrieve the same contact.

The only way that we think can resolve this problem is by building a queue. The queue would be implemented as a table, the particular implementation of this queue would be, when ever someone retrieves an entry from the queue, a background process will go on and generate a new queued item, i.e. in a FIFO manner. So that's how we think we should implement the queue.

Now come the question how to implement it. My idea is to have row level locking and a trigger to remove queue items from the queue. so that once one caller have looked at one of the item in the queue, another user can't look at the same item.

Any suggestions as to how i might be able to avoid concurrency problems?

What do you all think of my idea of implementing the FIFO queue?/
Is it possible to do row level locking in such a way that other users won't even be able to read the locked entry??



James :)

View 2 Replies View Related

How To Prevent Table-locking With Multiple Queue Readers

Apr 26, 2007



In a situation where messages are coming in faster than they can be processed, at what point will service broker start up another queue_reader? Also, how do you prevent table locking if part of the processing of that message involves inserting or updating data in a table? We are experiencing this problem because of the high number of messages coming through, and I'm not sure what the best solution is - does service broker have some built-in support for preventing contention on a table when multiple readers are running? Or maybe a pattern that can be used to get around it?

View 1 Replies View Related

READPAST Example

Jul 31, 2001

Does anyone have an example on how to make use of READPAST and ROWLOCK so that when a SELECT statement retrieves a record, other SELECT statements will 'Read Past' that record until it is unlocked?

Thanks!

View 1 Replies View Related

NoLock Vs ReadPast

Oct 25, 2006

I have been experiencing deadlock errors with two stored procs that I am using.

SP1 is a read query that gets all unprocessed orders (status = 0)

SP2 is an insert query that inserts new orders (Status = 0) uses a transaction.

I have a multithreaded application and once in a while the read query (SP1) will try to read a new row that has just been inserted but not committed yet hence the deadlock arises.

If i use a hint "With(NoLocks)" this will be a dirty read and still read the uncommitted insert from SP2 - is this correct?

Where as if I use hint "With(ReadPast)" this will now only read committed rows and hence the deadlock should not arise - it will not read any uncommitted rows - Correct?

So I think that it is better to use READPAST than NOLOCK. Any orders that have status = 0 not picked up will get picked up on the next round when SP1 is executed again.

Any thougths or suggestions are always appreciated.

Jawahar

View 16 Replies View Related

Clarifications On Queue Service And Queue Readers

Jan 11, 2006

Hello,
This is info that I am still not certain about and I just need to make sure, my gut feeling is correct:

A.
When a procedure is triggered upon reception of a message in a queue, what happens when the procedure fails and rolls back?
1. Message is left on the Queue.
2. is the worker procedure triggered again for the same message by the queue?
3. I am hoping the Queue keeps on triggering workers until it is empty.

My scenario is that my queue reader procedure only reads one message at a time, thus I do not loop to receive many messages.

B.
For my scenario messages are independent and ordering does not matter.
Thus I want to ensure my Queue reader procedures execute simultaneously. Is reading the Top message in one reader somehow blocking the queue for any other reader procedures? I.e. if I have BEGIN TRANSACTION when reading messages of the Queue, is that effectively going prevent many reader procedures working simultaneously. Again, I want to ensure that Service broker is effectively spawning procedures that work simultaneously.

Thank you very much for the time,

Lubomir

View 5 Replies View Related

NOLOCK And READPAST On Same Table?

Sep 17, 2005

Is it possible to use With (NOLOCK) and With (READPAST) in the sameSELECT query and what whould be the syntax?@param intSELECTmyRowFROMdbo.myTable WITH (NOLOCK)WHEREmyRow = @paramThanks,lq

View 6 Replies View Related

With Readpast Changing Isloation Level

Nov 2, 2007

We have an application that accesses a SQL Server 2005 database. The isolation level is set to Read Committed. When the app calls a procedure to insert,update, or delete it seems to change the isolation level after the commit. After the commit, it calls another proc to fill a drop down and it gets the following message

"You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ isolation levels"


The proc that fills the drop down isn't even using a with readpast command, but it is being used in some of the procs used prior to the commit.

Does anyone have any ideas?

View 5 Replies View Related

UPDLOCK Question

Apr 21, 2008

Any locking experts?


Are these two queries functionally identical (identical locking)?


--QUERY 1

update MyTable with (UPDLOCK)

set MyField = 1 where MyField2 = 2


--QUERY 2

update MyTable

set MyField = 1 where MyField2 = 2


In other words, is UPDLOCK redundant in an UPDATE query?

View 1 Replies View Related

BLock Records Using UPDLOCK

Oct 2, 2006

Hi all,
here my question :
I have 2 applications.

connection 1.
one does select max(grp) from orv and one does select max(grp) from orh. orh is the historical file from orv. We did this to know which is the greather grp between these 2 files.
After having did this, we add 1 at grp field.
we insert into orv the record max(grp) + 1

connection 2.
an other application could insert at the same time record in this table orv with same parameters.

my problem is the following. I need to block record in orv table either on the select ( connection 1) or Insert ( connection 2) to avoid having select max(grp) + 1 on orv at connection 1 and Insert a record into orv at connection 2.
I believe I need to use HOLDLOCK, UPDLOCK.
but I have not the habitude to use them.

Can I do this ?
connection 1
select max(grp) from orv WITH HOLDLOCK
connection 2
what should I use to avoid lock when I need to insert into orv. ?

thanks for your quick answer

View 1 Replies View Related

Admin Version Of WITH(UPDLOCK)?

Jul 26, 2006

Is there anything I can do from the Enterprise Manager console or fromwithin a JDBC connection to achieve the same effect as WITH(UPDLOCK)?Yes, I could change all of my SQL statements to include the lock... butisn't there any way to set or tweak something in SQL Server so that I won'thave to hack a lot of code* to make things concurrent? Perhaps a way to setUPDLOCK as the default behaviour for the server, or schema, or table, orsomething?Jerry H.* == The existing SQL has to remain as generic as possible so that it can beimplemented for four other databases.

View 3 Replies View Related

UPDLOCK On A Mult-table Join

Jul 23, 2005

Hi all,Is this legal ?SELECT a.col1,b.col2,c.col3FROM tab1 a WITH (UPDLOCK) , tab2 b, tab3 cWHERE a.col1 = b.col1and b.col2 = c.col1WIll the above cause a UPDLOCK on tab1 and not tab2 and tab3?COmments,thoughts,criticisms?I have a problem with a query that performs a multi-table jointo get column values and then one of the tables in the join isbeing updated. I am getting DEADLOCKs and was wondering if I couldtry and reduce that.Also how can I find out what locks are in effect for a certainquery..Is it syslocks or are there any other tables..?DrD

View 2 Replies View Related

Transact SQL :: Sessions Need To Have UPDLOCK To Avoid Deadlocks

Nov 21, 2015

The benifit of UPDLOCK is that it avoids deadlock in case both sessions run the below query at the same time.The table has clustered index on ID column

----session 1 --------
begin transaction
select * from a1

update a1
set id = 22
where id = 2

----session 2 --------
begin transaction
select * from a1

update a1
set id = 22
where id = 2

Now to avoid deadlock in the above scenario we should use (UPDLOCK) hint in the select statement.Now my question is that deadlock will be avoided in this case when both the sessions use UPDLOCK hint. If only one session uses UPDLOCk and other does not then there will be deadlock .For example session 1 uses UPDLOCK hint this will hold the U lock on the row, but the session 2 does not use this hint and apply shared lock on the same row. Now there will be deadlock when session 1 tries to update the record and is blocked by shared locks of session 2. same will be the case with session 2 and both will wait for each other and hence dead lock.so what steps can be taken to avoid deadlocks in this case. I do not want to use Snapshot isolation.

View 3 Replies View Related

Transact SQL :: Difference Between HOLDLOCK And UPDLOCK In Server

Jul 15, 2015

just see same kind of two sql statement

BEGIN TRAN
SELECT *
FROM authors AU
WITH (HOLDLOCK, ROWLOCK)
WHERE AU.au_id = '274-80-9391'

COMMIT TRAN
BEGIN TRANSELECT *
FROM authors AU
WITH (UPDLOCK, ROWLOCK)
WHERE AU.au_id = '274-80-9391'COMMIT TRAN

i like to know what is the difference between HOLDLOCK and UPDLOCK in sql server. explain with example to better understand.

View 4 Replies View Related

DB Engine :: Does UPDLOCK Request Incur Physical write In Any Isolation Level

Nov 12, 2015

Does a UPDLOCK request incur a physical write in any isolation level? (including read committed snapshot)

For example:
BEGIN TRANSACTION
SELECT col FROM dbo.test WITH ( UPDLOCK )
ROLLBACK

Is there any physical write taking place here?

View 13 Replies View Related

Hints ?

Jun 16, 2004

Is there anything equivalent available in SQL Server for Oracle HINTS ?

eg : Oracle query

select /* + INdex(sno index1) */ sno from test_table

Thanks,
Sam

View 2 Replies View Related

Hints

Jul 23, 2005

I am kind of confused about the way SQL Server 2000 handles the hintsthat users supply with their SQL statements.[color=blue]>From BOL, it seems that one can specify them with "WITH (...)" clauses[/color]in SQL statements known as table hints. Sometimes, multiple uses ofthis form in a statement is OK. Then there is the OPTION clause forspecifying statement hints. However, the documentation on OPTIONsection discourages their use.Being relatively new to SQL Server and still learning about it, what isthe general practice? Use hints or not? And if so, how (through WITHor OPTION clauses)?Cheers!

View 5 Replies View Related

Query Hints

Sep 12, 2005

My experience with query hints are that they are just that, a hint. What I don't understand is when does SQL decide to ignore your hint?
 

View 5 Replies View Related

Index Hints

May 15, 2001

I am running SQL7 SP2 and and noticing table the query processor table scans when I ussue a between 'date1' and 'date2' instead of using the datetime index. If I put in the index hint (index = ix_datetimeXXXX) the query runs fine. My question is does this index hint restict the use of other indexes in the query and secondly how can I specify multiple index hints? Thanks in advance.

View 1 Replies View Related

Query Hints

Sep 29, 2003

Please advise.

Whilst running a query I recieved the error below.
Cannot create a worktable row larger than allowable maximum. Resubmit your query with the ROBUST PLAN hint.

What is ROBUST PLAN hint?.

Help Appreciated.

View 6 Replies View Related

BUG With Join Hints

Apr 20, 2007

I am having problems with doing what seams to be a very easy query. For some reason the SQL Server is trying to do nested loops instead of hash join. I tried to force the use of the hash join using the join hint.





Query 1


select *
from DIM_DATE DD
inner hash join (
select A.student_key,
CONVERT(int, CONVERT(varchar, COALESCE (A.date_withdrawn, getdate()), 112)) AS date_withdrawn_current
FROM FACT_STUDENT AS A
) SSE on DD.date_key= date_withdrawn_current
This query gives an error:

Msg 8622, Level 16, State 1, Line 1
Query processor could not produce a query plan because of the hints defined in this query. Resubmit the query without specifying any hints and without using SET FORCEPLAN

Second query is not really what I want but it illustrate that it works fine when getdate() is not used.



Query 2


select *
from DIM_DATE DD
inner hash join (
select A.student_key,
CONVERT(int, CONVERT(varchar, COALESCE (A.date_withdrawn, A.date_enrolled), 112)) AS date_withdrawn_current
FROM FACT_STUDENT AS A
) SSE on DD.date_key= date_withdrawn_current
Is there some problem with using function getdate() ? It works fine in SQL Server 2000

This problem occurs on the SQL Server 2005 SP2 ( 9.00.3050.00 (X64) )
and (9.00.2050)

View 3 Replies View Related

Confused About Using Lock Hints?

Mar 19, 2006

I noticed that the online books say the following:
Note  The SQL Server query optimizer automatically makes the correct determination. It is recommended that table-level locking hints be used to change the default locking behavior only when necessary.
Also, at another place in online books, it says:
The table hints are ignored if the table is not accessed by the query plan.
From the above, it seems that using locking hints is not going to guarantee that SQL Server will follow them. Is this true?

View 24 Replies View Related

JOIN Hints: Why SQL Works As Follows?

Sep 9, 1998

Hi,

Why SQL server dose work as follows when I dose not provide any join hints?
It looks like HASH join is the best plan, but SQL dose not. What kind of
JOIN method is used by SQL optimizer?

Thanks in advance,
Wonhyuk William Chung
wonhyukc@usa.net
MCSE/ MCT



-----------
use northwind
go
select orderid, CompanyName --productname,
from orders o inner join customers c on o.customerID = c.CustomerID
/*
Table `Orders`. Scan count 91, logical reads 184, physical reads 0,
read-ahead reads 0.
Table `Customers`. Scan count 1, logical reads 1, physical reads 0,
read-ahead reads 0.
.0553
*/

select orderid, CompanyName --productname,
from orders o inner hash join customers c on o.customerID = c.CustomerID
/*
hash
Table `Customers`. Scan count 1, logical reads 1, physical reads 0,
read-ahead reads 0.
Table `Orders`. Scan count 1, logical reads 4, physical reads 0, read-ahead
reads 0.
.115
*/

select orderid, CompanyName --productname,
from orders o inner merge join customers c on o.customerID = c.CustomerID
/*
merge
Table `Customers`. Scan count 1, logical reads 4, physical reads 1,
read-ahead reads 3.
Table `Orders`. Scan count 1, logical reads 4, physical reads 0, read-ahead
reads 0.
.115
*/

select orderid, CompanyName --productname,
from orders o inner loop join customers c on o.customerID = c.CustomerID
/*
loop
Table `Customers`. Scan count 830, logical reads 1681, physical reads 0,
read-ahead reads 0.
Table `Orders`. Scan count 1, logical reads 5, physical reads 0, read-ahead
reads 0.
.116
*/

View 1 Replies View Related

Multiple Optimization Hints

Sep 3, 1999

I need to use two hints (INDEX=indexname) and (NOLOCK).
I've tried
(INDEX=indexname),(NOLOCK)
(INDEX=indexname,NOLOCK)
(INDEX=indexname)(NOLOCK)

and nothing works.

Thanks

View 1 Replies View Related

Reason To Use Optimizer Hints

Aug 5, 1999

While investigating performance problems within an application recently I carried out some tests using SET SHOWPLAN ON.

I had a query like this within a stored procedure:

SELECT MAX(X) FROM Y WHERE Z LIKE @MYVAR

Where @MYVAR was passed in. I discovered that SQL Server did a Table Scan even when Z had an index on it. A problem with 200,000 rows!

If I said

SELECT MAX(X) FROM Y WHERE Z LIKE 'HELLO%'

(i.e., used a constant instead of a variable) SQL Server did use the index correctly and did not do a table scan.

I got around this by rewriting my statement:

SELECT MAX(X) FROM Y (INDEX=MYINDEX) WHERE Z LIKE @MYVAR

in other words by manually specifying the index I had created on the Z column.

Hope this helps someone.

View 2 Replies View Related

Subquery Woes; Hints ?

Feb 9, 2008

hi all,
I'm trying to run queries on relatively small tables (a few hundred thousand rows) with subqueries of counts per primary key columns as such:

(ColA in tableA is the primary key)

select * from tableA p
where exists (select 1 from ( select ColA, count(1) cnt
from TableA
group by ColA
having count(1)>1 ) t
where t.ColA= p.ColA)
order by some_col

my problem is that sqlserver 2005 sp5 does not materialize the internal subquery properly, or execute it beforehand and it gets confused as heck and pegs the CPUs at 100% forever.

What hints can I use to solve this issue?
I've tried to use ..... "with ...." to prepare/materialize the table upfront, no luck, one version of statement pegged one cpu at 100%, while the other statement pegged ALL cpu's at 100% -- don't remember which.

My only solution right now was to create these subqueries as PHYSICAL tables -- and this would solve the problem but that would entail creating a lot of un-necessary objects.

thanks much for any feedback!!
Cos

View 5 Replies View Related

Traceflag To Ignore Index Hints

Jul 23, 2001

Hello.

There is a trace flag that tells SQL Server to ignore index hinting in incoming queries. I'm having a Monday morning problem and I can't remember the trace number nor find it in my notes. Can anyone else come up with it?

Thanks in advance,
-darin

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved