[SQL SERVER] [Performance] be used with caution With NoLock

[SQL SERVER] [Performance] be used with caution With NoLock


With NoLock see a question on the forum, I'm not sure who to ask whether normal use NoLock,

However, previous experience has taught me to be careful to use NoLock, let me explain the following ..

In 2003, Microsoft has documented articles about NoLock the following title

NOLOCK Optimizer tips may cause temporary damage to errors in the SQL Server error log file in

image

I was very concerned about the circle selected text.

When you use NoLock, etc. So you tell SQL Server users do not care about data accuracy and consistency,

If a user is updating data tables, it will affect other users queries (with nolock) the accuracy and consistency of the data,

User queries that may be encountered repeatedly reading the same data, the missed read or read .. midway other conditions,

I am more concerned about page splits action

(Page split here much narrative, but you can refer to the April 2011 RUN! PC I published index concept and design)

I told you only page segmentation is action-consuming system resources,

You definitely do not want users to query the data again, have to simultaneously process a page split operation,

So anyway, please be sure to reduce the frequency of occurrence of a page split.

Note:

NoLock and ReadUnCommitted the same effect.

create table mytest
(id int identity not null,
data uniqueidentifier default(newid()) not null 
)
- New data
declare @i int;set @i=1;
while @i<=30000
begin
insert mytest default values  
set @i=@i+1
end
- create a unique clustered index
create unique clustered index cidx on mytest( data )
- Confirm the use of space
exec sp_spaceused mytest
go

image

Now I look to simulate the effect of parallel

Connection1 the following Script1

declare @totalrows int
,@currentnow int
,@errorcount tinyint
 
set @errorcount = 0
select @totalrows= count(1) from mytest 
 
while 1 = 1
begin
    --waitfor delay '00:00:00.200'
    select @currentnow= count(1) from mytest WITH(NOLOCK,INDEX = cidx )  
    if @totalrows <> @currentnow
    begin
        print 'Total items now query =' + cast (@currentnow as varchar (10)) + 
        'Items difference =' + cast (@currentnow - @totalrows as varchar (10))
        set @errorcount = @errorcount + 1
        if @errorcount >= 8
            break
    end
end
 
 

Connection2 the following Script2

begin tran mytran
update mytest
    = data set change ()

result:

image

Script1 there are no new or deleted, but the query Total items will actually get different values,

These situations I mentioned earlier is repeated to read or missed read .. and so on,

While most Nolock can avoid Blocking (blocking) problem,

But Nolock it also brings another more troublesome problem, please take caution.

reference

Parallel effect

Table hint (Transact-SQL)

Original: Large column  [SQL SERVER] [Performance] be used with caution With NoLock


Guess you like

Origin www.cnblogs.com/petewell/p/11445690.html