简体   繁体   English

如何使用C#在Windows窗体中显示由MSSQL中的实时查询统计启用的执行查询%?

[英]How can I display the Executing Query % which is enabled by Live Query Statistics in MSSQL in a windows form using C#?

In SSMS, when I enable the Live Query Statistics, I can see the execution percentage on the bottom left of the window. 在SSMS中,当我启用实时查询统计信息时,我可以在窗口左下角看到执行百分比。

在此输入图像描述

I want to display this incrementing percentage to the end user on a windows form. 我想在Windows窗体上向最终用户显示此递增百分比。 So far, I tried to implement this using progress bar on Visual Studio but turns out until I use a data table, it's not possible. 到目前为止,我尝试使用Visual Studio上的进度条实现这一点,但结果直到我使用数据表,这是不可能的。

在此输入图像描述

Forget the progress bar, even if I could display the incremental percentage in a text format on a label - exactly like in SSMS it will do the job. 忘记进度条,即使我可以在标签上以文本格式显示增量百分比 - 就像在SSMS中它将完成工作一样。

Any suggestions to implement the code in C# would be helpful. 任何在C#中实现代码的建议都会有所帮助。

On SQL Server 2016 SP1+ it can be done via dm_exec_query_profiles: 在SQL Server 2016 SP1 +上,可以通过dm_exec_query_profiles完成:

-- to enable LQS infrastructure, you have to do it once, also it can be set a startup trace:
DBCC TRACEON(7412, -1)

-- Session to track:
DECLARE @YourSessin INT = 760

-- Query that track a progress
SELECT  session_id ,
        node_id ,
        physical_operator_name ,
        SUM(row_count) row_count ,
        SUM(estimate_row_count) AS estimate_row_count ,
        IIF(COUNT(thread_id) = 0, 1, COUNT(thread_id)) [Threads] ,
        ISNULL(CAST(SUM(row_count) * 100. / NULLIF(SUM(estimate_row_count),0) AS DECIMAL(30, 2)),0) [PercentComplete] ,
        CONVERT(TIME, DATEADD(ms, MAX(elapsed_time_ms), 0)) [Operator time] ,
        DB_NAME(database_id) + '.' + OBJECT_SCHEMA_NAME(QP.object_id,
                                                        qp.database_id) + '.'
        + OBJECT_NAME(QP.object_id, qp.database_id) [Object Name]
FROM    sys.dm_exec_query_profiles QP
WHERE QP.session_id = @YourSessin
GROUP BY session_id ,
        node_id ,
        physical_operator_name ,
        qp.database_id ,
        QP.OBJECT_ID ,
        QP.index_id
ORDER BY session_id ,
        node_id

And more compact version: 更紧凑的版本:

DBCC TRACEON(7412, -1)

DECLARE @YourSessin INT = 760

SELECT MIN( CAST(row_count * 100. / NULLIF(estimate_row_count,0) AS DECIMAL(30, 2))) [PercentComplete]         
FROM    sys.dm_exec_query_profiles QP 
WHERE QP.session_id = @YourSessin

Please note, that enabling LQS infrastructure will add some overhead. 请注意,启用LQS基础架构会增加一些开销。 According to MS, if SQL Server 2016 SP1+ it is 1-2%. 据MS称,如果SQL Server 2016 SP1 +它是1-2%。 In older versions it raise up to 75% 在旧版本中,它可以提高75%

In SQL Server 2019 LQS is enabled by default, so no actions needed. 在SQL Server 2019中,默认情况下启用LQS,因此不需要任何操作。

More information on topic in a recent thread: https://dba.stackexchange.com/questions/228957/sql-server-2014-view-any-live-execution-plan-in-activity-monitor/228958#228958 有关最近主题中主题的更多信息: https//dba.stackexchange.com/questions/228957/sql-server-2014-view-any-live-execution-plan-in-activity-monitor/228958#228958

Another warning is about accuracy: calculations are based on estimate_row_count of the query plan, therefore estimations can be very rough, especially if statistics are not up to date 另一个警告是关于准确性:计算是基于查询计划的estimate_row_count,因此估计可能非常粗略,特别是如果统计数据不是最新的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在查询中引用表单元素的值? C# MSSQL - How do I refer to my form elements' values in a query? C# MSSQL 通过ADO.NET执行查询时如何捕获SQL查询性能统计信息 - How can I capture SQL query performance statistics when executing queries via ADO.NET 使用c#执行SQL查询时出现问题 - Trouble in executing the SQL query using c# 如何在使用C#执行SQL查询之前验证它? - How do I validate an SQL query before executing it using C# 如何使用C#使用Nest Elasticsearch编写查询? - How can i write a query by using Nest Elasticsearch by C# ? 我可以检测到Windows PC上的视频显示是否在c#中启用了HDCP - Can I detect whether the video display on a windows PC is HDCP enabled in c# 如何使Windows窗体能够执行并显示数据库查询? - How can I make my Windows Form to do and display my database query? 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#? 在C#中执行包含子选择的Oracle查询 - Executing oracle query which contains sub-select in c# 如何在 C# windows 表单应用程序的文本属性中显示当前日期和时间? - How can I display the current date and time in my C# windows form application's text property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM