广告
返回顶部
首页 > 资讯 > 数据库 >【Azure SQL】数据库性能分析
  • 308
分享到

【Azure SQL】数据库性能分析

【AzureSQL】数据库性能分析 2016-02-11 19:02:33 308人浏览 绘本
摘要

前置条件 用户有查询数据统计权限 GRANT VIEW DATABASE STATE TO database_user; CPU性能问题 正在发生 查看前X个CPU消耗查询 (汇总) SELECT TOP 10

【Azure SQL】数据库性能分析

前置条件

  • 用户有查询数据统计权限
GRANT VIEW DATABASE STATE TO database_user;

CPU性能问题

正在发生

  • 查看前X个CPU消耗查询 (汇总)
SELECT TOP 10 GETDATE() runtime, * FROM (
	SELECT query_stats.query_hash,
				SUM (query_stats.cpu_time) "Total_Request_Cpu_Time_Ms",
				SUM (logical_reads) "Total_Request_Logical_Reads",
				MIN (start_time) "Earliest_Request_start_Time",
				COUNT (*) "Number_Of_Requests",
				SUBSTRING (REPLACE(REPLACE(MIN (query_stats.statement_text),CHAR (10)," "),CHAR (13)," "),1,256) AS "Statement_Text" FROM (
				SELECT req.*,
				SUBSTRING (ST.text,(req.statement_start_offset /2)+1,((CASE statement_end_offset WHEN-1 THEN DATALENGTH(ST.text) ELSE req.statement_end_offset END-req.statement_start_offset)/2)+1) AS statement_text 
				FROM sys.dm_exec_requests AS req CROSS APPLY sys.dm_exec_sql_text (req.sql_handle) AS ST) AS query_stats 
				GROUP BY query_hash) AS t 
ORDER BY Total_Request_Cpu_Time_Ms DESC;
  • 查看前X个CPU消耗查询(按会话)
PRINT "--top 10 Active CPU Consuming Queries by sessions--"; 
SELECT TOP 10 req.session_id,req.start_time,cpu_time "cpu_time_ms",OBJECT_NAME(ST.objectid,ST.dbid) "ObjectName",
SUBSTRING (REPLACE(REPLACE(SUBSTRING (ST.text,(req.statement_start_offset /2)+1,((CASE statement_end_offset WHEN-1 THEN DATALENGTH(ST.text) ELSE req.statement_end_offset END-req.statement_start_offset)/2)+1),CHAR (10)," "),CHAR (13)," "),1,512) AS statement_text 
FROM sys.dm_exec_requests AS req CROSS APPLY sys.dm_exec_sql_text (req.sql_handle) AS ST 
ORDER BY cpu_time DESC;
Go

历史发生

  • 统计某个时间段内前X CPU耗时查询
DECLARE @nums int = 15;
DECLARE @beginTime datetime2 = DATEADD(DAY, -1,GETUTCDATE());
DECLARE @endTime datetime2 = GETUTCDATE();

WITH AggregatedCPU AS (
	SELECT q.query_hash,
	SUM (count_executions*avg_cpu_time/1000.0) AS total_cpu_millisec,
	SUM (count_executions*avg_cpu_time/1000.0)/SUM (count_executions) AS avg_cpu_millisec,
	MAX (rs.max_cpu_time /1000.00) AS max_cpu_millisec,
	MAX (max_logical_io_reads) max_logical_reads,
	COUNT (DISTINCT p.plan_id) AS number_of_distinct_plans,
	COUNT (DISTINCT p.query_id) AS number_of_distinct_query_ids,
	SUM (CASE WHEN rs.execution_type_desc="Aborted" THEN count_executions ELSE 0 END) AS Aborted_Execution_Count,
	SUM (CASE WHEN rs.execution_type_desc="Regular" THEN count_executions ELSE 0 END) AS Regular_Execution_Count,
	SUM (CASE WHEN rs.execution_type_desc="Exception" THEN count_executions ELSE 0 END) AS Exception_Execution_Count,
	SUM (count_executions) AS total_executions,MIN (qt.query_sql_text) AS sampled_query_text 
	FROM sys.query_store_query_text AS qt 
	JOIN sys.query_store_query AS q ON qt.query_text_id=q.query_text_id 
	JOIN sys.query_store_plan AS p ON q.query_id=p.query_id JOIN sys.query_store_runtime_stats AS rs ON rs.plan_id=p.plan_id 
	JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id=rs.runtime_stats_interval_id 
	WHERE rs.execution_type_desc IN ("Regular","Aborted","Exception") AND rsi.start_time>= @beginTime AND rsi.start_time < @endTime AND count_executions > 1
	GROUP BY q.query_hash),OrderedCPU AS (
		SELECT query_hash,
		total_cpu_millisec,
		avg_cpu_millisec,
		max_cpu_millisec,
		max_logical_reads,
		number_of_distinct_plans,
		number_of_distinct_query_ids,
		total_executions,Aborted_Execution_Count,
		Regular_Execution_Count,Exception_Execution_Count,
		sampled_query_text,ROW_NUMBER () OVER (ORDER BY total_cpu_millisec DESC,query_hash ASC) AS RN 
		FROM AggregatedCPU) 


