Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does SQL Server know to lock view objects?

In SQL Server 2008 I have a view V over tables A and B that looks roughly like

create view V as
    select * from A
    union all
    select * from B

Reading from V causes a query to take intent shared locks on the base tables, but also takes an intent shared lock on the view object itself.

It is clear why we need the IS locks on the tables, and we can see that the IS lock on the view prevents concurrent modification to the tables underlying the view. That's fine.

The query plan contains no mention of the view. It's completely compiled out, and the resulting plan in this case is a simple concatenation of rows from the two base tables. Indeed the only mention of the view in the query plan XML is in the statement text.

If you add a second view U over the tables, reading from V does not cause any lock to be taken on U. This rules out that the engine just takes an IS lock on all views over A and B.

How does the database engine know to take a lock on the view?

  • Is the statement text parsed again?
  • Is there some other channel of information between the query planner and underlying execution to pass this information?

See the corresponding question on dba.stackexchange for further details.

like image 257
Joe Kearney Avatar asked Apr 23 '12 17:04

Joe Kearney


People also ask

How does locking work in SQL Server?

Locks are held on SQL Server resources, such as rows read or modified during a transaction, to prevent concurrent use of resources by different transactions. For example, if an exclusive (X) lock is held on a row within a table by a transaction, no other transaction can modify that row until the lock is released.

What is the difference between latch and lock in SQL Server?

The latch ensures memory consistency while Locks ensures logical transaction consistency. When multiple users or applications access the same data at the same time, locking prevents them from making simultaneous changes to the data. Locks are managed internally by the Microsoft SQL Server Compact Database Engine.

Does SQL Server lock on SELECT?

A SELECT in SQL Server will place a shared lock on a table row - and a second SELECT would also require a shared lock, and those are compatible with one another. So no - one SELECT cannot block another SELECT .


1 Answers

Copying from my answer on dba.stackexchange:

From Conor Cunningham, the ultimate source of anything engine- or optimizer-related:

We track things during compile to check at runtime. We do not parse things at execution for this purpose.

Note: the internals of what we do from one release to another are not guaranteed. This is beneath the officially supported surface area.

My belief is that the binary version of the execution plan (not the one that is readable and exposed to us via XML, which is only a subset of the binary version) must contain some pointer to the view(s) referenced in the original query text (and this was alluded to above). It obviously isn't parsing the query text every time. Conor implies as much above, but is careful to not reveal any details about where or how this is stored, since this could potentially change from release to release or even with a service pack or cumulative update. He probably also doesn't want to encourage any detective work. :-)

like image 115
Aaron Bertrand Avatar answered Nov 10 '22 01:11

Aaron Bertrand