Db2

Db2

Connect with Db2, Informix, Netezza, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
  • 1.  Does DB2 support optimizer hints?

    Posted Thu January 30, 2025 11:56 AM

    I'm having a lot of problems with a rather complex query, but essentially it boils down to this simplified version:

    select b.*
    from (select id from [complex subquery]) a
    inner join b
    on b.id = a.id

    I've rephrased this query in several different ways, but DB2 insists on doing a table scan on table b instead of using the primary key on id, even though the subquery just returns a handful of ids.

    Is there any way I can provide optimizer hints for the query like you can do with other database vendors?

    Or else, is there another way to efficiently execute the subquery first and then lookup the records in b?



    ------------------------------
    Menno Avegaart
    ------------------------------


  • 2.  RE: Does DB2 support optimizer hints?

    Posted Thu January 30, 2025 01:35 PM

    Menno,

    Yes, the "hints" are supported either in a form of optimization profiles (w/o need to edit an application statement) or inlined into the statement.

    Refer to the Optimization profiles and guidelines link.



    ------------------------------
    Mark Barinstein
    ------------------------------



  • 3.  RE: Does DB2 support optimizer hints?

    Posted Fri January 31, 2025 03:42 AM

    Hi Menno,

     

    It could be that because there aren't many rows, Db2 decides that a tablespace scan is the most efficient.

     

    This could be a job for "Captain Table Expression" though...

     

    with a as (

    select id from [complex subquery]

    )

    select b.*
    from a
    inner join b
    on b.id = a.id

     

    Regards,

    Marcus Davage CEng CITP MBCS

    Lead Product Developer
    Intelligent Z Optimization and Transformation
    BMC Software Ltd
    Direct +44 118 921 8517
    Mobile +44 7840 023 560
    Email marcus_davage@bmc.com

    MarcusDavage@bmcsoftware.onmicrosoft.com/bookings/">Book time to meet with me

    IBM Champion 2024 badge image. Issued by IBM

     






  • 4.  RE: Does DB2 support optimizer hints?

    Posted Fri January 31, 2025 04:25 AM

    I did a blog on this some years back but the examples might help. 

    https://www.triton.co.uk/line-optimization-v11-1/

    In-line optimization is far easier than messing with profiles and would allow you to test out your enforced index access more quickly



    ------------------------------
    Mark Gillis
    Principal Consultant
    Triton Consulting
    ------------------------------



  • 5.  RE: Does DB2 support optimizer hints?

    Posted Mon February 03, 2025 01:34 AM

    Menno,

    You provide not enough details of access path to make best possible answer, e.g. join method, size of table B, etc.

    Optimizer has estimated that Table Space Scan is cheapest, possibly in conjunction with Merge Scan Join (maybe not).

    Optimizer cannot estimate query cost accurately in cases of complex inner queries.  One can use Optimizer hints as others mention or help Optimizer to understand better by SQL mod. e.g. Cursors should code Optimize for N Rows clause. 

    If we used OPTMIZE FOR 10 ROWS, as example, Db2 might then estimate cost of simple nested loop join via an index to be much cheaper option.  It helps DB2 a lot to estimate how many rows will be in the final result set. FETCH FIRST n ROWS ONLY can also be used in final query or in sub queries to restrict rows and make estimates of cost better as well.

    Deep analysis of where complex queries get estimates wrong is often not needed, using some basic techniques.   The internal cost estimates by Optimizer at each access path step are very useful not to estimate actual cost but to see where DB2 misunderstands the cost or the filtering the worst or where it thinks the high costs are happening (often correctly).   Many correlated subqueries can estimate filtering of 50% quite far from the true filtering, which could be very strong or very weak. Hypothetical example as I don't know if your complex query has that in there.

    One hopes the table stats are somewhere in the ball park of reality too. Stats are used for cost estimates, not needing to be completely accurate, but not completely misrepresentative too.



    ------------------------------
    Michael Hannan
    ------------------------------