📜 ⬆️ ⬇️

Simple monitoring of SQL Server activity. Who is active?

What am I talking about?


Any database administrator probably had to deal with the fact that everything works slowly, or does not work at all. The first thing you need to do is figure out what is going on in SQL Server at the moment. It seemed that there would be so many useful pieces in the administrator’s arsenal: a guided Activity Monitor, a bunch of Dynamic Management Views (dmv), sp_who and sp_who2 stored procedures, inherited from the days of SQL Server 7 and SQL Server 2000.
But let's see ...

Monitoring tools


Activity Monitor

It would seem that a great thing, is engaged in just what you need - monitors the activity. I run a heavy accounting report and watch what the Activity Monitor will show me.
The screenshots monitor activity from SQL Server 2005:
image
and from SQL Server Denali (2012) CTP 3.
image
M-yes. And if a dozen people run such reports? But this is not a rarity ... It will be rather inconvenient to deal with it, although, of course, progress is obvious. In the Denali Activity Monitor it shows a lot more useful information (for example, on which particular resource is waiting), plus, we can, for example, run the profiler directly from the monitor for the necessary session and track it already in the profiler, but, damn it, it additionally loads and already loaded server. In addition, the problem with the brakes is already there, and those requests that at the time of launching the profiler have already started to be executed, we will not see.
And I want to see exactly this - who and what is performing right now.

sp_who and sp_who2

In the screenshot, the result of executing sp_who (above) and sp_who2 (below), executed while building the same unfortunate report:
image
Yeah. Very informative. Looking at sp_who we can see only that something is being executed. Of course it is executed - we are looking at it, and we see that some SELECT is executed. Or some SELECT'ov. Great.
sp_who2 shows already more information. Now we can see how much processor time was spent by the session (and probably add up the total time, probably) the number of i / o operations, the name of the database in which all this is done and who blocked this session (if it is locked).
Activity Monitor, as we see, gives more information.

DMV

Beginning with SQL Server 2005, we received a new opportunity to obtain information about the state of the server - Dynamic Management Views . MSDN says this: “Dynamic administrative views and functions return server state data that can be used to monitor the health of a server instance, diagnose problems, and tune performance.”
Indeed, in 2005, SQL Server had a set of views related to the execution of queries at the moment (however, there are also views to view the “history”): here they are . And their number, from version to version continues to increase!
Surely, mastable administrators have a lot of scripts at their disposal to get information about the current state of the server, but what to do if there is no experience with DMV yet, and there are already problems?
')

sp_WhoIsActive


Adam Machanic (SQL Server MVP and MCITP) has developed and is constantly refining the stored procedure sp_WhoIsActive, which relies on these very DMVs and is pretty darn easy to use. Download the latest version of sp_WhoIsActive here . Adam himself has a series of articles devoted to sp_WhoIsActive, consisting of as many as 30 (thirty!) Pieces, you can read it here , and I will try to interest you in reading this material :).
So, we will assume that you downloaded and launched this script on one of the test servers (on any version from 2005 to Denali). Adam advises to store it in the master system database so that it can be called in the context of any database, but this is not necessary, just when calling it in the context of another database, you have to write the name completely - BD.schema.sp_whoIsActive.
So, let's try. In the screenshot, the result of its execution at the time of building the same report:
image
The result of the exec sp_whoIsActive query is, alas, not in one screen, so here’s a text description of the output of the stored procedure called without parameters.

The remaining columns in the standard sp_WhoIsActive output are of little interest, and I will not describe them - I think their purpose is clear to everyone (host_name, database_name, program_name, start_time, login_time, request_id, collection_time).

And what? It's all?


No, that's not all. I will also talk about with what (the most interesting and useful, from my point of view) parameters you can call sp_WhoIsActive and what will come of it.


Now everything


As a result, we have another extremely convenient and flexible tool for tracking current activity on SQL Server. For its normal operation, the permission of VIEW SERVER STATE and the rights to access dmv are sufficient.
It is also worth adding, in the case when the server can only be connected via Dedicated Admin Connection , the sp_whoIsActive call goes off with a bang, while the Activity Monitor, alas, cannot be started.

Source: https://habr.com/ru/post/136481/


All Articles