Showing posts with label nolock. Show all posts
Showing posts with label nolock. Show all posts

Friday, March 9, 2012

NoLock vs ReadPast

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

I have a lot of experience with NOLOCK. The basic different in NOLOCK and READPAST is that NOLOCK will read the uncommitted data, preventing blocking because of that specific read. However, READPAST skips those rows. If you are updating a row, then NOLOCK reads the uncommitted data if the transaction is still open. If you use READPAST, it doesn't read the row at all, as I understand it.

Since I have a lot of experience with NOLOCK, I would suggest NOLOCK because I understand it a lot better.

I'm not all that convinced that your reads are causing DEADLOCKS however. I wonder if you are getting multiple inserts or updates that are blocking each other. The read locks (READ COMMITTED), by default, will just wait until the insert is done and then read the new inserted row. NOLOCK just tells it not to wait and reads UNCOMMITTED.

To test this, I would use NOLOCK and see if you still have the same problem.

|||

I would almost never suggest you use NOLOCK in a production environment. You don't want to have any chance that you have two users fetching the same row to process. So I would suggest you use readpast to read only committed rows, but skip those that are being inserted, in case that row fails.

As for deadlocks, I can't exactly fathom why this is occurring from the limited information. It is possible that you have indexing problems, causing full table scans to occur on selects... Can you post the table structure and queries.

|||I disagree about the production comment. If you are pulling reports, you want to be able to pull the same record quickly--regardless of locks. As for selecting the same record by two users at the same time, that would occur anyway whether you use NOLOCK or not--assuming that there is not an open transaction modifying the situation.|||There is a possibility for rows to show up twice when using NOLOCK because of page splitting so beware of the hint unless you know exactly how your data is used. This does not mean you should never use NOLOCK of course, there are many situations in which it is completely sane to use it but just remember the consequences.|||

Chris:

I think you have misunderstood what Louis means. I think here when he says "production" he means as part of a record-updating process -- in which case if you are reading a record for update you would never want a dirty read -- using a NOLOCK hint -- but always want a "clean" read of the data.

Dave

|||

Dave,

I think you are correct. I agree with the statement in that case :)

|||

All thank you for your insight

Here are my two sp that cause the Deadlock to occur sometims - I have poseted the queries ony not the whole SP with input paramters etc but the queries are the guts of the SP. I did tunr on Deadlock tracing and these SP were identified as the cause of the deadlock situtation - Many thanks -Jawahar

SP 1 - Read (currently there are no hints) - PLEASE note the queries are not optimized and do not follow good coding standards)

BEGIN
set rowcount @.nMaxMessages
select A.* from MsgRequest A
where ProcessingCode = 0
and RequestID = ( select Min(RequestID) from MsgRequest B where A.ClientID= B.ClientID and B.ProcessingCode = 0 )
and A.ClientID NOT IN (select ClientID from MsgRequest where ProcessingCode = 1)
order by ReceiveDT ASC
END

SP 2 Insert (currently there are no hints) - PLEASE note the queries are not optimized and do not follow good coding standards)

BEGIN
BEGIN TRAN
insert into MsgRequest ( ReceiveDT, ClientID, CommandCode, ProcessingCode, MessageLen, QueryParameterString,
MessageData, ServiceOrderID, NewServiceOrderFlag, ClientVersionNumber)
Values ( getdate(), @.ClientID, @.CommandCode, @.ProcessingCode, @.MessageLen, @.QueryParameterString,
@.MessageData, @.ServiceOrderID, @.NewServiceOrderFlag, @.ClientVersionNumber)
if (@.@.error != 0)
BEGIN
RAISERROR 20001 'Error in csp_MsgRequestInsert'
ROLLBACK TRAN
RETURN(1)
END
COMMIT
return (0)
END

|||

Jawahar:

Is the RequestID column unique in the MsgRequest table? That is, is the RequestID a key to this table? Also, is the data returned by this procedure used as a select list or is this potentially used as the basis for a record update?

Dave

|||

Dave,

Yes the RequestID is the Primary key

The data from the select is processed row by row using a multithreaded system. We run a windows service (multithreaded) to process the data (new orders).

Jawahar

|||

Jawahar:

There are a few other things that I need to know:

How many rows are in the MsgRequest table ?|||

You need to first determine the cause of the deadlock before trying to use locking hints. What version of SQL Server are you using? Did you enable trace flag # 1204? In SQL Server 2005, there is a new trace flag# 1222 and trace events that will help identify the cause. See link below for troubleshooting steps for SQL Server 2000:

http://support.microsoft.com/default.aspx/kb/832524/

|||

Jawahar:

Umachandar is right about the need to fully understand your deadlock. But I went ahead and attempted to mock up your select query.

First, I created an index on the MsgRequest table based on (1) ProcessingCode, (2) ClientID and (3) requestID. I figured that the requestID component was not completely necessary but I wanted to avoid some bookmark lookups so I included. When I got all done testing I tried recreating the index without the requestID component and found that when I removed the requestID component from the index my logical reads increased from 38 to 4007 so I left it in.

I was able to improve on the performance of your select query by eliminating one of the scans through the MsgRequest table. The query I used was:

select A.*
from msgRequest A
inner join
( select clientId,
max (processingCode) as max_processingCode,
min (requestID) as min_requestID
from msgRequest p
where processingCode between 0 and 1
group by clientId
) b
on a.requestID = b.min_requestID
and max_processingCode = 0
order by ReceiveDT ASC

