dbTalk Databases Forums  

select rows database lock?

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss select rows database lock? in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
gl
 
Posts: n/a

Default select rows database lock? - 02-25-2005 , 08:47 AM






When in the sql enterprise manager, if you right click a table and go to open
table--> select all rows, does that "lock" the database? As in if you select
all rows will someone else trying to read the data (via app or web interface)
and update it be locked out of it? We're often told that we shouldn't use
this method at our work and we should only run statements with nolocks on
live data. I had always thought that the data only locks when you put the
cursor in one of the rows to actually edit it.

What is the correct ruling? I could do tests myself but i'm not sure they
would be accurate. I mainly want to know what happens when you select all
rows (or select top) but don't put your cursor in the data.

Reply With Quote
  #2  
Old   
Sue Hoegemeier
 
Posts: n/a

Default Re: select rows database lock? - 02-25-2005 , 09:37 AM






Enterprise Manager wasn't designed to be a client data tool
so how it retrieves records from large tables is not
necessarily very efficient.
Enterprise Manager will need to consume the entire recordset
to have the locks released. You need to scroll to the last
record in Enterprise Manager to have the locks
released. You would have an IS lock on the table and a
shared lock on the specific page you are on until moving to
the end of the result set.
A shared lock is not compatible with an exclusive lock.
Exclusive locks are needed for updates.

-Sue

On Fri, 25 Feb 2005 05:47:02 -0800, "gl"
<gl (AT) discussions (DOT) microsoft.com> wrote:

Quote:
When in the sql enterprise manager, if you right click a table and go to open
table--> select all rows, does that "lock" the database? As in if you select
all rows will someone else trying to read the data (via app or web interface)
and update it be locked out of it? We're often told that we shouldn't use
this method at our work and we should only run statements with nolocks on
live data. I had always thought that the data only locks when you put the
cursor in one of the rows to actually edit it.

What is the correct ruling? I could do tests myself but i'm not sure they
would be accurate. I mainly want to know what happens when you select all
rows (or select top) but don't put your cursor in the data.


Reply With Quote
  #3  
Old   
David Gugick
 
Posts: n/a

Default Re: select rows database lock? - 02-25-2005 , 09:41 AM



gl wrote:
Quote:
When in the sql enterprise manager, if you right click a table and go
to open table--> select all rows, does that "lock" the database? As
in if you select all rows will someone else trying to read the data
(via app or web interface) and update it be locked out of it? We're
often told that we shouldn't use this method at our work and we
should only run statements with nolocks on live data. I had always
thought that the data only locks when you put the cursor in one of
the rows to actually edit it.

What is the correct ruling? I could do tests myself but i'm not sure
they would be accurate. I mainly want to know what happens when you
select all rows (or select top) but don't put your cursor in the data.
You should not use SQL EM for browsing tables because it does leave
locks on unfetched pages on the server. You can easily test this
yourself:

1- Pick any medium sized table
2- Open it up in SQL EM (don't scroll the table at all at this point)
3- Run Profiler and use the default template
4- Back in SQL EM drag the scroll bar to the end of the table to force
all rows to be fetched
5- Go back to Profiler and you'll see the SQL:BatchCompleted event pop
up


--
David Gugick
Imceda Software
www.imceda.com



Reply With Quote
  #4  
Old   
gl
 
Posts: n/a

Default Re: select rows database lock? - 02-25-2005 , 11:25 AM



So if i go to open sql-->query then put a NOLOCK on the base select statement
will that solve the problem?

So what i'm getting here is the database is locked until the you scroll to
the last record? Is the best way of viewing live data just typing out query
statements with a nolock in the sql query tool then?

"Sue Hoegemeier" wrote:

Quote:
Enterprise Manager wasn't designed to be a client data tool
so how it retrieves records from large tables is not
necessarily very efficient.
Enterprise Manager will need to consume the entire recordset
to have the locks released. You need to scroll to the last
record in Enterprise Manager to have the locks
released. You would have an IS lock on the table and a
shared lock on the specific page you are on until moving to
the end of the result set.
A shared lock is not compatible with an exclusive lock.
Exclusive locks are needed for updates.

-Sue

On Fri, 25 Feb 2005 05:47:02 -0800, "gl"
gl (AT) discussions (DOT) microsoft.com> wrote:

When in the sql enterprise manager, if you right click a table and go to open
table--> select all rows, does that "lock" the database? As in if you select
all rows will someone else trying to read the data (via app or web interface)
and update it be locked out of it? We're often told that we shouldn't use
this method at our work and we should only run statements with nolocks on
live data. I had always thought that the data only locks when you put the
cursor in one of the rows to actually edit it.

What is the correct ruling? I could do tests myself but i'm not sure they
would be accurate. I mainly want to know what happens when you select all
rows (or select top) but don't put your cursor in the data.



Reply With Quote
  #5  
Old   
David Gugick
 
Posts: n/a

Default Re: select rows database lock? - 02-25-2005 , 01:40 PM



gl wrote:
Quote:
So if i go to open sql-->query then put a NOLOCK on the base select
statement will that solve the problem?

So what i'm getting here is the database is locked until the you
scroll to the last record? Is the best way of viewing live data just
typing out query statements with a nolock in the sql query tool then?

Yes, I would use a query tool like QA instead.

--
David Gugick
Imceda Software
www.imceda.com


Reply With Quote
  #6  
Old   
Roman Rehak
 
Posts: n/a

Default Re: select rows database lock? - 02-28-2005 , 12:22 AM



NOLOCK helps, but you also have to attach it to every table in the select
statement. It's really best to stay away from viewing data in EM. I learned
the hard way that it can interfere with production applications. I did some
experiments and it also seemed that whether locking occurred or not was
really unpredictable, especially with views.

Roman

Roman Rehak, MCSD, MCDBA, MCSA
SQL Server Specialist
Competitive Computing
http://sqljunkies.com/WebLog/Roman


"gl" <gl (AT) discussions (DOT) microsoft.com> wrote

Quote:
So if i go to open sql-->query then put a NOLOCK on the base select
statement
will that solve the problem?

So what i'm getting here is the database is locked until the you scroll to
the last record? Is the best way of viewing live data just typing out
query
statements with a nolock in the sql query tool then?

"Sue Hoegemeier" wrote:

Enterprise Manager wasn't designed to be a client data tool
so how it retrieves records from large tables is not
necessarily very efficient.
Enterprise Manager will need to consume the entire recordset
to have the locks released. You need to scroll to the last
record in Enterprise Manager to have the locks
released. You would have an IS lock on the table and a
shared lock on the specific page you are on until moving to
the end of the result set.
A shared lock is not compatible with an exclusive lock.
Exclusive locks are needed for updates.

-Sue

On Fri, 25 Feb 2005 05:47:02 -0800, "gl"
gl (AT) discussions (DOT) microsoft.com> wrote:

When in the sql enterprise manager, if you right click a table and go
to open
table--> select all rows, does that "lock" the database? As in if you
select
all rows will someone else trying to read the data (via app or web
interface)
and update it be locked out of it? We're often told that we shouldn't
use
this method at our work and we should only run statements with nolocks
on
live data. I had always thought that the data only locks when you put
the
cursor in one of the rows to actually edit it.

What is the correct ruling? I could do tests myself but i'm not sure
they
would be accurate. I mainly want to know what happens when you select
all
rows (or select top) but don't put your cursor in the data.





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.