python 目标直方图

An Extended events target is the destination for all of the information that is captured by Extended Events sessions. You can rely on couple of different targets such as event counter, event file, event tracing for Windows (ETW), ring buffer, event pairing, and histogram.

扩展事件目标是扩展事件会话捕获的所有信息的目标。 您可以依赖几个不同的目标,例如事件计数器,事件文件,Windows的事件跟踪(ETW),环形缓冲区,事件配对和直方图

A Histogram target is used to count the number of times a specific event occurs, based on a specified column or action. It provides grouping of a specific event within the event session. We can group data based on data within the event itself or to an action that has been additionally added to the event. Being relatively lightweight it can be used for a gathering information over a large time period, the same cannot be said for most of the other extended event targets.

直方图目标用于根据指定的列或操作对特定事件发生的次数进行计数。 它提供事件会话中特定事件的分组。 我们可以根据事件本身中的数据或已添加到事件中的操作对数据进行分组。 由于它相对较轻,因此可以在很长的一段时间内用于收集信息,而对于大多数其他扩展事件目标而言,则不能这么说。

The histogram is an asynchronous target which means that the collected data is stored within the memory buffer first and then transferred to the target. It is possible that events are not transferred immediately and there is a delay caused from the buffer flushes. The captured information is lost upon SQL Server restart.

直方图是一个异步目标,这意味着收集的数据首先存储在内存缓冲区中,然后再传输到目标。 事件可能不会立即传输,并且缓冲区刷新会导致延迟。 SQL Server重新启动后,捕获的信息将丢失。

We will cover the creation of an extended event session, the configuration possibilities of a histogram target, and extracting the collected data with TSQL.

我们将介绍扩展事件会话的创建,直方图目标的配置可能性以及使用TSQL提取收集的数据。

Let us start by creating an extended events session that will capture information for SQLOS waits. This event occurs when there is a wait on a SQLOS controlled resource. The configured action is capturing the database_id in addition.

让我们首先创建一个扩展事件会话,该会话将捕获SQLOS等待的信息。 当等待SQLOS控制的资源时,将发生此事件。 配置的操作正在捕获另外的database_id。


CREATE EVENT SESSION [XE_collect_wait_stats_db] ON SERVER
ADD EVENT sqlos.wait_info(ACTION(sqlserver.database_id))
GO

So far, the collected data is stored only within the memory buffer. We can access it using the ‘Watch live data’ option from the SQL Server management studio. The collected data consists of events each representing a single SQLOS wait.

到目前为止,收集的数据仅存储在内存缓冲区中。 我们可以使用SQL Server管理工作室中的“观看实时数据”选项来访问它。 收集的数据由每个代表一个SQLOS等待的事件组成。

Next, we will create a histogram target for the session:

接下来,我们将为该会话创建一个直方图目标:


ALTER EVENT SESSION [XE_collect_wait_stats_db] ON SERVER
ADD TARGET package0.histogram(SET slots = 64, filtering_event_name=N'sqlos.wait_info',source=N'sqlserver.database_id',source_type=(1))
GO

The histogram target has four configuration options. Although none of them are mandatory, without them we have almost no control over the collected data.

直方图目标具有四个配置选项。 尽管它们都不是强制性的,但没有它们,我们几乎无法控制所收集的数据。

slots – a user defined value indicating the maximum number of groupings. When the limit is reached, new events that do not belong to any of the already existing groupings will be ignored. The default value is 256 and for performance reasons the slot number is rounded up to the next power of 2;

slot –用户定义的值,指示最大分组数量。 当达到限制时,不属于任何现有分组的新事件将被忽略。 默认值为256,并且出于性能原因,插槽号会四舍五入到下一个2的幂。

filtering_event_name – a user-specified value that is used to identify a class of events for the histogram, all other events are ignored;

filter_event_name –用户指定的值,用于标识直方图的事件类别,所有其他事件均被忽略;

source – the name of the event column or action name used as data source;

–用作数据源的事件列的名称或操作名称;

source_type – indicates the type of object that the bucker is based on, 0 when source is event, 1 when source is action. The default value is 1.

source_type –指示合并程序所基于的对象的类型, source为事件时为0,source为动作时为1。 预设值是1。

