SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


While, variable and table with nolock


While, variable and table with nolock

Author
Message
salliven
salliven
Mr or Mrs. 500
Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)

Group: General Forum Members
Points: 540 Visits: 678
Hello!

When I looping a variable from a table with nolock hint in the while, then the execution time is bigger.
Why?
Here is the script:



use tempdb
go

set nocount on;
set xact_abort on;
go

if OBJECT_ID('tempdb..tmp_zs_456') is not null drop table tmp_zs_456
go
create table tmp_zs_456 (id int primary key identity(1,1), szam int default 0)
go


declare
@id int = 0,
@from_id int,
@to_id int = 10000,
@end_time datetime,
@start_time datetime

while @id < 1000000 begin
set @id += 1
if @@TRANCOUNT = 0 begin tran
insert into tmp_zs_456 default values
if @id % 5000 = 0 AND @@TRANCOUNT <> 0 commit
end

if @@TRANCOUNT <> 0 commit

-- Loop 1: the execution time is ok.
select @start_time = GETDATE(), @from_id = 0
while @from_id < @to_id begin
select top 1 @from_id += 1 from tmp_zs_456
--select top 1 @from_id += 1 from sys.databases
end

set @end_time = GETDATE()

select DATEDIFF(millisecond, @start_time, @end_time) as execution_time_without_nolock

-- Loop 2: the execution time is bigger. Why?
select @start_time = GETDATE(), @from_id = 0
while @from_id < @to_id begin
select top 1 @from_id += 1 from tmp_zs_456 with (nolock) -- Here is the execution time is bigger.
--select top 1 @from_id += 1 from sys.databases with (nolock) -- This is ok.
end

set @end_time = GETDATE()

select DATEDIFF(millisecond, @start_time, @end_time) as execution_time_with_nolock




Thanks
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)

Group: General Forum Members
Points: 9140 Visits: 8492
Because nolock is not "go faster stripes" ? More seriously, probably because the nolock hint is performing a scan which needs to read more pages due to reading in allocation order.

Check out these links for more reasons not to use nolock:
Missing rows with nolock
Allocation order scans with nolock
Consistency issues with nolock
Transient Corruption Errors in SQL Server error log caused by nolock
Dirty reads, read errors, reading rows twice and missing rows with nolock


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search