SELECT OD.query_hash,OD.total_cpu_millisec,OD.avg_cpu_millisec,OD.max_cpu_millisec,OD.max_logical_reads,OD.number_of_distinct_plans,OD.number_of_distinct_query_ids,OD.total_executions,OD.Aborted_Execution_Count,OD.Regular_Execution_Count,OD.Exception_Execution_Count,OD.sampled_query_text,OD.RN 
FROM OrderedCPU AS OD 
WHERE OD.RN <= @nums
ORDER BY avg_cpu_millisec DESC;

IO性能问题

当前发生

  • 查看一个小时内每15S , IO使用情况
SELECT end_time, avg_data_io_percent, avg_log_write_percent
FROM sys.dm_db_resource_stats
ORDER BY end_time DESC;

历史发生

  • 统计时间段IO等待情况
-- top queries that waited on buffer
-- note these are finished queries
WITH Aggregated AS (SELECT q.query_hash, SUM(total_query_wait_time_ms) total_wait_time_ms, SUM(total_query_wait_time_ms / avg_query_wait_time_ms) AS total_executions, MIN(qt.query_sql_text) AS sampled_query_text, MIN(wait_category_desc) AS wait_category_desc
                    FROM sys.query_store_query_text AS qt
                         JOIN sys.query_store_query AS q ON qt.query_text_id=q.query_text_id
                         JOIN sys.query_store_plan AS p ON q.query_id=p.query_id
                         JOIN sys.query_store_wait_stats AS waits ON waits.plan_id=p.plan_id
                         JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id=waits.runtime_stats_interval_id
                    WHERE wait_category_desc="Buffer IO" AND rsi.start_time>=DATEADD(HOUR, -24, GETUTCDATE())
                    GROUP BY q.query_hash), Ordered AS (SELECT query_hash, total_executions, total_wait_time_ms, sampled_query_text, wait_category_desc, ROW_NUMBER() OVER (ORDER BY total_wait_time_ms DESC, query_hash ASC) AS RN
                                                        FROM Aggregated)
SELECT OD.query_hash, OD.total_executions, OD.total_wait_time_ms, OD.sampled_query_text, OD.wait_category_desc, OD.RN
FROM Ordered AS OD
WHERE OD.RN<=15
ORDER BY total_wait_time_ms DESC;
GO
-- Top transaction log consumers
-- Adjust the time window by changing
-- rsi.start_time >= DATEADD(hour, -24, GETUTCDATE())
WITH AggregatedLogUsed
AS (SELECT q.query_hash,
           SUM(count_executions * avg_cpu_time / 1000.0) AS total_cpu_millisec,
           SUM(count_executions * avg_cpu_time / 1000.0) / SUM(count_executions) AS avg_cpu_millisec,
           SUM(count_executions * avg_log_bytes_used) AS total_log_bytes_used,
           MAX(rs.max_cpu_time / 1000.00) AS max_cpu_millisec,
           MAX(max_logical_io_reads) max_logical_reads,
           COUNT(DISTINCT p.plan_id) AS number_of_distinct_plans,
           COUNT(DISTINCT p.query_id) AS number_of_distinct_query_ids,
           SUM(   CASE
                      WHEN rs.execution_type_desc = "Aborted" THEN
                          count_executions
                      ELSE
                          0
                  END
              ) AS Aborted_Execution_Count,
           SUM(   CASE
                      WHEN rs.execution_type_desc = "Regular" THEN
                          count_executions
                      ELSE
                          0
                  END
              ) AS Regular_Execution_Count,
           SUM(   CASE
                      WHEN rs.execution_type_desc = "Exception" THEN
                          count_executions
                      ELSE
                          0
                  END
              ) AS Exception_Execution_Count,
           SUM(count_executions) AS total_executions,
           MIN(qt.query_sql_text) AS sampled_query_text
    FROM sys.query_store_query_text AS qt
        JOIN sys.query_store_query AS q
            ON qt.query_text_id = q.query_text_id
        JOIN sys.query_store_plan AS p
            ON q.query_id = p.query_id
        JOIN sys.query_store_runtime_stats AS rs
            ON rs.plan_id = p.plan_id
        JOIN sys.query_store_runtime_stats_interval AS rsi
            ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id
    WHERE rs.execution_type_desc IN ( "Regular", "Aborted", "Exception" )
          AND rsi.start_time >= DATEADD(HOUR, -24, GETUTCDATE())
    GROUP BY q.query_hash),
     OrderedLogUsed
