Modernization Hub

Dynamic Plan Selection

Enhanced Definition

Dynamic Plan Selection, in the context of IBM Db2 for z/OS, refers to the capability to choose a specific Db2 package (and thus its associated access path) at runtime, rather than having it fixed at bind time. This allows different users or applications to execute the same SQL statement using potentially different, optimized access paths based on runtime conditions or special register settings. Dynamic Plan Selection, often referred to as `REOPT` (Reoptimization), is a DB2 for z/OS feature that allows the DB2 optimizer to choose or re-evaluate the access path for an SQL statement at runtime, rather than solely at the time the package is bound. This process enables DB2 to adapt to the current state of data and host variable values, potentially leading to more efficient execution plans.

Key Characteristics

    • Runtime Determination: The specific Db2 package to be used for a dynamic SQL statement is determined at the time of execution, not during the BIND process.
    • Special Register Dependency: It primarily relies on the values of Db2 special registers such as CURRENT_SQLID, CURRENT_S_SQLID, CURRENT_SCHEMA, CURRENT_PATH, or CURRENT_PACKAGE_PATH.
    • Multiple Package Versions: Allows for multiple versions of a package (each potentially with a different access path) to be bound under different QUALIFIERs or OWNERs, and then selected dynamically.
    • DYNAMICRULES Option: The DYNAMICRULES option of the BIND PACKAGE command (DYNAMICRULES(BIND) or DYNAMICRULES(RUN)) dictates how authorization, qualification, and path resolution are handled for dynamic SQL within the package.
    • Flexibility in Optimization: Provides flexibility to optimize SQL statements for different scenarios, users, or data distributions without requiring a rebind of the entire application.

Use Cases

    • Multi-tenant Applications: In applications serving multiple clients, each client might have its own set of tables or data subsets. Dynamic plan selection can direct SQL to use packages optimized for each client's specific data structure or access patterns.
    • Testing and Development: Allows developers to test new access paths or package versions by simply changing a special register value (e.g., CURRENT_SQLID) without affecting other users or requiring a full application rebind.
    • Performance Tuning: DBAs can bind multiple packages with different access paths (e.g., forcing different indexes) and dynamically switch between them to find the optimal performance for specific workloads or time periods.
    • Application Migration: Facilitates phased migrations or A/B testing of new application versions by allowing a subset of users to execute SQL through a new package while others use the old one.

Related Concepts

Dynamic Plan Selection is intrinsically linked to Db2 Packages and Plans. A Db2 PLAN is a collection of PACKAGES, and each PACKAGE contains the access paths for SQL statements within a program. Dynamic plan selection allows an application to choose *which* package (and therefore which set of access paths) to use at runtime. It contrasts with static SQL, where the access path is determined and fixed at BIND time. It heavily leverages Db2 Special Registers to influence the selection process, providing a mechanism for runtime control over SQL Optimization and Access Path selection.

Best Practices:
  • Understand DYNAMICRULES: Carefully choose between DYNAMICRULES(BIND) and DYNAMICRULES(RUN). BIND typically offers more predictability and security as authorization and qualification are based on the package binder's ID. RUN bases them on the end-user's ID, which can be less secure and harder to manage.
  • Monitor Performance: Dynamically chosen access paths can sometimes be less optimal than statically bound ones. Thoroughly monitor SQL performance (e.g., using Db2 EXPLAIN and performance monitors) to ensure desired behavior.
  • Document Logic: Clearly document the logic and conditions under which special registers are set and how they influence package selection to ensure maintainability and troubleshooting.
  • Security Considerations: Be mindful of the security implications, especially when using DYNAMICRULES(RUN), as it grants the end-user's authorization ID control over qualification and path resolution.
  • Minimize Complexity: While powerful, overusing dynamic plan selection can introduce complexity. Use it judiciously for specific, well-defined scenarios where its benefits outweigh the added management overhead.

Related Vendors

ASE

3 products

IBM

646 products

Tone Software

14 products

Trax Softworks

3 products

Related Categories

Performance

171 products

Operating System

154 products

Databases

211 products

Automation

222 products

Browse and Edit

64 products