Re: Is the query engine always smart enough to know when to generate an internal CTE on a query?

Daniel Gross <[email protected]>
Newsgroups gmane.comp.hardware.ibm.midrange
Message-ID <[email protected]>
   Generally yes - the SQE does not create CTEs, but it does promote where
   clause conditions "up-stream" and filters early in the query execution. It
   even promotes conditions from join clauses to the where clause and
   vice-versa if it finds out that this gives an advantage. 
   But to analyze, where your query "burns" the most time, you should run it
   with Visual Explain or look up the query in the plan cache and find out,
   what is the really "expensive" stuff in your query. Normally you will find
   one or more table scans, that are done, because no matching indexes are
   found. 
   There are very rare cases, where you really get a performance boost from
   creating a CTE - CTEs are more a readability thing, not so much a
   performance thing. 
   If you really want to learn more about query performance I can only
   recommend: [1]SQL Performance Explained by Markus Winand - or his
   website [2]https://use-the-index-luke.com which holds more or less the
   same content. He concentrates on Db2 LUW but 99% of the information is
   also applying to Db2 for i. 
   HTH
   Daniel

     Am 06.08.2026 um 23:23 schrieb Dan Bale via MIDRANGE-L
     <[email protected]>:

     We've got a long-running query that has a tendency to get canceled
     because it hits our temporary storage threshold.
     The current table has a lot of conditions in the WHERE clause on the
     primary table that greatly narrow the rows from that table.  There are
     also 15 left joins on that primary table.  I've thought about modifying
     the query to use a CTE on the primary table and move the conditions in
     the WHERE clause to the CTE.
     I'm just wondering if the query engine optimizes this anyway without the
     explicit CTE.  Thoughts?
     - Dan Bale
     *** CONFIDENTIALITY NOTICE: The information contained in this
     communication may be confidential, and is intended only for the use of
     the recipients named above. If the reader of this message is not the
     intended recipient, you are hereby notified that any dissemination,
     distribution, or copying of this communication, or any of its contents,
     is strictly prohibited. If you have received this communication in
     error, please return it to the sender immediately and delete the
     original message and any copy of it from your computer system. If you
     have any questions concerning this message, please contact the sender.
     ***
     --
     This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
     list
     To post a message email: [email protected]
     To subscribe, unsubscribe, or change list options,
     visit: https://lists.midrange.com/mailman/listinfo/midrange-l
     or email: [email protected]
     Before posting, please take a moment to review the archives
     at https://archive.midrange.com/midrange-l.
     Please contact [email protected] for any subscription related
     questions.
     You can help support midrange.com by visiting
     https://donate.midrange.com and making a contribution.

References

   Visible links
   1. https://sql-performance-explained.com/
   2. https://use-the-index-luke.com/
-- 
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: [email protected]
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.

Please contact [email protected] for any subscription related questions.

You can help support midrange.com by visiting https://donate.midrange.com and making a contribution.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.