AS (SELECT query_hash,
           total_log_bytes_used,
           number_of_distinct_plans,
           number_of_distinct_query_ids,
           total_executions,
           Aborted_Execution_Count,
           Regular_Execution_Count,
           Exception_Execution_Count,
           sampled_query_text,
           ROW_NUMBER() OVER (ORDER BY total_log_bytes_used DESC, query_hash ASC) AS RN
    FROM AggregatedLogUsed)
SELECT OD.total_log_bytes_used,
			 (OD.total_log_bytes_used / OD.total_executions) avg_log_bytes_used,
       OD.number_of_distinct_plans,
       OD.number_of_distinct_query_ids,
       OD.total_executions,
       OD.Aborted_Execution_Count,
       OD.Regular_Execution_Count,
       OD.Exception_Execution_Count,
       OD.sampled_query_text,
       OD.RN
FROM OrderedLogUsed AS OD
WHERE OD.RN <= 15
ORDER BY total_log_bytes_used DESC;
GO

连接数查询

SELECT
    c.session_id, c.net_transport, c.encrypt_option,
    c.auth_scheme, s.host_name, s.program_name,
    s.client_interface_name, s.login_name, s.nt_domain,
    s.nt_user_name, s.original_login_name, c.connect_time,
    s.login_time
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s
    ON c.session_id = s.session_id
WHERE c.session_id = @@SPID;

服务器使用情况

SELECT  
    AVG(avg_cpu_percent) AS "Average CPU use in percent",
    MAX(avg_cpu_percent) AS "Maximum CPU use in percent",
    AVG(avg_data_io_percent) AS "Average data IO in percent",
    MAX(avg_data_io_percent) AS "Maximum data IO in percent",
    AVG(avg_log_write_percent) AS "Average log write use in percent",
    MAX(avg_log_write_percent) AS "Maximum log write use in percent",
    AVG(avg_memory_usage_percent) AS "Average memory use in percent",
    MAX(avg_memory_usage_percent) AS "Maximum memory use in percent"
FROM sys.dm_db_resource_stats;

前X个消耗最多平均CPU时间查询

SELECT TOP 10 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_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;

转载请标明出处 : https://www.cnblogs.com/WilsonPan/p/13153400.html

您可能感兴趣的文档:

--结束END--

本文标题: 【Azure SQL】数据库性能分析

