有一个表a{available BIGINT,allure INT}
每次更新available字段 或者插入新行时,要重新计算allure
allure是个依赖于available的值,并且带有随机性
Allure=FLOOR((200*Available)/(50000+Available)+20-40*Rand())问题是我如何获得具体哪些行被更新了,我在触发器中写
ALTER TRIGGER [dbo].[ResetAllure]
ON [dbo].[a]
AFTER INSERT,UPDATE
AS
BEGIN IF UPDATE(Available)
Begin
Update SM_AccountsList Set Allure=FLOOR((200*Available)/(50000+Available)+20-40*Rand())
Update SM_AccountsList Set Allure=1 Where Allure<1
End
END
执行起来没有问题,但是每次更新一条记录时都计算所有行,速度很慢
而且多人同时操作且很频繁时,总出现deadlock导致更新失败请问有没有更好的解决办法?
每次更新available字段 或者插入新行时,要重新计算allure
allure是个依赖于available的值,并且带有随机性
Allure=FLOOR((200*Available)/(50000+Available)+20-40*Rand())问题是我如何获得具体哪些行被更新了,我在触发器中写
ALTER TRIGGER [dbo].[ResetAllure]
ON [dbo].[a]
AFTER INSERT,UPDATE
AS
BEGIN IF UPDATE(Available)
Begin
Update SM_AccountsList Set Allure=FLOOR((200*Available)/(50000+Available)+20-40*Rand())
Update SM_AccountsList Set Allure=1 Where Allure<1
End
END
执行起来没有问题,但是每次更新一条记录时都计算所有行,速度很慢
而且多人同时操作且很频繁时,总出现deadlock导致更新失败请问有没有更好的解决办法?
ON [dbo].[a]
AFTER INSERT,UPDATE
AS
BEGIN IF UPDATE(Available)
Begin
Update SM_AccountsList
Set Allure=FLOOR((200*Available)/(50000+Available)+20-40*Rand())
where
exists(select 1 from inserted where allure=SM_AccountsList)
or
exists(select 1 from deleted where allure=SM_AccountsList) Update SM_AccountsList Set Allure=1
from
Where Allure <1 and
(exists(select 1 from inserted where allure=SM_AccountsList)
or
exists(select 1 from deleted where allure=SM_AccountsList))
End 根据实际情况判断:
update时有deleted,inserted
insert时有inserted
or
exists(select 1 from deleted where allure=SM_AccountsList.allure)--漏了加列名
exists(select 1 from inserted where allure=SM_AccountsList.allure)--漏了加列名
or
exists(select 1 from deleted where allure=SM_AccountsList.allure)--漏了加列名中的where allure=SM_AccountsList.allure是啥意思没看明白
另外想问下,触发器中的代码是不是和存储过程一样经过了预编译,能提高执行速度?就本例子中,我如果去掉Allure字段,而在每次查询时都写Select *,FLOOR((200*Available)/(50000+Available)+20-40*Rand()) as Allure From SM_AccountsList其实也能达到目的,想了解一下,用触发器和在查询时计算到底哪个快呢