在执行时打印触发器的名称是个好主意吗?

发布于 2024-08-21 11:28:17 字数 86 浏览 4 评论 0原文

我们在顶部放置了一条打印语句,因此当我们在 SSMS 中运行查询时,消息选项卡向我显示可能发生的情况比最初看到的更多。

性能会受到很大影响吗?

We put a print statement at the top, so when we're running a query in SSMS the messages tab shows me that more may be happening than initially meets the eye.

Would there be much of a performance hit?

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

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

发布评论

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

评论(2

满天都是小星星 2024-08-28 11:28:17

性能损失可能可以忽略不计。

为了得到结论性的答案,您需要对此进行测试 - 基准测试使用打印语句需要多长时间以及不使用打印语句需要多长时间(无论是在使用新统计数据进行首次编译时还是在后续尝试中)。

The performance penalty is probably negligible.

For a conclusive answer, you need to test this - benchmark how long it takes with the print statements and how long without (both on first compile with fresh statistic and on subsequent attempts).

做个少女永远怀春 2024-08-28 11:28:17

我同意@Oded - 打印语句的性能影响可能可以忽略不计。可以说,您可以检查 sys.dm_exec_sessions 中的调用 @@SPID 并查看它们正在使用什么应用程序,并且仅当它是 Management Studio 时才进行有条件打印。但这将比一开始就打印更昂贵。只是为了展示它的样子:

IF EXISTS
(
 SELECT 1 
  FROM sys.dm_exec_sessions 
  WHERE session_id = @@SPID
  AND [program_name] LIKE '%Management Studio%'
)
 PRINT 'Trigger : <trigger name>';

为了避免复制/粘贴错误,因为您从现有触发器复制了代码并且没有注意到,可能会指示错误的触发器,我会考虑更改新的触发器模板以包含此代码SET NOCOUNT ON 之后:

PRINT 'Trigger : ' + OBJECT_NAME(@@PROCID);

这显然比仅仅对名称进行硬编码要贵一些,但如果您容易出现这种类型的复制/粘贴错误,可以防止以后出现一些故障排除问题(我知道我有时会这样做) 。

在任何情况下,即使您不想这样做,您也可以添加以下行:

PRINT 'Trigger : <Trigger_Name, sysname, Trigger_Name>';

您可以通过转到“查看”>“更改新触发器的模板”。模板资源管理器,展开“触发器”,右键单击“创建 T-SQL 触发器(新菜单)”,然后选择“编辑”。添加上面的行并转到“文件”>“节省。现在,当您想要创建新的触发器时,您可以通过我刚才提到的相同过程打开此文件,或者您可以在对象资源管理器中展开一个表,右键单击“触发器”并选择“新建触发器...”打开代码,您可以按 CTRL + SHIFT + M,它会给您一个简单的小 UI,允许您替换所有令牌参数,从而可以轻松输入表名称、操作类型等。当然,当您已完成并创建了触发器,您应该将该脚本保存在源代码管理中,但这是一个不同的讨论。

I agree with @Oded - performance impact of a print statement is probably negligible. Arguably you could check sys.dm_exec_sessions for the calling @@SPID and see what application they are using, and conditionally print only if it is Management Studio. But that is going to be more expensive than just printing always in the first place. Just to show what that would look like:

IF EXISTS
(
 SELECT 1 
  FROM sys.dm_exec_sessions 
  WHERE session_id = @@SPID
  AND [program_name] LIKE '%Management Studio%'
)
 PRINT 'Trigger : <trigger name>';

To avoid copy/paste errors where the wrong trigger might be indicated because you copied the code from an existing trigger and didn't notice, I'd consider changing your new trigger template to have this code after SET NOCOUNT ON:

PRINT 'Trigger : ' + OBJECT_NAME(@@PROCID);

This is a little more expensive than just hard-coding the name obviously, but could prevent some troubleshooting headaches later if you are prone to this type of copy/paste error (I know I do it from time to time).

In any case, even if you don't want to do that, you could instead add the following line:

PRINT 'Trigger : <Trigger_Name, sysname, Trigger_Name>';

You can change the template for new triggers by going to View > Template Explorer, expanding "Trigger", right-clicking "Create T-SQL Trigger (New Menu)", and selecting Edit. Add the line above and go to File > Save. Now when you want to create a new trigger, you can open this file through the same process I just mentioned, or you can expand a table in Object Explorer, right-click "Triggers" and select "New Trigger..." When you open the code you can hit CTRL + SHIFT + M and it will give you a simple little UI to allow you to replace all of the token parameters, making it easy to enter the table name, type of action, etc. Of course when you are finished and have created the trigger you should save that script in source control, but that's a different discussion.

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