by poolpool · 12y ago · 0 comments
Can you share an example of two queries that are equivalent where one is written using CTEs and it doesn't construct the same plan?
But: The optimizer will first fully resolve the CTE expression and only then join. If the join condition eliminates many of the rows selected in the CTE, this might indeed yield a much worse execution time than doing it inline.
This behaviour is documented on http://www.postgresql.org/docs/9.3/static/queries-with.html (at the end of 7.8.1)
SQL Server's plan generation is not always perfect, but it's good 98 times out of 100.