问题
I've created a complex process that uses several CTEs (mainly for recursive hierarchical work).
On small sample sets of data everything goes as expected but when I apply the code to large sets of data I received unexpected (and wrong) results.
I think I've narrowed it done to the CTEs. The recursive CTEs are "fed" data processed in several earlier CTEs, and that seems to be the problem.
I set up a sample data set as follows:
- Four rows with unique data
- Each row receives a random row number (this is added in a CTE
I then take the results of the first CTE and perform a self-join in a second CTE.
I expected all rows to join, each to itself. What actually happens is that unequal rows join up.
Can someone offer up an explanation for this behaviour?
回答1:
There's nothing unexpected about that result, except maybe if you don't understand it.
Each CTE is resolved each and every time
it is referenced. Yes, this is why it is possible for a simple CTE on a highly transactional table to return 4 rows in one reference and 5 rows in the next 2 levels down.
On your sample however, it's because of the ORDER BY NEWID(), giving each resolution of the original CTE a different row_number()-ing. Did you think CTEs are stored in memory and cached? On the SQLFiddle site, under your results, there is a "view execution plan" link. It shows 2 distinct, separate scans of the table.
来源:https://stackoverflow.com/questions/12891637/unexpected-results-from-cte