The data that we have collected so far within the histogram target is as follows:

到目前为止,我们在直方图目标中收集的数据如下:

The histogram target has five buckets created at the moment (or slots). More buckets will be created if they are required until the limit of 64 is reached (slots = 64). After the limit is reached, if new information is captured and it does not belong to any of the existing buckets, it will be discarded.

直方图目标目前创建了五个存储桶 (或slot )。 如果需要它们,则会创建更多的存储桶,直到达到64个限制(插槽= 64)。 达到限制后,如果捕获了新信息,但该信息不属于任何现有存储桶,则将其丢弃。

The column value is showing data grouped by the database_id (we are running only with the four system databases, the value 0 indicates a wait_info event that is not related to a specific database).

显示按database_id分组的数据 (我们仅在四个系统数据库上运行,值0表示与特定数据库无关的wait_info事件)。

The column count is representing the number of wait_info events that were generated and captured by our session for each of the specified database_id buckets.

表示我们的会话为每个指定的database_id存储桶生成和捕获的wait_info事件的数量。

Let us go the other way around and create a histogram target with source type based on an event.

让我们走另一条路,创建一个基于事件的源类型的直方图目标。

This way we can choose to show the information collected by the wait_info events not grouped by the database_id but based on the wait type information.

这样,我们可以选择显示由wait_info事件收集的信息,这些事件不是按database_id分组的,而是基于等待类型的信息。

First, we need to drop the existing histogram target, we are limited to only one histogram target for an extended event session. The same is applicable for the other extended events targets as well.

首先,我们需要删除现有的直方图目标,对于扩展事件会话,我们仅限于一个直方图目标。 这同样适用于其他扩展事件目标。


ALTER EVENT SESSION [XE_collect_wait_stats_db] ON SERVER
DROP TARGET package0.histogram

Then, we can add a new histogram target. Note that the source_type is now set to 0, this is because we using event as a source, and the event is wait_info.

然后,我们可以添加一个新的直方图目标。 请注意, source_type现在设置为0,这是因为我们使用event作为源,并且该事件是wait_info


ALTER EVENT SESSION [XE_collect_wait_stats_db] ON SERVER
ADD TARGET package0.histogram(SET slots = 64, filtering_event_name=N'sqlos.wait_info', source=N'wait_type',source_type=(0))

The data collected within the new histogram target is as follows:

在新的直方图目标中收集的数据如下:

The rows are again our buckets (or slots), the column wait_type, is showing data grouped by the type of the wait we have recorded. The column wait_count is of course counting how much wait_info events were generated with the specific wait_type.

这些行又是我们的存储桶(或插槽), wait_type列显示的是按我们记录的等待类型分组的数据。 当然, wait_count用于计算使用特定的wait_type生成了多少wait_info事件。

The data that we have captured with the new histogram is quite valuable but it is again hard to read as we need to find the wait type corresponding to each of the gather number codes. The data is also stored only in memory and will not be saved upon SQL Server reboot.

我们用新的直方图捕获的数据非常有价值,但是又很难读取,因为我们需要找到与每个收集编号代码相对应的等待类型。 数据也仅存储在内存中,并且在SQL Server重新启动时将不会保存。

Let us review how we can query the histogram target and present the collected data in a more readable way.

让我们回顾一下如何查询直方图目标并以更具可读性的方式显示收集的数据。

Of course, we can find the histogram outcome from the SQL Server management studio and view it within the GUI, but if we need to store this or access it with TSQL it is not quite usable.

当然,我们可以从SQL Server管理工作室中找到直方图结果,并在GUI中查看它,但是如果我们需要存储该直方图或使用TSQL对其进行访问,则该实用性不太好。

Instead, we will extract the target data from within the SQL Server. The collected data for all extended event on our server can be found within the view sys.dm_xe_session_targets. We can locate our specific histogram target by limiting the results:

相反,我们将从SQL Server中提取目标数据。 我们服务器上所有扩展事件的收集数据都可以在sys.dm_xe_session_targets视图中找到。 我们可以通过限制结果来定位特定的直方图目标:


