Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL Server monitoring with built-in features

$
0
0

Do you think you need to use commercial tools like SQL Diagnostic Manager, SQL Sentry, Spotlight etc.. ? Or do you think the built-in features are enough? There are complelling arguments in favor of each.

In this blog I hope to touch on every built-in feature to gather the performance information, some give real-time overviews of your databases whereas others provide detailed, on-demand data on SQL Server health and performance. I hope you learn something new.


Using T-SQL

There are several ways to use T-SQL queries to monitor SQL Server, using dynamic management views, built-in functions, stored procedures, or system data collections…

Dynamic management views

DMVs are virtual tables that you can query on adhoc or as part of your custom, automated monitoring. Some shows sql server state at a particular instant whereas others, especially those that deal with performance counters, measures values on a regular internal and show you the difference between two samples.


sys.dm_os_performance_counters DMV is of particular interest to view every performance object and counter of interest.

For example, to view the buffer manager performance object values:

SELECT object_name ,

counter_name ,

cntr_value

FROM sys . dm_os_performance_counters

WHERE object_name = 'SQLServer:Buffer Manager'

AND cntr_value != 0 ;


go

You’ll get a result similar to this:


SQL Server monitoring with built-in features

My goal here is not to teach you each dmv, rather teach you to teach yourself! You can find all such views in a system view sys.system_objects.


select top 10000 * from sys . system_objects where type_desc = 'view' order by name


SQL Server monitoring with built-in features
For example, Filter the results on specific terms lie hadr for the always on views.

select top 10000 * from sys . system_objects

where type_desc = 'view'

and name like '%hadr%'

order by


SQL Server monitoring with built-in features

HA Clusters:

select top 10000 * from sys . system_objects where type_desc = 'view'

and name like '%cluster%'

order by name


SQL Server monitoring with built-in features

To get a better understanding or each dmv, you should check out the specific documentation.

Built-in functions

Unlike dynamic management views, which return data in the form of virtual tables, built-in functions return system data as single numerical values, calculated since the server was last started. You can call each built-in function as the argument of a SELECT statement. For example, you can use the built-in function @@connections to return the sum of successful and unsuccessful connections over time:

SELECT @@connections AS "Total Connections";


Total Connections

-----------------

121


Note: @@connections increments every time a user attempts to log in (even if the attempt is unsuccessful).

The only built-in system statistics function that doesn’t return a single numerical value is sys.fn_virtualfilestats.

System stored procedures

Most stored procedures help with administrative tasks such as attaching a database or adding a login, but some stored procedures report metrics. But there are plenty that return stored meta data/configuration information.

EXEC sp_spaceused;


SQL Server monitoring with built-in features
Management Data Warehouse

Simply put, MDW is SQL Server databases to store the state and performance data are stored from one or more SQL Servers. From there reports are generated and you can also create your own custom reports.


This feature actually brings it much closer to the way the compercial monitoring tools work and someday might make the paid tools completely redundent.

It uses the SQL Server Integration Services to automate the task of querying the database and writing the results to the Management Data Warehouse.

To read more on it:

https://docs.microsoft.com/en-us/sql/relational-databases/data-collection/data-collection?view=sql-server-2017

SQL Server Management Studio Activity Monitor

The Activity Monitor makes it possible to view SQL Server metrics in real time. To use the Activity Monitor, type “Ctrl-Alt-A” or click the icon within the SSMS toolbar.


SQL Server monitoring with built-in features

To view execution plan for an expensive query, right click on the query and select Show Execution Plan:


SQL Server monitoring with built-in features
SQL Server monitoring with built-in features
Note: You can’t adjust the sizes of the graphs or the metrics they show. Nor can you change the way the Activity Monitor calculates the statistics for q

Viewing all articles
Browse latest Browse all 3160