sql-server - 为什么 UPDATE 比 SELECT 花费的时间长得多?

标签 sql-server performance sql-server-2005 tsql triggers

我有以下几乎立即完成的选择语句。

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/

相关文章:

sql-server - 如何从运行时传递 n 值的表中获取前 n 行?

c# - 将数百万条记录从平面文件插入 SQL Server 的陷阱是什么?

sql-server - 根据 SQL Server 中另一列中的值将金额转换为负数

sql-server - 适用于 REST/Swagger API 的 SQL Azure 连接器 - 市场?套餐设置?

php - 如何在mysql中使用普通列对聚合函数进行搜索

mysql - nginx-tornado-django 请求超时

c++ - 中值滤波器 OpenMP 优化

c# - 多次 Response.writeAsync 调用

sql-server - 如何在 SQL Server 中转义宪章?

mysql - 将记录从 MySQL 数据库导入 MS SQL 的最佳解决方案(每小时)