简体   繁体   中英

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; 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.

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!

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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