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

Why does an UPDATE take much longer than a SELECT?(为什么 UPDATE 比 SELECT 花费的时间长得多?)
本文介绍了为什么 UPDATE 比 SELECT 花费的时间长得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following select statement that finishes almost instantly.

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  

然而,等效的更新语句需要 1m40s

However, the equivalent update statement takes 1m40s

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

在将写入次数减少为零的更新语句的末尾,它需要相同的时间.

at the end of the update statement reducing the number of writes to zero, it takes the same amount of time.

我在这里做错了吗?为什么会有如此巨大的差异?

Am I doing something wrong here? Why is there such a huge difference?

推荐答案

  • 事务日志文件写入
  • 索引更新
  • 外键查找
  • 外键级联
  • 索引视图
  • 计算列
  • 检查约束
  • 闩锁
  • 锁定升级
  • 快照隔离
  • 数据库镜像
  • 文件增长
  • 其他进程读/写
  • 页面拆分/不合适的聚集索引
  • 前向指针/行溢出事件
  • 糟糕的索引
  • 统计数据已过时
  • 糟糕的磁盘布局(例如,一个大型 RAID 用于所有内容)
  • 检查具有表访问权限的 UDF 的约束
  • ...
  • 虽然,通常的嫌疑人是触发器...

    Although, the usual suspect is a trigger...

    另外,你的条件 extra 没有意义:SQL Server 怎么知道忽略它?大多数行李仍然会生成更新......即使触发器仍然会触发.例如,在为其他条件搜索行时必须保持锁定

    Also, your condition extra has no meaning: How does SQL Server know to ignore it? An update is still generated with most of the baggage... even the trigger will still fire. Locks must be held while rows are searched for the other conditions for example

    在 2011 年 9 月和 2012 年 2 月修改了更多选项

    Edited Sep 2011 and Feb 2012 with more options

    这篇关于为什么 UPDATE 比 SELECT 花费的时间长得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)