简体   繁体   English

简单更新查询的不同性能

[英]Different performance for simple update query

I have a database restored on two different machines (developer machine, and tester machine), and whilst not identical they have similar performance. 我在两个不同的机器(开发者机器和测试器机器)上还原了一个数据库,尽管它们并不相同,但它们具有相似的性能。

Using the following query (obfuscated): 使用以下查询(混淆):

 CREATE TABLE #TmpTable (MapID int, Primary key(MapID))

 UPDATE MapTable
    SET Tstamp = GetDate() 
   FROM MapTable    
        JOIN Territory as Territory ON Territory.ID = MapTable.TerritoryID
        LEFT JOIN #TmpTable ON #TmpTable.MapID = MapTable.MapID  
  WHERE MapTable.TStamp > DateAdd(year, -100, GETDATE())    
    AND Territory.Name IS NOT null    
    AND Territory.Name NOT LIKE '!%'   
    AND #TmpTable.MapID IS NULL

For 400k records, the developer machine updates in about 4 seconds, but on the tester machine it updates in about 25 seconds; 对于400k条记录,开发人员计算机在约4秒内更新,但在测试仪计算机上,它在约25秒内更新; the same DB was restored on both machines. 两台计算机上都还原了相同的数据库。

The problem is that when running this through a tool we use, the timeout for queries is set to 30 seconds and it timeouts 90+ % of the time on tester machine. 问题是,通过我们使用的工具运行此命令时,查询超时设置为30秒,并且在测试机上的超时时间超过90+ %

But the execution plan on both is the same... 但是两者的执行计划是相同的...

Can anyone suggest why this is, and possible optimisation(s)? 谁能说出为什么会这样,并可能进行优化?

One thing that I can see that 'might' have an impact on performance is the use of the GETDATE() function that might be called twice for each record, but definitely once, so that's 400K calls to the function! 我可以看到“可能”对性能产生影响的一件事是使用了GETDATE()函数,该函数可能为每条记录调用两次,但肯定会被调用一次,因此该函数需要400K调用!

I would put the result of GETDATE() into a variable and use that, I always do that unless there is a very good reason no to, for example the changes in the date throughout the query is required like in batch processing within a CURSOR . 我会将GETDATE()的结果放入变量中并使用它,除非有充分的理由不这样做,否则我将始终这样做,例如,像在CURSOR中进行批处理一样,需要更改整个查询中的日期。

However, I doubt this will be the main issue with performance. 但是,我怀疑这将是性能的主要问题。 With such a large difference in execution time between the different machines where the execution plan is the same I would be looking at factors external to SQL such as CPU usage, HDD usage, speed and fragmentation etc. 由于执行计划相同的不同机器之间的执行时间差异如此之大,我将研究SQL外部的因素,例如CPU使用率,HDD使用率,速度和碎片等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM