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

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_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)) 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

本系列的下一篇文章

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

python 目标直方图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值