This query returned for me the same rows as your original query was returning and ran with far less logical IO. I don't like the A.* syntax, but like you said ... Sometimes you got what you you got. Hopefully, this query will provide (1) a smaller lock profile and (2) a much smaller IO profile.

Dave

|||

Jawahar:

One thing that I failed to point out is that my test was based on a relatively low cardinality of the '0' and '1' processing codes compared to all other processing codes. I used a 1000000 row test table with 8000 rows in the 1 process state and 800 rows in the 0 process state because it looks to me like we are talking about a "new record" process.

Dave

|||

Dave

Thanks for your new suggestion on the Selection query - sorry for the late reply

I am using SQL Sever 2000

I did run the trace DBCC Trace 1204 and 3605 and found that the two SP I have listed are the ones that are involved in the Deadlock. The Select SP being the victim each time. Currently I only have an index on the RequestID column, but adding the ClientID and Processing code is a good Idea.

Out table at present has 600, 000 records. 99.33 % of the records have already been processed and have a Processing code of = 2 and we never touch those records. At any given time we should not have more than 15 to 20 records of the Processing code = 0 and 15 to 20 records Processing code = 1.

would the DBCC trace be helpful to you?

Thanks again for your suggestions I will try yor revised query. Another process that I might add is to archive Rows that have already been processd to an archive table to help reduce the size of the request table.

-jawahar

Wednesday, March 7, 2012

NOLOCK usage

If we have a select statment using the NOLOCK hint ...
will it prevent other processes from
UPDATING/INSERTING/DELETING any of the table(s) it is
using within it query? Would really like to hear the
answer from an MS tech for validity purposes. Thanks and
have a great day.Jay,
From the BOL on FROM:
NOLOCK is equal to READUNCOMMITTED
READUNCOMMITTED
Specifies that dirty reads are allowed. This means that no shared locks are
issued and no exclusive locks are honored. Allowing dirty reads can result
in higher concurrency, but at the cost of lower consistency. If
READUNCOMMITTED is specified, it is possible to read an uncommitted
transaction or to read a set of pages rolled back in the middle of the read;
therefore, error messages may result. For more information about isolation
levels, see SET TRANSACTION ISOLATION LEVEL.
So, it absolutely will not prevent other processes from doing whatever they
want to the tables. One interesting error that you can get using NOLOCK is
Error 601 "could not continue scan due to data movement."
Russell Fields (not from Microsoft)
"Jay Kusch" <Jay.Kusch@.mm-games.com> wrote in message
news:014001c3927d$c453a810$a401280a@.phx.gbl...
> If we have a select statment using the NOLOCK hint ...
> will it prevent other processes from
> UPDATING/INSERTING/DELETING any of the table(s) it is
> using within it query? Would really like to hear the
> answer from an MS tech for validity purposes. Thanks and
> have a great day.

NOLOCK statement

If you were to set up a SQL statement with 6 tables and you want to make
sure all 6 tables were not locking other tables should a NOLOCK statement be
placed after each table name?
Thanks,
select a.table_name, b.table_name, c.table_name,
d.table_name, e.table_name, f.table_name
from a (NOLOCK),
b (NOLOCK),
c (NOLOCK),
d (NOLOCK),
e (NOLOCK),
f (NOLOCK)Or you could use SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
http://www.aspfaq.com/
(Reverse address to reply.)
"Joe K." <JoeK@.discussions.microsoft.com> wrote in message
news:3FD67600-052E-4E91-8EF1-D5A7E22B1346@.microsoft.com...
> If you were to set up a SQL statement with 6 tables and you want to make
> sure all 6 tables were not locking other tables should a NOLOCK statement
be
> placed after each table name?
> Thanks,
> select a.table_name, b.table_name, c.table_name,
> d.table_name, e.table_name, f.table_name
> from a (NOLOCK),
> b (NOLOCK),
> c (NOLOCK),
> d (NOLOCK),
> e (NOLOCK),
> f (NOLOCK)
>|||As Aaron stated it may be easier to use the READ UNCOMMITTED isolation level
but you should get in the habit of using WITH when you specify hints.
While it is optional now it may not be in the next release.
Andrew J. Kelly SQL MVP
"Joe K." <JoeK@.discussions.microsoft.com> wrote in message
news:3FD67600-052E-4E91-8EF1-D5A7E22B1346@.microsoft.com...
> If you were to set up a SQL statement with 6 tables and you want to make
> sure all 6 tables were not locking other tables should a NOLOCK statement
> be
> placed after each table name?
> Thanks,
> select a.table_name, b.table_name, c.table_name,
> d.table_name, e.table_name, f.table_name
> from a (NOLOCK),
> b (NOLOCK),
> c (NOLOCK),
> d (NOLOCK),
> e (NOLOCK),
> f (NOLOCK)
>

NOLOCK SQL Query Safety

Hi,
I have a question that has to do with the safety of a NOLOCK SQL Query. The
situation includes a database that is hit very heavily by a workflow engine.
I have a request to build a app that will query the same tables as the
workflow engine and generate reports. I am worried about the reporting
application causing a deadlock, which in turn would crash the workflow
engine. I have read that NOLOCK will not issue a shared lock and not honor
a
exclusive lock, I am also aware that there is a chance that a NOLOCK query
will not return accurate information. Can a select query from one process
like a reporting application cause a error in another process like the
workflow engine in this situation? Does using NOLOCK sufficiently
eliminate the risk of a error occurring? Is the NOLOCK needed at all? Thank
you for any guidance that you could give me in this matter as it is a little
over my head.
SELECT * FROM table_name WITH (NOLOCK)Do you need live data? Have you considered (transactional) replication? That
way the reporting database can be separate from the production database.
Also consider using the READPAST locking hint instead of the NOLOCK.
ML
http://milambda.blogspot.com/|||NOLOCK will make your queries return incorrect results at lightning speed.
Here are a couple instances where NOLOCK is indicated. If you're computing
an average involving thousands of rows, and only a few rows may change
during the query, then it's probably OK to use NOLOCK. If you're
serializing updates to the tables involved in a query by using a mechanism
other than resource locks--such as an application lock--, then it's OK to
use NOLOCK.
What you should do is find out the order in which the workflow engine
obtains locks. Then you should use the appropriate transaction isolation
level to perform your query, but make sure that locks are obtained in the
same order. In addition, you can use SET DEADLOCK PRIORITY LOW to ensure
that the query will be the deadlock victim should one still occur.
"Eric" <Eric@.discussions.microsoft.com> wrote in message
news:31804F59-E569-4C33-8B14-7697EBA526FC@.microsoft.com...
> Hi,
> I have a question that has to do with the safety of a NOLOCK SQL Query.
> The
> situation includes a database that is hit very heavily by a workflow
> engine.
> I have a request to build a app that will query the same tables as the
> workflow engine and generate reports. I am worried about the reporting
> application causing a deadlock, which in turn would crash the workflow
> engine. I have read that NOLOCK will not issue a shared lock and not
> honor a
> exclusive lock, I am also aware that there is a chance that a NOLOCK query
> will not return accurate information. Can a select query from one process
> like a reporting application cause a error in another process like the
> workflow engine in this situation? Does using NOLOCK sufficiently
> eliminate the risk of a error occurring? Is the NOLOCK needed at all?
> Thank
> you for any guidance that you could give me in this matter as it is a
> little
> over my head.
> SELECT * FROM table_name WITH (NOLOCK)
>

NOLOCK sentence

Hello !!

I'm using the sentence NOLOCK for selects, but I have many sentences, Is there any way to set a parameter in the DBMS, to use NOLOCK parameter by default ?? I mean, I don't like to lock any table for selects.

Is It possible ?? How to do It (step by step) ?

Thanks !!You can set transaction isolation level for your connection:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED|||Choosing to not use locking in inherantly dangerous. It means that you can have all kinds of strange problems due to interactions with other spids (users) that can be impossible to diagnose because they are impossible to recreate.

If you want to desend into the madness, all you need to do is:SET TRANSACTION LEVEL READ_UNCOMMITTED (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_set-set_74bw.asp) Be sure to read Customizing Locking (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_con_7a_27cc.asp) AND all of the sections under it before you do this!

-PatP|||I wouldn't exaggerate the dangers of dirty reads. In a busy OLTP database there are hundreds of calls made to static tables and there is NO NEED to allow default READ COMMITTED behavior. Of course, when a DML is relying on a SELECT, then this should be seriously taken into account. Personally, I wouldn't use the SET statement to control transaction isolation level. It takes less than 10 characters to explicitly state how you want the data to be accessed by using table hints.|||From my perspective, I don't exaggerate the dangers of dirty reads. I see them as a serious problem that lots of people overlook because allowing dirty reads is easier than solving the underlying problems in their code.

While there are reasons that dirty reads are necessary, and many cases where they are convenient, I feel very strongly that dirty reads are both dangerous and overused.

Most of the time when I encounter dirty reads, it is because of financial statements that don't balance consistantly in production systems, but almost always balance in test. The test system may only have a simulated load of 50 or 100 users, which may not be enough to cause the report to be unbalanced. It will never cause it to be unbalanced the same way twice. This leads to lots and lots of hair loss if someone doesn't think to check the locking to see that the developer specified that the statements don't need balance!

-PatP|||Pat, financial statements need to be generated when there is no activity going on against the period for which the statement has been requested. Otherwise, dirty or not, you won't be able to balance it anyway.|||Pat, financial statements need to be generated when there is no activity going on against the period for which the statement has been requested. Otherwise, dirty or not, you won't be able to balance it anyway.In the ideal world that is true, but I can't even get a vistor's pass for there anymore! I have to live and code in the real world.

You are correct that final statements need to be done after the period is closed, but working statements are generated from shortly after the period starts until sometime after the period ends. When the bean counters get a statement that doesn't balance, they don't think about why it might not balance, they just scream that it doesn't. You can explain to them, and they understand that "work papers" might not always balance, but those papers have the same format/appearance as final papers and that can make the users crazy.

In some ways, this is a training issue. The users need to realize that a statement for YE 12/31 isn't complete on 06/18, and if they stopped to think about it they'd know that it wasn't complete, but that still doesn't make them willing to excuse a statement that doesn't balance as of the time it was run.

I can control what the developers do (to some extent). I have little or no control over what the users do. I fix the problems where I can.

-PatP|||I wash windows...

And string up my dbas who use (NOLOCK)

EDIT: And if you want, why not pin the code tables?

EDIT2: And if you notice they want to do that for the ENTIRE db...

WOW...holy corruption bartman!|||I think I must continue using NOLOCK clause !|||Well, I think it's more an app design issue rather than users training or NOLOCK. App design will be reconciled with db design which should have the same set of business rules as the foundation, just like the app design must. And all this results from a sound system analysis where each data/info flow is accounted for and projected onto app/db design...But, as you said, - that's an ideal world, and "they" don't pay us enough to dedicate several years of our lives to creating one :(|||The application development finished, and they are having locking problems, and I can't change it !|||Doesn't sound finished to me...

