有一个表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导致更新失败
请问有没有更好的解决办法?
------解决方案--------------------
- SQL code
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()) 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,insertedinsert时有inserted