SQL Server Profiler 2005:如何使用触发器测量插入语句的执行时间?

发布于 2024-10-06 17:34:36 字数 86 浏览 3 评论 0原文

我想测量具有替代插入触发器的插入语句的执行时间(使用我猜测的 SQL Server Profiler 的持续时间)。如何衡量该语句的完整时间(包括触发时间)?

I want to measure the execution time (using I guess duration from SQL Server Profiler) of an insert statement that has an instead-of insert trigger on it. How do I measure the complete time of this statement including the trigger time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-10-13 17:34:36

您在 SQL Server Profiler 中看到的查询执行时间(持续时间)是执行该查询所花费的时间,包括评估任何触发器或其他约束

由于触发器旨在用作检查数据完整性的替代方法,因此在任何触发器也完成之前,SQL 语句不会被视为已完成。

更新:一些常用 SQL Server 分析器事件的概述:

  • SQL:BatchCompleted 当 SQL Server 批处理(一组语句)完成执行时发生 -持续时间是执行批处理的总时间。

  • SQL:StmtCompleted 当作为批处理的一部分执行的 SQL 语句完成执行时发生 - 同样,持续时间是执行该单个语句的时间。

  • SP:Completed 当存储过程完成执行时发生 - 显示的持续时间是完成存储过程执行的时间。

  • SP:StmtCompleted 当作为存储过程的一部分执行的 SQL 语句完成时发生。

批处理是一组由 GO 语句分隔的 SQL 语句,但是要理解上述内容,您还应该知道所有 SQL Server 命令都是在批处理*的上下文中执行的。

此外,上述每个事件还有一个相应的 Starting 事件 - SP:StartingSQL:BatchStartingSQL:StmtStarting< /code> 和 SP:StmtCompleted。这些没有列出持续时间(因为我们还不知道持续时间,因为它尚未完成,但确实有助于显示持续时间记录的开始时间)。

为了更好地理解这些事件之间的关系,我建议您尝试捕获一些简单示例(来自 SQL Server Management Studio 中)的一些跟踪,例如:

SELECT * FROM SomeTable
GO

SELECT * FROM SomeTable
SELECT * FROM OtherTable
GO

SELECT * FROM SomeTable
exec SomeProc
GO

正如您应该看到的,对于上面的 3 个示例中的每一个,您总是会得到一个然而,其他事件类型 SQL:BatchStartingSQL:BatchCompleted 提供了有关运行的各个命令的更多详细信息。

出于这个原因,我通常最倾向于使用 SQL:BatchCompleted 事件,但是如果您尝试测量的语句是作为较大批处理(或存储过程)的一部分执行的,那么您可以发现其他事件类之一有帮助。

有关各种 SQL Server 的详细信息,请参阅 TSQL 事件类别 (MSDN)分析事件 - 有很多!

最后,如果您从 SQL Server Management studio 中执行此命令,请注意记录执行时间的最简单方法是使用客户端统计功能:

替代文字

(*) 我很确定所有内容都是作为批处理的一部分执行的,尽管我没有管理过在互联网上找到任何证据来证实这一点。

The execution time (duration) that you see in SQL server profiler for a query is the time it took to execute that query including evaluating any triggers or other constraints.

Because triggers are intended to be used as an alternative way to check data integrity, an SQL statement is not considered to have completed until any triggers have also finished.

Update: An overview of some commonly used SQL Server profiler events:

  • SQL:BatchCompleted Occurs when a SQL server batch (a group of statements) has completed execution - the duration is the total time to execute the batch.

  • SQL:StmtCompleted Occurs when a SQL statement executed as part of a batch completes execution - again the duration is the time to execute that single statement.

  • SP:Completed Occurs when a stored procedure has completed execution - the duration shown is the time to complete execution of the stored procedure.

  • SP:StmtCompleted Occurs when an SQL statement executed as part of a stored procedure completes.

A batch is a set of SQL statements separated by a GO statement, however to understand the above you should also know that all SQL server commands are executed in the context of a batch*.

Also, each of the above events also has a corresponding Starting event - SP:Starting, SQL:BatchStarting, SQL:StmtStarting and SP:StmtCompleted. These don't list durations (as we don't know the duration yet because its not completed, however do help show when the duration recording starts from).

To better understand the relationship between these events, I recommend that you experiment with capturing some traces of some simple examples (from within SQL Server Management Studio), for example:

SELECT * FROM SomeTable
GO

SELECT * FROM SomeTable
SELECT * FROM OtherTable
GO

SELECT * FROM SomeTable
exec SomeProc
GO

As you should see, for each of the 3 examples above you always get a SQL:BatchStarting and SQL:BatchCompleted, the other event types however provide more detail on the individual commands run.

For this reason I generally tend to use the SQL:BatchCompleted event the most, however if the statement you are attempting to measure is executed as part of a larger batch (or in a stored procedure) then you may find one of the other event classes helpful.

See TSQL Event Category (MSDN) for more information on the various SQL Server Profiling events - there are lots!

Finally, if you are executing this command from within SQL Server Management studio, be aware that the simplest way to record the execution time is to use the client side statistics feature:

alt text

(*) I'm pretty sure that everything is executed as part of a batch, although I've not managed to find any evidence on the internet to confirm this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文