If they have that many problems...you can bet the wheels are gonna fly right off when you change the ISOLATION LEVEL

They may be complaining now...soon they will be blaimng you and the database for screwing up the data

Oh

MOO|||The application development finished, and they are having locking problems, and I can't change it !Have they finished, or are they having locking problems? The two are mutually exclusive, they can't have both.

Changing the locking level would be what we call a "Class 2 CLM (Career Limiting Move)". It might not get you fired, but whether it does or not you'll wish that it had!

I think that life is too short to volunteer to sign up for that kind of problems.

-PatP|||Thanks everybody !

NOLOCK or Not to NOLOCK

I need some help to under stand when the right time is for NOLOCK. I work in a small dev group and NOLOCK seams to be a buzz word and others are throwing it in all over for no apparent reason.

I read the thing from http://www.sql-server-performance.com/ and I am sure that our web and SQL servers are about 100x over sized for the application. While are ASP.Net (VB) app may demonstrate some hesitation from time to time I am more inclined to blame poor VB.Net coding techniques before slow SQL. The point being the NOLOCK is being added to SELECTS that are not part of a transaction and were using the SQL data adapter to return datasets or single column values.

Also I am not even sure it's being used correctly. The OLM has the example:
SELECT au_lname FROM authors WITH (NOLOCK)

However I am seeing it formatted like this:
SELECT au_lname FROM authors (NOLOCK)

I am by no mean an expert, I follow what I read in books or from examples from others. And I have never read in a book go crazy with NOLOCK because it's the bomb!

Any thoughts? I am trying to learn as much as I can before I raise my hand and say this might be a bad idea.

Thankshttp://www.sql-server-performance.com/lock_contention_tamed_article.asp|||This is the article that I read.
I guess I need to learn if I am having a lock contention problem? I have been using the Enterprise manager and watching for locks and there does not appear to be any issues. I am also trying a demo of "Spotlight on SQL" and it shows just a few database level shared locks and no blocking locks.

I am guessing I would see blocking locks if there was contention which would warrant the use of the NOLOCK correct?|||In general, I use NOLOCK when I have a problem, and generally, I see the problem when the front end is MS Access, because of the way folks tend to use Access (binding to the entire table).|||To be honest, NOLOCK used to be very helpful when you were stuck with COM+'s serialized isolation level. My biggest problem with table hints is that they are just that, hints, not instructions. You can write some lovely SQL statement that works for days, months, years. Then all of sudden it stops working, why? Because SQL has decided that it no longer wants to take notice of your hint.

So I'd say, like has already been said, use them when you *need* to use them. Normally sorting out your isolation levels will solve most the of the problems.

NOLOCK optimizer hint on iterator

An interesting discussion yesterday. One of the programmers asked about the use of the NOLOCK optimizer hint with an iterator table aka table of numbers. His comment was that this optimizer hint was not efficient. Rather than give a knee-jerk response I thought it would be better to ask. The main circumstance is that the iterator table is completely static with a fill factor of 100%. My purpose is to eliminate lock contention if I can.

Are there reasons to not use the NOLOCK hint in this case to potentially improve performance?

Dave

Dave,

I am a big fan of using NOLOCK appropriately. Generally, the NOLOCK reads dirty, ignoring locks like an UPDATE lock and would not wait for the lock to release before reading the data (helps against blocking).

However, if the table is static and there are no modification locks on the table, NOLOCK will not likely be of much help to you on this table. It is possible (MS please verify) that the NOLOCK would also skip reading the lock table so that could be something that would help if your SELECT (NOLOCK) is in a large repeating loop or process.

|||

>>His comment was that this optimizer hint was not efficient. <<

Not efficient? While I am not 100% sure that you would get noticable performance improvements by using NOLOCK except in very large query situations, it will save time by not checking or leaving locks. You won't save any contention per ce because you should never leave any exclusive locks that cause contention.

So I don't see that it will ever hurt anything to do this to a read-only table, but it could help, if just a tiny amount.

|||

>> His comment was that this optimizer hint was not efficient.

Did he say how it's not efficient? That statement doesn't make much sense. Your reply should have been: "You keep using that word. I do not think it means what you think it means."

But then, I always welcome an opportunity to quote Inigo Montoya!!!

|||

The reasoning had something to do with "excessive reading of the transaction logs." I've used the NOLOCK hint heavily since the 90's and have never noticed this supposed problem. Moreover, the hint has often been the solution to contention between reports and transaction processes. Back in the 90s I heard a similar objection from a collegue but discounted it. Since This was something I had "heard" before I felt it was better to re-verify.

Dave

NOLOCK on views

Hey guys,

I came across a SQL statement, thought up by a developer, in which two views were joined with the NOLOCK hint:
SELECT v1.xxx, v2.yyy
FROM dbo.vw_SomeView v1 WITH (NOLOCK)
INNER JOIN dbo.vw_SomeOtherView WITH (NOLOCK) ON v1.id = v2.id
The views are not created the NOLOCK hint. So my question is: has the NOLOCK hint any effect here?

I've looked in the BOL and searched on the net but can't find anything on this particular topic.

Lex

PS. Personally I don't like to use views in JOINs. I've seen too many cases in which tables are joined twice just because they are part of both views. Further more I don't like the "random" use of NOLOCK because most people don't seem to understand the implications of it. But this is besides the point of my question ;)Looks like it's time for a little hands on experiment. Take an update lock on one of the tables used in either of the views in one QA window, and try to run the sql in another.|||Looks like it's time for a little hands on experiment. Take an update lock on one of the tables used in either of the views in one QA window, and try to run the sql in another.

