I have the following table:
CREATE TABLE [dbo].[StateChanges] ( [RecordId] uniqueidentifier NOT NULL, [LinkedObjectId] uniqueidentifier NOT NULL, [ChangeType] TINYINT not null, [ChangeTime] DATETIME not null, -- some other columns too )
with the following indices:
-- yes, clustered over GUID. I know it's not very good. CREATE CLUSTERED INDEX StateChangesIndex ON [StateChanges](RecordId) GO CREATE INDEX LastChangeIndex_Object_Type_Time] ON [StateChanges] (LinkedObjectId, ChangeType, ChangeTime) INCLUDE(SomeColumnsHere) GO
And I have a function that goes like this initially:
CREATE FUNCTION [dbo].[ObjectHasInterestingChanges_v1] ( @objectId uniqueidentifier ) RETURNS BIT AS BEGIN IF (SELECT TOP(1) 1 FROM StateChanges WHERE LinkedObjectId=@objectId AND (ChangeType = 1 OR ChangeType = 2 ) ) IS NOT NULL BEGIN RETURN 1; END RETURN 0; END
This function is being called for a large number of rows processed by an outer query - one thousand time for the very same three thousand rows.
If I try to
SELECT ObjectHasInterestingChanges_v1(something) all I have is a "constant scan" in the plan which isn't interesting so I extract the contents into a separate query and look into the execution plan. The plan is as follows initially:
- "index scan" over LastChangeIndex_Object_Type_Time - as expected
- "top" of "index scan result"
- "left outer join" result of "top" with some "constant scan" result
- "compute scalar" of "left outer join result"
- "cond with query" of "compute scalar"
- that's it
I look into
sys.dm_exec_query_stats table and note the number of execution for this function, number of reads and time taken.
I then change the function like this:
CREATE FUNCTION [dbo].[ufn_ObjectHasInterestingChanges_v2] ( @objectId uniqueidentifier ) RETURNS BIT AS BEGIN DECLARE @result BIT SET @result = 0 SELECT TOP(1) @result=1 FROM StateChanges WHERE LinkedObjectId=@objectId AND (ChangeType = 1 OR ChangeType = 2 ) RETURN @result; END
It yields exactly the same results. If I look into the plan it's the following now:
- "index seek"
- "compute scalar"
- that's it
I run the very same outer query in the same database such that exactly the same data is being processed and exactly the same data is being read and the background workload is exactly the same (zero) and so I'm sure that the comparison is fair and reasonable.
I look into
sys.dm_exec_query_stats again. I see that
- the number of executions is the same for v1 and v2 - large number close to three millions
- the number of logical reads, physical reads and physical writes is the same for v1 and v2
- "worker time" is 53.5 seconds for v1 and 48.3 seconds for v2 - 10 percent speed up
- "total time" is 86 seconds for v1 and 67 seconds for v2 - 22 percent speed up
Why is this? Even if "worker time" is decreased because if-statements are somehow time consuming why would "total time" decrease even further? "total time" also accounts for I/O and "total time" decreasing more than "worker time" would imply some I/O speedup which is completely unreasonable here.
Where does this notable difference in both "worker time" and "total time" come from after such minor code change?