Question

Please explain.

a) "subquery factoring" is used to replace a non-correlated subquery. What about correlated subquery? Is there any way to move a correlated sub-query to 'WITH' clause section?

b) are "subquery" "subquery factoring" executed exactly once?

c) "subquery" vs "subquery factoring" which one is better

Thank you.

Was it helpful?

Solution

You can use subquery factoring to replace a non-correlated subquery.
How on Earth do you propose doing so for a correlated subquery?

I don't understand part (b), can you rephrase?
Taking a guess at what you mean: a subquery in the WITH clause is typically executed only once before the main query is executed.

For large datasets, subquery factoring is obviously better since you're executing the subquery only once in most if not all cases. For smaller datasets the overhead of creating temporary tables may take longer than the actual query.

Apart from the performance concerns mentioned above, subquery factoring results in much cleaner and easer-to-maintain code.

OTHER TIPS

By the term "subquery factoring" do you really mean refactoring using a subquery? Refactoring is the process of altering a routine to improve maintenance and readability without altering its result. There are times when one cannot refactor a subquery into a common table expression (into "WITH" clause). Further, there is no golden rule about always using a CTE or always using a subquery (or derived table). It depends on the data and the DBMS as to what approach will perform best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top