本文链接: https://www.lsjlt.com/news/6739.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【Azure SQL】数据库性能分析
    前置条件 用户有查询数据统计权限 GRANT VIEW DATABASE STATE TO database_user; CPU性能问题 正在发生 查看前X个CPU消耗查询 (汇总) SELECT TOP 10 ...
    99+
    2016-02-11
    【Azure SQL】数据库性能分析
  • Azure 文档 (SQL 数据仓库, Azure SQL 数据库文档)
    Azure 文档: https://docs.azure.cn/zh-cn/#pivot=products&panel=databases SQL 数据仓库文档:https://docs.azure.cn/...
    99+
    2022-10-18
  • 如何创建一个SQL Azure数据库
    这期内容当中小编将会给大家带来有关如何创建一个SQL Azure数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。之前我们已经创建了一个SQL Azure Server...
    99+
    2022-10-18
  • 数据库SQL Azure如何迁移到本地SQL Server
    这篇文章主要介绍了数据库SQL Azure如何迁移到本地SQL Server,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 ...
    99+
    2022-10-18
  • SQL server数据库中数据完整性的分析
    小编给大家分享一下SQL server数据库中数据完整性的分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!存储在数据库中的所有...
    99+
    2022-10-18
  • MySQL数据库的性能分析
    本篇内容主要讲解“MySQL数据库的性能分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL数据库的性能分析”吧!  1. MySQL性能优化简介  在...
    99+
    2022-10-18
  • MySQL数据库性能优化之SQL优化的示例分析
    这篇文章将为大家详细讲解有关MySQL数据库性能优化之SQL优化的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。  注:这篇文章是以 MySQL 为背景,很多内容...
    99+
    2022-10-19
  • 如何用SqlPackage.exe对SQL Azure数据库做备份
    SqlPackage.exe 是一个命令行实用工具,可自动完成以下数据库开发任务:     提取:从活动的 SQL Server 或 Windows Azure SQL Databas...
    99+
    2022-10-18
  • PolarDB数据库性能实例分析
    这篇文章主要讲解了“PolarDB数据库性能实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PolarDB数据库性能实例分析”吧!赛题概览比赛总体分成...
    99+
    2022-10-19
  • SQL数据库的案例分析
    小编给大家分享一下SQL数据库的案例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!数据表PRAGMA foreign_keys =&nbs...
    99+
    2023-06-22
  • 数据库中sql完整性约束的示例分析
    小编给大家分享一下数据库中sql完整性约束的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!完整性约束  完整性约束是为了...
    99+
    2022-10-18
  • MySQL数据库服务器的性能分析
    小编给大家分享一下MySQL数据库服务器的性能分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!3.1简介性能:为完成某件任务所...
    99+
    2022-10-18
  • PHP与数据库性能分析的集成
    PHP作为一种开源的服务器端脚本语言,广泛应用于Web开发领域。为了提高服务器端应用程序的性能,开发人员需要针对系统进行性能分析,找出瓶颈所在并加以优化。在众多性能分析工具中,数据库性能分析工具是至关重要的一环。本文将探讨PHP与数据库性能...
    99+
    2023-05-16
    数据库 PHP 性能分析
  • thinkphp 数据库性能监听SQL
    如果开启数据库的调试模式的话,你可以对数据库执行的任何SQL操作进行监听,使用如下方法: Db::listen(function($sql, $time, $explain){ // 记录SQL echo $sql. ‘ [‘....
    99+
    2014-10-31
    thinkphp 数据库性能监听SQL 数据库入门 数据库基础教程 数据库 mysql
  • SQL Server数据库分区分表的示例分析
    这篇文章主要介绍SQL Server数据库分区分表的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1、 需求说明将数据库Demo中的表按照日期字段进行水平分区分表。要求数据文件按一年一个文件存储,且...
    99+
    2023-06-14
  • ORACLE数据库性能优化的示例分析
    这篇文章给大家分享的是有关ORACLE数据库性能优化的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。ORACLE数据库的优化方式和MYSQL等很大的区别,今天通过一个OR...
    99+
    2022-10-18
  • MySQL数据库性能优化的示例分析
    小编给大家分享一下MySQL数据库性能优化的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!为什么做优化??因为数据量太多了,项目部署上线再到用户使用,每天...
    99+
    2023-06-22
  • Linq To Sql性能举例分析
    这篇文章主要讲解了“Linq To Sql性能举例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linq To Sql性能举例分析”吧!Linq To Sql性能Linq To Sql性...
    99+
    2023-06-17
  • 如何分析SQL Server 数据库的设计
    如何分析SQL Server 数据库的设计,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、数据库设计的必要性在实际的软件项目中,如果系统中需要存储的数据量...
    99+
    2023-06-26
  • 数据库中sql注入的示例分析
    这篇文章将为大家详细讲解有关数据库中sql注入的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。首先可能大家都会问什么是sqlSql是数据库的一种类型,是用来存储网...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作