开发者

Why does an UPDATE take much longer than a SELECT?

开发者 https://www.devze.com 2022-12-15 08:18 出处:网络
I have the following select statement that finishes almost instantly. declare @weekending varchar(6) set @weekending = 100103

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  

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.Accountnumber开发者_StackOverflow社区sorders.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

Even if I add:

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?


  • transaction log file writes
  • index updates
  • foreign key lookups
  • foreign key cascades
  • indexed views
  • computed columns
  • check constraints
  • locks
  • latches
  • lock escalation
  • snapshot isolation
  • DB mirroring
  • file growth
  • other processes reading/writing
  • page splits / unsuitable clustered index
  • forward pointer/row overflow events
  • poor indexes
  • statistics out of date
  • poor disk layout (eg one big RAID for everything)
  • Check constraints with UDFs that have table access
  • ...

Although, the usual suspect is a trigger...

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

Edited Sep 2011 and Feb 2012 with more options


The update has to lock and modify the data in the table, and also log the changes to the transaction log. The select does not have to do any of those things.


Because reading does not affect indices, triggers, and what have you?


In Slow servers or large database i usually use UPDATE DELAYED, that waits for a "break" to update the database itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消