I've done my best to follow Why is my query suddenly slower than it was yesterday?
The ShowPlanXML parses fine with SQL Sentry Plan Explorer but not via brentozar.com/pastetheplan (not sure why) so I have pasted text files to my github instead below:
Here is the DDL and TSQL I am executing.
Here is the bad query plan (times out). PasteThePlan
Here is the good query plan. PasteThePlan
Any idea how I can more reliably get the engine to use a "good" query plan? Thank you!
Buildattribute – Charlieface Jul 25 '22 at 23:54(PartId, RootId)allowing them to do a merge join, and perhaps should just beEXISTSorINrather than regular joins. You can reorganize the query and move the join on#tmp_rollupout of the CTE, thereby only referencing it once, this should remove the table spool. – Charlieface Jul 26 '22 at 00:04#tmp_rollupand therefore there are many optimizations available anyway, querying the table twice is not going to help it. And since the temp table is only referenced once (the reference being modified) it won't need a spool – Charlieface Jul 26 '22 at 00:46