在我的 ERP 数据库中的表上触发代码
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
老实说,即使速度较慢,我也可以在不到一毫秒的时间内在最大的表上运行这样的选择语句,该表有数百万行,此触发器不太可能命中。改变它并没有真正的性能提升。我很好奇为什么你想要回滚任何超过 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.
我认为过去 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.