博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Windows Azure] Monitoring SQL Database Using Dynamic Management Views
阅读量:7042 次
发布时间:2019-06-28

本文共 5803 字,大约阅读时间需要 19 分钟。

Monitoring Windows Azure SQL Database Using Dynamic Management Views

5 out of 7 rated this helpful -

Microsoft Windows Azure SQL Database enables a subset of dynamic management views to diagnose the performance problems, which might be caused by blocked or long-running queries, resource bottlenecks, poor query plans, and so on. This topic provides information on how to detect common performance problems by using the dynamic management views in Windows Azure SQL Database.

Windows Azure SQL Database partially supports three categories of dynamic management views:

  • Database-related dynamic management views.
  • Execution-related dynamic management views.
  • Transaction-related dynamic management views.

For a list of fully supported dynamic management views in Windows Azure SQL Database, see . For detailed information on dynamic management views, see in SQL Server Books Online.

Permissions

In Windows Azure SQL Database, querying a dynamic management view requires VIEW DATABASE STATE permissions. The VIEW DATABASE STATE permission returns information about all objects within the current database.

To grant the VIEW DATABASE STATE permission to a specific database user, run the following query:

 
GRANT VIEW DATABASE STATE TO database_user;

In an instance of on-premise SQL Server, dynamic management views return server state information. In Windows Azure SQL Database, they return information regarding your current logical database only.

noteNote
When executing the sys.dm_exec_requests and sys.dm_exec_sessions views, if the user has VIEW DATABASE STATE permission on the database, the user will see all executing sessions on the database; otherwise, the user will see only the current session.

 

 

Calculating Database Size

You are billed for the edition and the capacity of your SQL Databases. If the size of your database reaches its MAXSIZE you will receive an error code 40544. You cannot insert or update data, or create new objects (such as tables, stored procedures, views, and functions) unless you update the MAXSIZE of your database or delete data. For more information, see . The sys.dm_db_partition_stats view returns page and row-count information for every partition in the database, which can be used to calculate database size.

The following query returns the size of your database (in megabytes):

 
-- Calculates the size of the database. SELECT SUM(reserved_page_count)*8.0/1024FROM sys.dm_db_partition_stats; GO

The following query returns the size of individual objects (in megabytes) in your database:

 
-- Calculates the size of individual database objects. SELECT sys.objects.name, SUM(reserved_page_count) * 8.0 / 1024FROM sys.dm_db_partition_stats, sys.objects WHERE sys.dm_db_partition_stats.object_id = sys.objects.object_id GROUP BY sys.objects.name; GO

Monitoring Connections

You can use the view to retrieve information about the connections established to a specific SQL Database server and the details of each connection. In addition, the sys.dm_exec_sessions view is helpful when retrieving information about all active user connections and internal tasks.

The following query retrieves information on the current connection:

Transact-SQL
-- monitor connectionsSELECT      e.connection_id,      s.session_id,      s.login_name,      s.last_request_end_time,      s.cpu_timeFROM      sys.dm_exec_sessions s      INNER JOIN sys.dm_exec_connections e      ON s.session_id = e.session_idGO

Monitoring Query Performance

Slow or long running queries can consume significant system resources. This section demonstrates how to use dynamic management views to detect a few common query performance problems. For detailed information, see article on Microsoft TechNet.

Finding Top N Queries

The following example returns information about the top five queries ranked by average CPU time. This example aggregates the queries according to their query hash, so that logically equivalent queries are grouped by their cumulative resource consumption.

Transact-SQL
-- Find top 5queriesSELECT TOP 5 query_stats.query_hash AS "Query Hash",     SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",    MIN(query_stats.statement_text) AS "Statement Text"FROM     (SELECT QS.*,     SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,    ((CASE statement_end_offset         WHEN -1 THEN DATALENGTH(st.text)        ELSE QS.statement_end_offset END             - QS.statement_start_offset)/2) + 1) AS statement_text     FROM sys.dm_exec_query_stats AS QS     CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_statsGROUP BY query_stats.query_hashORDER BY 2 DESC;GO

Monitoring Blocked Queries

Slow or long-running queries can contribute to excessive resource consumption and be the consequence of blocked queries. The cause of the blocking can be poor application design, bad query plans, the lack of useful indexes, and so on. You can use the sys.dm_tran_locks view to get information about the current locking activity in your SQL Database. For example code, see in SQL Server Books Online.

Monitoring Query Plans

An inefficient query plan also may increase CPU consumption. The following example uses the sys.dm_exec_query_stats view to determine which query uses the most cumulative CPU.

Transact-SQL
-- Monitor query plansSELECT    highest_cpu_queries.plan_handle,      highest_cpu_queries.total_worker_time,     q.dbid,     q.objectid,     q.number,     q.encrypted,     q.[text] FROM     (SELECT TOP 50          qs.plan_handle,          qs.total_worker_time      FROM         sys.dm_exec_query_stats qs      ORDER BY qs.total_worker_time desc) AS highest_cpu_queries      CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS q ORDER BY highest_cpu_queries.total_worker_time desc

See Also

Concepts

转载于:https://www.cnblogs.com/licheng/p/3266395.html

你可能感兴趣的文章
DOS windows 使用bat脚本获取 IP MAC 系统信息
查看>>
PHP给图片添加水印
查看>>
Yaf学习(三)----Yaf类库Library和Model的命名规则
查看>>
蓝桥学院2019算法题2.1-2.5
查看>>
浅谈CLR CTS CLS。。。
查看>>
软件需求十步走读书笔记2
查看>>
WPF学习05:2D绘图 使用Transform进行控件变形
查看>>
APB协议
查看>>
系统调用与库函数
查看>>
Mysql运维管理-MySQL数据库存储引擎知识19
查看>>
[leetcode-553-Optimal Division]
查看>>
[leetcode-647-Palindromic Substrings]
查看>>
UEditor
查看>>
nowcoder N约数个数
查看>>
Test a ; vs Test a( ) ;
查看>>
企业架构研究总结(4)——企业架构与企业架构框架概论
查看>>
yarn的学习之2-容量调度器和预订系统
查看>>
提取网页数据
查看>>
基于链接服务器的跨服务器查询
查看>>
lemp(lnmp)web网站搭建
查看>>