开发者

subquery factoring questions

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 fact开发者_高级运维oring" which one is better

Thank you.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