I use and recommend (NOLOCK) Optimizer hints on a regular basis. Just know that when a (NOLOCK) hint is used, it performs a "Dirty Read" against the data.

The primary benefit to a (NOLOCK) hint is to prevent the blocking of objects from occurring when users are selecting data. I would recommend using them if you have contention in your environment with users holding exclusive locks on tables.

Hope this helps!

NOLOCK hint on views?

Hi all

If i have a view:

CREATE VIEW vw_Users

AS

SELECT * FROM Users WITH(NOLOCK)

Is it suggested to use nolock in views?

And if i needed to use this view in stored procs is it then suggested to apply the nolock hint?

CREATE PROC [dbo] .[usp_GetCompanyUsers]

AS

SELECT * FROM Companies WITH(NOLOCK) JOIN

vw_Users WITH(NOLOCK) --<< --is this suggested?

If you using NOLOCK hint while creating view, you don't need to use the NOLOCK again when accessing the view. The locking is always done on the table and not on the view.

NOLOCK hint causes process blocking?

We recently added NOLOCK hints to our less-important queries to cut down on
deadlocks. It does decrease the deadlocks but it seemed to increase other
process blocking... the type that nearly hangs the sql server. Is that
possible? Has anyone else noticed? I am starting to think NOLOCK is a
NO-NO, and that deadlocks are better.
-Dan
What's probably happeneing is that without NOLOCK the queries were being
slowed down. Now with NOLOCK they run full out and are hitting resource
shortages elsewhere. Deadlocks are NEVER better.
How is your server nearly hanging? If you're trying to resolve deadlocks by
adding NOLOCK then it may be that your queries/updates have problems.
Are you accessing resourcing in the same sequence?
Nik Marshall-Blank MCSD/MCDBA
Linz, Austria
"Dan English" <dan_english2@.cox.net> wrote in message
news:OC8PKDE0FHA.3408@.TK2MSFTNGP09.phx.gbl...
> We recently added NOLOCK hints to our less-important queries to cut down
> on deadlocks. It does decrease the deadlocks but it seemed to increase
> other process blocking... the type that nearly hangs the sql server. Is
> that possible? Has anyone else noticed? I am starting to think NOLOCK is
> a NO-NO, and that deadlocks are better.
> -Dan
>
|||Thanks for the response. I've posted the offending stored proc in a new
post.
"Nik Marshall-Blank" <Nik@.here.com> wrote in message
news:PlJ3f.136806$vt2.119755@.fe08.news.easynews.co m...
> What's probably happeneing is that without NOLOCK the queries were being
> slowed down. Now with NOLOCK they run full out and are hitting resource
> shortages elsewhere. Deadlocks are NEVER better.
> How is your server nearly hanging? If you're trying to resolve deadlocks
> by adding NOLOCK then it may be that your queries/updates have problems.
> Are you accessing resourcing in the same sequence?
> --
> Nik Marshall-Blank MCSD/MCDBA
> Linz, Austria

NOLOCK hint causes process blocking?

We recently added NOLOCK hints to our less-important queries to cut down on
deadlocks. It does decrease the deadlocks but it seemed to increase other
process blocking... the type that nearly hangs the sql server. Is that
possible? Has anyone else noticed? I am starting to think NOLOCK is a
NO-NO, and that deadlocks are better.
-DanWhat's probably happeneing is that without NOLOCK the queries were being
slowed down. Now with NOLOCK they run full out and are hitting resource
shortages elsewhere. Deadlocks are NEVER better.
How is your server nearly hanging? If you're trying to resolve deadlocks by
adding NOLOCK then it may be that your queries/updates have problems.
Are you accessing resourcing in the same sequence?
Nik Marshall-Blank MCSD/MCDBA
Linz, Austria
"Dan English" <dan_english2@.cox.net> wrote in message
news:OC8PKDE0FHA.3408@.TK2MSFTNGP09.phx.gbl...
> We recently added NOLOCK hints to our less-important queries to cut down
> on deadlocks. It does decrease the deadlocks but it seemed to increase
> other process blocking... the type that nearly hangs the sql server. Is
> that possible? Has anyone else noticed? I am starting to think NOLOCK is
> a NO-NO, and that deadlocks are better.
> -Dan
>|||Thanks for the response. I've posted the offending stored proc in a new
post.
"Nik Marshall-Blank" <Nik@.here.com> wrote in message
news:PlJ3f.136806$vt2.119755@.fe08.news.easynews.com...
> What's probably happeneing is that without NOLOCK the queries were being
> slowed down. Now with NOLOCK they run full out and are hitting resource
> shortages elsewhere. Deadlocks are NEVER better.
> How is your server nearly hanging? If you're trying to resolve deadlocks
> by adding NOLOCK then it may be that your queries/updates have problems.
> Are you accessing resourcing in the same sequence?
> --
> Nik Marshall-Blank MCSD/MCDBA
> Linz, Austria

NOLOCK hint causes process blocking?

