3 ms·
Be careful with CTEs with regard to performance, some SQL DBs can optimise by pushing predicates through to the inner queries, where for some (postgres being th
by dspillett 9y ago
Be careful with CTEs with regard to performance, some SQL DBs can optimise by pushing predicates through to the inner queries, where for some (postgres being the one example that I know but there are probably others) a CTE presents an optimisation fence.
- MarkusWinand 9y agoPostgreSQL being the one noteworthy: http://modern-sql.com/feature/with/performance http://modern-sql.com/feature/with/performance
- nerdponx 9y agoI never understood this. Why can't CTEs be transparently compiled into subqueries?
- dspillett 9y agoThere is plenty of discussion about it out there (search for "postgres cte optimisation fence") including official word. Note though that simply replacing CTE references with appropriate sub-queries simply won't work in the recursive case, and in some cases where a CTE is referred to multiple times the result may be an even less optimal query plan.
- deleted 9y ago[deleted]