我有以下几乎立即完成的选择语句。
declare @weekending varchar(6)
set @weekending = 100103
select InvoicesCharges.orderaccnumber, Accountnumbersorders.accountnumber
from Accountnumbersorders, storeinformation, routeselecttable,InvoicesCharges, invoice
where InvoicesCharges.pubid = Accountnumbersorders.publication
and Accountnumbersorders.actype = 0
and Accountnumbersorders.valuezone = 'none'
and storeinformation.storeroutename = routeselecttable.istoreroutenumber
and storeinformation.storenumber = invoice.store_number
and InvoicesCharges.invoice_number = invoice.invoice_number
and convert(varchar(6),Invoice.bill_to,12) = @weekending
但是,等效的更新语句需要 1 分 40 秒
declare @weekending varchar(6)
set @weekending = 100103
update InvoicesCharges
set InvoicesCharges.orderaccnumber = Accountnumbersorders.accountnumber
from Accountnumbersorders, storeinformation, routeselecttable,InvoicesCharges, invoice
where InvoicesCharges.pubid = Accountnumbersorders.publication
and Accountnumbersorders.actype = 0
and dbo.Accountnumbersorders.valuezone = 'none'
and storeinformation.storeroutename = routeselecttable.istoreroutenumber
and storeinformation.storenumber = invoice.store_number
and InvoicesCharges.invoice_number = invoice.invoice_number
and convert(varchar(6),Invoice.bill_to,12) = @weekending
即使我添加:
and InvoicesCharges.orderaccnumber <> Accountnumbersorders.accountnumber
在更新语句末尾将写入次数减少到零时,需要相同的时间。
我在这里做错了什么吗?为什么会有这么大的差异?
最佳答案
- 事务日志文件写入
- 索引更新
- 外键查找
- 外键级联
- 索引 View
- 计算列
- 检查约束
- 锁
- 闩锁
- 锁定升级
- 快照隔离
- 数据库镜像
- 文件增长
- 其他进程读/写
- 页面拆分/不合适的聚集索引
- 前向指针/行溢出事件
- 索引较差
- 统计数据已过时
- 磁盘布局不佳(例如,一个大型 RAID 满足所有需求)
- 检查具有表访问权限的 UDF 的约束
- ...
尽管如此,通常的嫌疑人是一个触发因素...
此外,您的条件 extra 没有任何意义:SQL Server 如何知道忽略它?大部分行李仍然会生成更新......甚至触发器仍然会触发。例如,在行搜索其他条件时必须保持锁定
2011 年 9 月和 2012 年 2 月编辑,包含更多选项
关于sql-server - 为什么 UPDATE 比 SELECT 花费的时间长得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2009659/