It looks as though some of the difference is because the slow plan was run first. For whatever reason the execution of the TVF itself took 5.5 seconds compared with 1.35 in the fast case.
The slow plan was clearly operating against a cold cache as it shows additional physical reads and managed to accumulate an additional 15 seconds of PAGEIOLATCH_SH
waits compared to the fast case.
This looks as though it affected both the execution of the TVF itself (which took 5.5
seconds compared with 1.35
in the fast case.) and the wider plan using the result of it.
You say in the comments that on second execution it took 1111
seconds. This is still 3 times slower than the fast plan (3.4463.446
seconds) so doesn't explain all the performance difference.
The main problem you are experiencing is due to the poor default estimation for multi statement TVFs (fixed guess of 100100
rows in compatibility levels 2014/2016 and 11
in earlier versions). In reality your TVF returns 1,715
rows.
In the inner join case because inner joins are associative and commutative it can be re-ordered flexibly and gets moved quite deep intoto the deepest part of the tree. This would make sense if one row was actually returned as it could whittle down the row count early for the other joins in the plan.
The execution plan is below. The pink annotations are "Actual Elapsed Time (ms)" from the XML.
Because of the 1 row estimate it starts off badly by joining onto dbo.ParticipantTrades
and selecting a plan with nested loops and lookups. That nested loops has an elapsed time of 13.97613.976
seconds (presumably most of which was spent waiting on the spilling sort immediately upstream to request rows from it, with 33.26
seconds taken up on the lookups themselves and associated IO waits for the physical reads at that operator).
923,646923,646
rows are emitted from the join vs an estimated 1423.181423.18
and this mis estimate proagates-estimate propagates upwards in the plan through 3 sorts and a hash join spilling as it goes (due to the row underestimate)
When you add the LEFT JOIN
it can not be as freely re-ordered and the join happens much higher up in the plan (where it would do less damage in the problematic INNER JOIN
case). The semantics of outer join help here anyway.
Whilst the estimate for the number of rows coming out of the TVF is still 1
this does not adversely affect the estimates for the join it is directly involved in (SQL Server assumes that the cardinality coming out of that join will be the same as that of the other sub tree to the join and this is infactin fact what happens - an outer join cannot reduce this number as a non joined row would still pass through - just with NULL
for the fvh
columns).
There are still cardinality estimation errors in the outer join plan but not of the same magnitude and it requests a sufficient memory grant to avoid spilling anywhere.
This cardinality estimation issue has been resolved in the most recent version with interleaved execution. In the meantime (as you say in the comments it needs to be 2008 compatible) you can manually interleave it by storing the TVF result into a #temp
table and joining onto that to allow the count of the intermediate result (and column statistics) to be taken into account.