473,765 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
lq

Sep 17 '05 #1
6 8523
laurenq uantrell (la************ *@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, es****@sommarsk og.se

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

Sep 17 '05 #2
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)
Sep 17 '05 #3
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

Sep 18 '05 #4
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

?

Sep 18 '05 #5
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)
Sep 18 '05 #6
laurenq uantrell (la************ *@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, es****@sommarsk og.se

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

Sep 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5735
by: TheTamdino | last post by:
Hello all, I've tried to work this one out myself, but appearantly my brain is just a little wacked right now and I need some feedback. I'm trying to design a genealogy database (mainly because most databases out there don't do what I want them to). I'm creating what is essentially a "People" table. In this People table I need to have three fields which look up other names from within the same table: Father, Mother, and Spouse. ...
6
27862
by: Roy Gourgi | last post by:
Hi, I am trying to add a row to my table but I get the error message "invalid column name SOBN and BN1" on this statement. Basically, I am trying to add the row into the same table that I am searching if it does not find SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary key? Is it possible to have more than 1 primary key (i.e. secondary ....... ). strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1...
3
518
by: tlyczko | last post by:
I have a main form with 2 subforms, each subform references different data in the same table. How do I make sure each subform only creates NEW records in the referenced table and do not overwrite or change data in previously created rows?? Otherwise I have to use two different tables, with similar kinds of data, for each subform, which may well be the way to go even though it's not really normalized, to make the data entry easier and...
11
10154
by: FreeToGolfAndSki | last post by:
Hi, I have an Orders Table that has Employee1 and Employee2 (one is the sales rep the other is the telemarketing rep) Both of these fields need to cascade update against the Employees table. I can't seem to create the desired relationship in a Diagram and I'm not sure how best to set this up. Any ideas? Thanks in advance...
1
1891
by: keliie | last post by:
I have a relatively simple (I assume) issue which I am at a complete loss to address. My issues is: I want to populate fields in my tables with summary data from the same table. Let me explain: tblItemDetails (contains data on food products purchased) Item_Description_ID (key, source link to tblMenuItemRecipe) Item_Unit_of_Measure Item_Location Item_Type Item_Category
0
1134
by: whome | last post by:
Hello, in the query below, i'm trying to compare the first 3 characters of the symbol value ASymbol with first 3 characters of FinSymbol which are in the same table. You can have more than one FinSymbol in the table. is there a way to compare ASymbol to all FINSymbols? The query should only give results when ASymbol does not match any FINSymbols currently, this query is giving me results when ASymbol=XXX FinSymbol=YYY but my...
22
6656
by: DreamersDelight | last post by:
Hi, I'm stuck on this problem and I can't find a sollution. I'm going to try and explain this step by step. 1 After certain rows get updated with a certain value. I don't know wich rows in advance. And they do not have a certain order, but after the update they all have the same trans_id. (in unload_details_tab). 2 I select select the rows using the value that just got updated (trans_id), this way I know wich rows I needed to select, the...
0
1328
by: Moor | last post by:
I'm unable to to open a linked table T that is stored in an Access 2.0 MDB database file (shared/back-end,) using both Access 2.0 and Access 2003, in the same time. Here is the scenario: 1- Start Access 2.0 and open the linked table T. 2- Start Access 2003 then try to open the same linked table T. 3- You may get the error "Could not use <.mdb file name>; file already in use."
2
2928
dlite922
by: dlite922 | last post by:
I have a permission table that gives a userID permission to a module and the any actions within that module. What I want to do is duplicate his permissions to another user. Permission table has: id (PK) userID (FK) module action
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.