We recently added NOLOCK hints to our less-important queries to cut down on
deadlocks. It does decrease the deadlocks but it seemed to increase other
process blocking... the type that nearly hangs the sql server. Is that
possible? Has anyone else noticed? I am starting to think NOLOCK is a
NO-NO, and that deadlocks are better.
-DanWhat's probably happeneing is that without NOLOCK the queries were being
slowed down. Now with NOLOCK they run full out and are hitting resource
shortages elsewhere. Deadlocks are NEVER better.
How is your server nearly hanging? If you're trying to resolve deadlocks by
adding NOLOCK then it may be that your queries/updates have problems.
Are you accessing resourcing in the same sequence?
--
Nik Marshall-Blank MCSD/MCDBA
Linz, Austria
"Dan English" <dan_english2@.cox.net> wrote in message
news:OC8PKDE0FHA.3408@.TK2MSFTNGP09.phx.gbl...
> We recently added NOLOCK hints to our less-important queries to cut down
> on deadlocks. It does decrease the deadlocks but it seemed to increase
> other process blocking... the type that nearly hangs the sql server. Is
> that possible? Has anyone else noticed? I am starting to think NOLOCK is
> a NO-NO, and that deadlocks are better.
> -Dan
>|||Thanks for the response. I've posted the offending stored proc in a new
post.
"Nik Marshall-Blank" <Nik@.here.com> wrote in message
news:PlJ3f.136806$vt2.119755@.fe08.news.easynews.com...
> What's probably happeneing is that without NOLOCK the queries were being
> slowed down. Now with NOLOCK they run full out and are hitting resource
> shortages elsewhere. Deadlocks are NEVER better.
> How is your server nearly hanging? If you're trying to resolve deadlocks
> by adding NOLOCK then it may be that your queries/updates have problems.
> Are you accessing resourcing in the same sequence?
> --
> Nik Marshall-Blank MCSD/MCDBA
> Linz, Austria

NOLOCK and READPAST on same table?

Is it possible to use With (NOLOCK) and With (READPAST) in the same
SELECT query and what whould be the syntax?

@.param int

SELECT
myRow
FROM
dbo.myTable WITH (NOLOCK)
WHERE
myRow = @.param

Thanks,
lqlaurenq uantrell (laurenquantrell@.hotmail.com) writes:
> Is it possible to use With (NOLOCK) and With (READPAST) in the same
> SELECT query and what whould be the syntax?

The syntax would be

SELECT ... FROM tbl WITH (NOLOCK, READPAST)

But I got the error message:

Server: Msg 650, Level 16, State 1, Line 1
You can only specify the READPAST lock in the READ COMMITTED or
REPEATABLE READ isolation levels.

Which makes sense. READPAST means that you skip rows that you would be
blocked on, and you will not be blocked with NOLOCK.

What are you trying to achieve?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||On 17 Sep 2005 14:04:40 -0700, laurenq uantrell wrote:

>Is it possible to use With (NOLOCK) and With (READPAST) in the same
>SELECT query and what whould be the syntax?

Hi Lauren,

The NOLOCK hint specifies that all locks should be disregarded. The
READPAST hint specifies that locked rows should be skipped. This means
that these lock hints are mutually exclusive.

The syntax for combining hints is
WITH (NOLOCK, READPAST)
which will result in an error for this combination. The use of several
hints might be useful for other combinations, though:
WITH (TABLOCK, XLOCK)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)|||I have a table dbo.myTableName to which hundreds of users are
UPDATE-ing or INSERT-ing 24/7. They are also running SELECT queries
against that table 24/7.
I am tring to remove the slowdown caused by rows that might be in use
when users run a query that looks like: SELECT myID, myRow1 etc. FROM
dbo.myTableName WITH (READPAST) WHERE StartDate = @.DateParam|||So which should result in a faster scan of the table with fewer
possibilty of locking in a situation where:

I have a table dbo.myTableName to which hundreds of users are
UPDATE-ing or INSERT-ing 24/7. They are also running SELECT queries
against that table 24/7.
I am tring to remove the slowdown caused by rows that might be being
written to when users run a query that looks like: SELECT myID, myRow1
FROM dbo.myTableName WITH (READPAST) <OR> WITH (NOLOCK) WHERE StartDate
= @.DateParam

?|||On 18 Sep 2005 08:28:14 -0700, laurenq uantrell wrote:

>So which should result in a faster scan of the table with fewer
>possibilty of locking in a situation where:
>I have a table dbo.myTableName to which hundreds of users are
>UPDATE-ing or INSERT-ing 24/7. They are also running SELECT queries
>against that table 24/7.
>I am tring to remove the slowdown caused by rows that might be being
>written to when users run a query that looks like: SELECT myID, myRow1
>FROM dbo.myTableName WITH (READPAST) <OR> WITH (NOLOCK) WHERE StartDate
>= @.DateParam
>?

Hi Lauren,

Here's an answer you probably don't want to hear :-)

Try to use neither. In both cases, you'll return information that is
besides the truth. In the case of (READPAST), rows will be missing in
your result set that should be included. In the case of (NOLOCK), you'll
return data that is currently being changed, but might still be rolled
back (e.g. because it violates a business rule).

You should not be trying to get "a faster scan of the table" - you
should be trying to eliminate table scans at all. Especially on a table
that is under heavy use by hundreds of users. Making sure that all
inserts, updates and selects can use appropriate indexes will go a long
way toward preventing table scans. This will also mean that you'll spend
far less time waiting for a lock to be released on a row you didn;t want
to see after all!!

If you're still facing blocking issues after this, you might want to
consider duplicating the table: one "live" table for all the inserts and
updates, and a "reporting" copy for all the selects. Set up a routine
that will periodically (e.g. every 5 minutes, or whatever time delay is
acceptable in your situation) copy over all changes from the "live"
table to the "reporting" copy.