SELECT name, target_name, CAST(xet.target_data AS xml) as XML_data
FROM sys.dm_xe_session_targets AS xet
JOIN sys.dm_xe_sessions AS xe
ON (xe.address = xet.event_session_address)
WHERE xe.name = 'XE_collect_wait_stats_db' --We have specified the session name

The specific histogram we needed:

我们需要的特定直方图:

Opening the XML, we can see that schema is relatively simple, although there is not a standard for how the xml schema is build the following illustrates it:

打开XML,我们可以看到模式是相对简单的,尽管对于xml模式的构建没有标准,下面将对其进行说明:

<Slots truncated = “0” buckets=[count]>
 <Slot count=[count] truncated=[truncated bytes]>
  <value>
  </value>
 </Slot>
</Slots>

<截短的插槽=“ 0”个桶= [计数]>
<插槽计数= [计数]被截断= [截断的字节]>
<值>
</ value>
</ Slot>
</ Slots>

The results from our histogram:

直方图的结果:

The next step will be to limit to only the XML data in order to parse it to a standard table:

下一步将仅限于XML数据,以便将其解析为标准表:


SELECT CAST(xet.target_data AS xml) as XML_data
FROM sys.dm_xe_session_targets AS xet
JOIN sys.dm_xe_sessions AS xe
ON (xe.address = xet.event_session_address)
WHERE xe.name = 'XE_collect_wait_stats_db' --We have specified the session name
AND target_name= 'histogram' --We have specified the target type (in case there are >1)

Once done, we can now parse the XML to a standard table:

完成后,我们现在可以将XML解析为标准表:


SELECT
xed.XML_data.value('(value)[1]', 'varchar(256)') AS wait_type,
xed.XML_data.value('(@count)[1]', 'varchar(256)') AS wait_count
FROM (
SELECT CAST(xet.target_data AS xml) as XML_data
FROM sys.dm_xe_session_targets AS xet
JOIN sys.dm_xe_sessions AS xe
ON (xe.address = xet.event_session_address)
WHERE xe.name = 'XE_collect_wait_stats_db' --We have specified the session name
AND target_name= 'histogram' --We have specified the target type (in case there are >1)) as t
CROSS APPLY t.XML_data.nodes('//HistogramTarget/Slot') AS xed (XML_data)

The results are as follows, the same we can see from the SQL Server management studio:

结果如下,从SQL Server管理工作室中可以看到相同的结果:

To make the results more readable we will use the data stored in to sys.dm_xe_map_values that is relating the map_key to the specific wait:

为了使结果更具可读性,我们将使用存储在sys.dm_xe_map_values中的数据,该数据将map_key与特定的等待关联:

Combining dm_xe_map_values and our outcome:

结合dm_xe_map_values和我们的结果:


SELECT
xm.wait_count
,xm.wait_type
,mv.map_value
FROM (SELECT xed.XML_data.value('(value)[1]', 'varchar(256)') AS wait_type,xed.XML_data.value('(@count)[1]', 'varchar(256)') AS wait_countFROM (SELECT CAST(xet.target_data AS xml) as XML_dataFROM sys.dm_xe_session_targets AS xet  JOIN sys.dm_xe_sessions AS xeON (xe.address = xet.event_session_address)WHERE xe.name = 'XE_collect_wait_stats_db' --We have specified the session nameAND target_name= 'histogram' --We have specified the target type (in case there are >1)) as tCROSS APPLY t.XML_data.nodes('//HistogramTarget/Slot') AS xed (XML_data)) xm
JOIN sys.dm_xe_map_values as mv
ON xm.wait_type = mv.map_key
WHERE mv.name='wait_types'

And the final result is as follows, now the outcome is both readable and accessible via TSQL from a standard table!

最终结果如下,现在可以从标准表中通过TSQL读取和访问结果了!

The next article in this series

本系列的下一篇文章

  • Deep dive into SQL Server Extended events – The Event Pairing target 深入研究SQL Server扩展事件–事件配对目标

翻译自: https://www.sqlshack.com/deep-dive-into-the-extended-events-histogram-target/

python 目标直方图

