在我的 ERP 数据库中的表上触发代码

发布于 2024-08-27 00:54:30 字数 581 浏览 4 评论 0原文

我的 ERP 供应商在表上有以下触发器:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[SOItem_DeleteCheck]
ON [dbo].[soitem]
FOR DELETE
AS 
BEGIN
    DECLARE @RecCnt int, @LogInfo varchar(256)

    SET @RecCnt = (SELECT COUNT(*) FROM deleted)

    IF @RecCnt > 150
    BEGIN
        RAISERROR (54010, 18, 1, 'SOItem') WITH LOG
        ROLLBACK TRANSACTION
    END

    SET @LogInfo = 'Deleting ' + LTRIM(STR(@RecCnt)) + ' Rows From SOItem'

    EXEC LogDeletes @LogInfo
END
GO

这对我来说似乎非常低效。 select count(*) 是否比 Count(特定字段) 花费的时间更长?

My ERP Vendor has the following trigger on a table:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[SOItem_DeleteCheck]
ON [dbo].[soitem]
FOR DELETE
AS 
BEGIN
    DECLARE @RecCnt int, @LogInfo varchar(256)

    SET @RecCnt = (SELECT COUNT(*) FROM deleted)

    IF @RecCnt > 150
    BEGIN
        RAISERROR (54010, 18, 1, 'SOItem') WITH LOG
        ROLLBACK TRANSACTION
    END

    SET @LogInfo = 'Deleting ' + LTRIM(STR(@RecCnt)) + ' Rows From SOItem'

    EXEC LogDeletes @LogInfo
END
GO

This seems very inefficient to me. Doesn't select count(*) take longer than Count(specific field)?

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

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

发布评论

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

评论(2

捎一片雪花 2024-09-03 00:54:30

老实说,即使速度较慢,我也可以在不到一毫秒的时间内在最大的表上运行这样的选择语句,该表有数百万行,此触发器不太可能命中。改变它并没有真正的性能提升。我很好奇为什么你想要回滚任何超过 150 条记录的事务。

Honestly even if is is slower, I can run a select stament like that in less than a millisecond on my largest table that has millions of rows which this trigger is unlikely to hit. There is no real performance gain from changing it. I'm curious as to why you would want to rollback any transaction with more than 150 records thoug.

深海不蓝 2024-09-03 00:54:30

我认为过去 count(1) 与 count(*) 相比有一个好处,我们都被教导使用这种方法,但目前更多的是风格而不是性能。

I think in the past there was a benefit to count(1) vs count(*), and we were all taught to use that approach, but at this point it's more about style than performance.

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