Now to your original question:
>So which should result in a faster scan of the table with fewer
>possibilty of locking in a situation where:
(snip)
>WITH (READPAST) <OR> WITH (NOLOCK)

The only way to find out is to test them both. If I were forced to
guess, I'd say that NOLCOK might be faster as it doesn't check for
existing locks, nor take any locks, whereas NOLOCK still checks for
locks and takes a lock if the row is not currently locked.

But as I said - that's just a guess.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)|||laurenq uantrell (laurenquantrell@.hotmail.com) writes:
> I have a table dbo.myTableName to which hundreds of users are
> UPDATE-ing or INSERT-ing 24/7. They are also running SELECT queries
> against that table 24/7.
> I am tring to remove the slowdown caused by rows that might be in use
> when users run a query that looks like: SELECT myID, myRow1 etc. FROM
> dbo.myTableName WITH (READPAST) WHERE StartDate = @.DateParam

I echo what Hugo said: try to avoid NOLOCK and READPAST as long as you
can. Rather investigate if indexes can help. A query like the one
above, might excute faster with an index on StartDate.

As for whether you should use NOLOCK or READPAST, there are two things
consider: a) what result do you want and b) and what is your blocking
problem?

a) Both NOLOCK and READPAST can result in the queries giving incorrect
result.
NOLOCK means that you read uncommitted data, which could violate business
rules, and that will be rolled back the next second (or is in fact in the
process of being rolled back). In more devilish cases an updating process
may first delete some data to re-inserted it in some new version, leading
to that you get no data at all.

This last thing is also very typical for READPAST. "SELECT SUM(amt) FROM tbl
WITH (READPAST) WHERE date = @.somdate". Oops, a bunch of rows were locked,
and you get back a value which os 40% of the right one.

If the queries that run are reports that is mainly interested in general
trends, and not used for reconcilliation etc, then it may be OK to run
with NOLOCK, but you should really investigate the consequences.

b) READPAST will not help if SELECTs that performs table scans block
UPDATE statements, the SELECT gets a lock on table level, and the updaters
will have to wait. READPAST makes sense if selects are fast, but your
UPDATE/INSERT operations are complex and long-running.

Generally, first try to see if better indexing can help. But if you have
queries that comes from search functions where the user can select
conditions wildly can be difficult to have an index for everything.
Investing in a second server for reports, may be worth the effort.

In SQL 2005 there is a new isolation level, SNAPSHOT. With this isolation
level, SELECT statements can run on a snapshot of the state of the database
in a given moment. This can help a lot to prevnent this sort of problems.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

NOLOCK

Hi
can any one tell me about the use of NOLOCK. Whrere can we use it and where
dont ?
pros and cons of the above ?
rgards laraNolock will read all records, regardless of their state, it will bypass any
locks on the table.
For example;
IF OBJECT_ID('tempdb..##tmp') IS NOT NULL DROP TABLE ##tmp
CREATE TABLE ##tmp ( ID INT PRIMARY KEY CLUSTERED , Data CHAR(1) )
INSERT INTO ##tmp
SELECT TOP 0 NULL AS ID , NULL AS Data
UNION ALL SELECT 1 , 'A'
UNION ALL SELECT 2 , 'B'
UNION ALL SELECT 3 , 'C'
UNION ALL SELECT 4 , 'D'
BEGIN TRANSACTION
INSERT INTO ##tmp VALUES ( 5 , 'E' )
-- run this on connection 2
SELECT * FROM ##tmp WITH(NOLOCK)
ROLLBACK TRANSACTION
Connection 2 will return 5 records, even though the inserted record didn't
actually get entered.
Without the nolock, the 2nd connection will wait until
connection1-transaction is rolledback or committed and return 4 records
(without the discarded 5th record).
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
where
> dont ?
> pros and cons of the above ?
> rgards lara
>|||Hi,
NoLock hint in SELECT statement will allow you to read the uncommited
transactions (Dirty reads).
Demerits:-
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
> rgards lara
>|||Look in the BOL:
NOLOCK Do not issue shared locks and do not honor exclusive locks. When
this option is in effect, it is possible to read an uncommitted
transaction or a set of pages that are rolled back in the middle of a
read. Dirty reads are possible. Only applies to the SELECT statement.
pros: can read data without waiting for a lock to be opened up for your
query, Cons: Could reflect non-acutal data.
HTH, Jens Suessmeyer.|||Hi,
NoLock hint in SELECT statement will allow you to read the uncommited
transactions (Dirty reads).
Demerits:-
NOLOCK hint will open yourself up to the risk of reading incorrect data. If
possible this should be probably be avoided.
Merits:-
Do not issue shared locks and do not honor exclusive locks. Thus blocking
can be avoided. This is not actually a merit as far as data consistency
is concerned :)
Thanks
hari
SQL Server MVP
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
> rgards lara
>|||Hi,
NoLock hint in SELECT statement will allow you to read the uncommited
transactions (Dirty reads).
Demerits:-
NOLOCK hint will open yourself up to the risk of reading incorrect data. If
possible this should be probably be avoided.
Merits:-
Do not issue shared locks and do not honor exclusive locks. Thus blocking
can be avoided. This is not actually a merit as far as data consistency
is concerned :)
Thanks
hari
SQL Server MVP
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
> rgards lara
>|||Hi,
NoLock hint in SELECT statement will allow you to read the uncommited
transactions (Dirty reads).
Demerits:-
NOLOCK hint will open yourself up to the risk of reading incorrect data. If
possible this should be probably be avoided.
Merits:-
Do not issue shared locks and do not honor exclusive locks. Thus blocking
can be avoided. This is not actually a merit as far as data consistency
is concerned :)
Thanks
hari
SQL Server MVP
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
> rgards lara
>|||Lara (lara169@.gmail.com) writes:
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
One should be very careful with NOLOCK, and if you don't understand the
exact implications of it, don't use it.
NOLOCK is probably OK if you are reading a table that has INSERT activity,
but where rows from yesterday and before are usually not affected, and
you are only reading historic data. NOLOCK can prevent that an accidental
table blocks the writers.
NOLOCK can also be OK for reading current data, if you are only interested
in trends, and the data will not be used for reconcilliation.
If the clustered index on the table are on columns that may be updated,
be extra careful - I've seen reports where SQL Server have read the same
row twice in this case.
NOLOCK queries can also result in errors that indicate serious corruption.
The errors themselves are false alarm, but they are quite ugly.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks
"Lara" <lara169@.gmail.com> wrote in message
news:ulv5R8h1FHA.3336@.TK2MSFTNGP12.phx.gbl...
> Hi
> can any one tell me about the use of NOLOCK. Whrere can we use it and
> where dont ?
> pros and cons of the above ?
> rgards lara
>