python 目标直方图_深入了解扩展事件–直方图目标相关推荐

  1. sql server死锁_如何使用扩展事件和SQL Server代理自动执行SQL Server死锁收集过程

    sql server死锁 介绍 (Introduction) This article is the last one of a series in which we discussed how to ...

  2. sql活动监视器 死锁_使用system_health扩展事件监视SQL Server死锁

    sql活动监视器 死锁 Performance monitoring is a must to do the task for a DBA. You should ensure that the da ...

  3. python 方向梯度直方图_手动绘制方向梯度直方图(HOG)

    HOG(Histogram of Oriented Gradients)--方向梯度直方图,是一种表示图像特征量的方法,特征量是表示图像的状态等的向量集合. 在图像识别(图像是什么)和检测(物体在图像 ...

  4. 五大领域总目标指南_幼儿园五大领域活动总目标

    区域活动总目标: 幼儿通过自身的实践活动(指与物质环境的相互作用,包括体育活动.智力操作活动.生活活动.社 会活动等)积累有关活动情况的经验和感受(如成功.失败),从而产生对自己能力的认识和评价.发展 ...

  5. python多目标优化_多目标优化算法(四)NSGA3(NSGAIII)论文复现以及matlab和python的代码...

    前言:最近太忙,这个系列已经很久没有更新了,本次就更新一个Deb大神的NSGA2的"升级版"算法NSGA3.因为multi-objective optimization已经被做烂了 ...

  6. python 个性化推荐系统_如何在 Python 中使用 LightFM 构建可扩展的个性化推荐系统?...

    在我国,电商非常发达.今年双 11 的成交额仅仅过了 12 小时就达到了惊人的 1491.6 亿元!电商在我国的火爆程度由此可见一斑.不知你们有没有发现,在网店浏览商品时,它们好像能读懂你的内心,推荐 ...

  7. python目标识别代码_利用ImageAI库只需几行python代码超简实现目标检测

    什么是目标检测 目标检测关注图像中特定的物体目标,需要同时解决解决定位(localization) + 识别(Recognition).相比分类,检测给出的是对图片前景和背景的理解,我们需要从背景中分 ...

  8. python zope 工作流_使用C语言来扩展Python程序和Zope服务器的教程

    有几个原因使您可能想用 C 扩展 Zope.最可能的是您有一个已能帮您做些事的现成的 C 库,但是您对把它转换成 Python 却不感兴趣.此外,由于 Python 是解释性语言,所以任何被大量调用的 ...

  9. sql server调试_使用SQL Server扩展事件来调试应用程序

    sql server调试 介绍 (Introduction) Often enough, multilayer software has bugs. SQL Server Extended Event ...

最新文章

  1. 太强啦!一个普通摄像头就让二次元老婆“活”了过来!
  2. Linux系统工程师的必备素质
  3. 编写简单的服务和客户端(C++)---ROS学习第10篇
  4. python将文本转化成语音并播放
  5. 笔记-项目管理基础知识-复习要点
  6. VTK修炼之道26:图像基本操作_三维图像切片提取
  7. sql limit 的用法
  8. [转]多线程更新Processbar
  9. java jpa jar_JPA 开发所需的Jar包 (基于Hibernate)
  10. linux系统上手工建库步骤,Linux下Oracle手工建库过程
  11. mysql load 占位符,mysql:用不存在的数据的占位符初始化摘要表
  12. js中自执行函数(function(){})()和(function(){}())区别
  13. 类似select下拉选择框同时又支持手动输入的元素 datalist 介绍。
  14. 【LeetCode】162-寻找峰值
  15. zju眨眼数据集_浙大 CBIST团队发布高质量的多中心MRI公开数据集
  16. paip.提升性能---并行多核编程哈的数据结构list,set,map
  17. Winsock2_WSADATA
  18. 彼得·林奇的成功投资
  19. arm开发板开发环境搭建
  20. UVA - 10158 War

热门文章

  1. binwalk 提取bootimg_boot.img的解包与打包
  2. activity 生命周期_Activity 源码解析
  3. 景安服务器域名解析不起作用的正确解析方式之一
  4. 优秀网站设计:打造有吸引力的网站(原书第3版)
  5. 「一入 Java 深似海 」系列课程
  6. pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始
  7. JEECG中datagrid方法自定义查询条件
  8. 10G整数文件中寻找中位数或者第K大数
  9. 取消回车表单自动提交
  10. android权限检查