Is it valid to use nolock in 'System catalog views' and 'System dynamic management views'

博雄 胡 545 Reputation points
2024-12-20T03:32:55.6166667+00:00

and if it is valid does it mean that nolock is blocked when not used?

I use trace 1200 to observe that there are some differences between using nolock and not using nolock, and some are not. Is there a uniform rule?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 115.6K Reputation points MVP
    2024-12-20T22:47:07.0633333+00:00

    It's valid in the sense that you don't get any error. Whether they have an effect is another matter.

    For most DMVs there is not really anything you can lock, since you are reading from internal structures in the engine, and they are anything but stable. If you join a couple of DMVs, the result you get may not be fully consistent, because things change while your query is running. And, of course, you don't want the entire server to stall when you run your diagnostic query.

    When it comes to the catalog views, you are reading physical tables. However, the locks taken are not always the same as when you read normal tables. For instance, when you read a row in sys.objects, you are not taking a shared lock on that table, you take a schema-stability lock (Sch-S), and you take that lock, NOLOCK or not. Thus when reading catalog views, you can be blocked if there is a DDL transaction.

    This also applies to some DMVs, for instance those that retrieve SQL text and query plans.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.