Nolock

Will nolock on queries on the concerning tables will reduce blocking in DB ?
Assumption- I am ready to do dirty reads as I am just checking counts.It can, yes -- it will keep writes from blocking reads, but not writes from
blocking other writes, which can be an issue as well in some cases.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Rect" <Rect@.discussions.microsoft.com> wrote in message
news:5B762CA5-55B8-4F66-988E-1DCA6D71EA0F@.microsoft.com...
> Will nolock on queries on the concerning tables will reduce blocking in DB
> ?
> Assumption- I am ready to do dirty reads as I am just checking counts.

Nolock

Will nolock on queries on the concerning tables will reduce blocking in DB ?
Assumption- I am ready to do dirty reads as I am just checking counts.It can, yes -- it will keep writes from blocking reads, but not writes from
blocking other writes, which can be an issue as well in some cases.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Rect" <Rect@.discussions.microsoft.com> wrote in message
news:5B762CA5-55B8-4F66-988E-1DCA6D71EA0F@.microsoft.com...
> Will nolock on queries on the concerning tables will reduce blocking in DB
> ?
> Assumption- I am ready to do dirty reads as I am just checking counts.

nolock

Hi,
i wonder how to set the nolock option.
if i use subviews do i have to go througg all of them to find all
tables and add nolock or does it affect the subqueries automatically?
is there a database switch i can set which
affects all select statements if that wont work to save time
select vp_id, Sum(summe_drawing) as summe_drawing, sum(summe_opp) as
summe_opp, sum(summe_quota) as summe_quota, product_id
from mpc_draw_opp_quota_v (nolock) '
greets mikeWhen specifying join hints use the WITH keyword, e.g.
select <column list>
from <table> with(<hint> [, ...])
Specify hints for all tables in the from clause, but be really, really
careful when using hints - they might have a negative impact on performance
and/or yield unexpected results.
ML
http://milambda.blogspot.com/|||set transaction isolation level read uncommitted
select * from yourview
Tony.
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
<peppi911@.hotmail.com> wrote in message
news:1137681397.337443.5080@.z14g2000cwz.googlegroups.com...
> Hi,
> i wonder how to set the nolock option.
> if i use subviews do i have to go througg all of them to find all
> tables and add nolock or does it affect the subqueries automatically?
> is there a database switch i can set which
> affects all select statements if that wont work to save time
> select vp_id, Sum(summe_drawing) as summe_drawing, sum(summe_opp) as
> summe_opp, sum(summe_quota) as summe_quota, product_id
> from mpc_draw_opp_quota_v (nolock) '
> greets mike
>|||NOLOCK will make your queries return incorrect results at lightning speed.
You shouldn't use it unless there is an overwhelming need to do so, or if
the impact of changes is minimal: for example, if you're computing an
average based on thousands of rows, it doesn't really matter if a few rows
are modified during the calculation. Under no circumstances should you use
NOLOCK to compute changes that are about to be applied to the database,
unless you use some other mechanism to serialize access to the source
tables. For example, if you're calculating payroll, and a change to an
employee's salary is rolled back after it's read out, then the amounts on
the paycheck will be incorrect. If, on the other hand, you use an
application lock to block updates to the employee table during the payroll
calculation, then you can use NOLOCK.
The READ UNCOMMITTED isolation level and the NOLOCK hint are often misused
by neophytes in an attempt to improve performance or to deal with deadlocks.
SQL Server 2005 has a new isolation level READ COMMITTED SNAPSHOT which can
be used to improve the performance of database queries and to reduce
blocking and deadlocks because it does not apply locks. It should NOT be
used to calculate changes to the database, however, because it's possible
for the information read out to become stale by the time the changes are
committed.
<peppi911@.hotmail.com> wrote in message
news:1137681397.337443.5080@.z14g2000cwz.googlegroups.com...
> Hi,
> i wonder how to set the nolock option.
> if i use subviews do i have to go througg all of them to find all
> tables and add nolock or does it affect the subqueries automatically?
> is there a database switch i can set which
> affects all select statements if that wont work to save time
> select vp_id, Sum(summe_drawing) as summe_drawing, sum(summe_opp) as
> summe_opp, sum(summe_quota) as summe_quota, product_id
> from mpc_draw_opp_quota_v (nolock) '
> greets mike
>