You should pretty much ignore the subtree costs in this case.
Even in the actual plan they are just based on estimates.
From what you say about STATISTICS IO
output for example the actual number of executions of the operators accessing Table2
is 0
.
However the plan will likely estimate Number of Executions = 1.
(You can see estimated and actual figures in the properties window in SSMS after selecting an operator)
If some branches of the plan have an under estimate of the number of executions you could try using a #temp
table instead so the column statistics are taken into account.
You could get somewhat more representative subtree costs by adding some helper variables and OPTION (RECOMPILE)
but still they are only as accurate as the modelling assumptions and the estimates.
For example
DECLARE @T TABLE(
X INT,
StorageTable VARCHAR(50));
INSERT INTO @T
VALUES (1, 'Table1')
DECLARE @Branch1Exists BIT = iif(EXISTS(SELECT * FROM @T WHERE StorageTable = 'Table1'), 1, 0)
DECLARE @Branch2Exists BIT = iif(EXISTS(SELECT * FROM @T WHERE StorageTable = 'Table2'), 1, 0)
SELECT X
FROM @T
JOIN master..spt_values V
ON [@T].X = number
WHERE @Branch1Exists = 1
UNION ALL
SELECT X
FROM @T
JOIN sys.objects
ON [@T].X = object_id
WHERE @Branch2Exists = 1
OPTION (recompile)
Removes at compile time the branch of the plan that isn't executed rather than showing costs for an estimated single execution.