Emka
Emka

Reputation: 350

Update Synonym Table with values stored in tempdb

I have a perfomance issue regarding the update of Synonym table.

I have two SQL Servers:

and they communicate through linked Server (collation compatible = TRUE). I have a log table (let's say T) on Server A and a synonym of this table on Server B.

The following code takes 10 seconds to update a single value (!)

DECLARE @VAR nvarchar(max),@guid uniqueidentifier

SET @VAR = 'TEST VALUE'
SET @guid = 'some value'

UPDATE LogTable 
SET SizeKB=SizeKB+isnull(DATALENGTH(@VAR)/1024.0,0) 
WHERE LogTable_guid=@guid

Execution Plan: enter image description here

But this code takes less than 1 second

DECLARE @guid uniqueidentifier

SET @guid = 'some value'
    
UPDATE LogTable 
SET SizeKB=SizeKB+isnull(DATALENGTH('TEST VALUE')/1024.0,0) 
WHERE LogTable_guid=@guid

Execution Plan:

enter image description here

Event this code takes less than 1

DECLARE @VAR nvarchar(max),@guid uniqueidentifier,@temp float

SET @VAR = 'TEST VALUE'
SET @guid = 'some value'

SELECT @temp = SizeKB
FROM LogTable 
WHERE LogTable_guid=@guid

SET @temp = @temp+isnull(DATALENGTH(@VAR)/1024.0,0)

UPDATE LogTable 
SET SizeKB=@temp 
WHERE LogTable_guid=@guid

Execution Plan:

enter image description here

I am not looking for a work around for this, rather I would like to understand why there's difference in execution plan between the variable in DATALENGTH and when there's just the value of the variable..

This behavour is not limited to DATALENGTH.. I have tried LEN() and SUBSTRING() and they both have the same issue as mentioned.

Upvotes: 0

Views: 131

Answers (0)

Related Questions