TATECHATLAS
◎ English
SAP & ERP / Tip

Using Plan Variants in SAP HANA Cloud to Stabilize Parameterized Query Performance

Plan Variants let SAP HANA Cloud cache multiple execution plans for a single parameterized statement, selecting the best plan per filter selectivity cluster to reduce performance swings caused by varying parameter values and data distribution.

On this page

Enable Plan Variants for parameterized queries where execution times spike when parameter values change selectivity dramatically. Monitor active variants through M_SQL_PLAN_VARIANTS and M_SQL_PLAN_VARIANT_STATISTICS, and verify that the number of cached variants stays within manageable bounds to avoid plan-cache pressure.

Context and Recommendation

When a parameterized query in SAP HANA Cloud receives vastly different parameter values across executions, the optimizer may compile a single plan that suits one selectivity range but degrades badly for others. This is a well-documented source of SQL performance fluctuation. Plan Variants address the problem by allowing SAP HANA to cache multiple execution plans for the same statement, each optimized for a specific cluster of filter selectivities. The recommendation is to enable Plan Variants for parameterized queries where you observe execution-time spikes correlated with parameter value changes, and to monitor them through the dedicated system views M_SQL_PLAN_VARIANTS and M_SQL_PLAN_VARIANT_STATISTICS. This approach complements, rather than replaces, general performance analysis with tools like the SQL Analyzer in SAP Business Application Studio.

Mechanism and Trade-offs

The mechanism operates through cluster creation. When a parameterized statement executes, the optimizer evaluates predicates against the actual parameter values. If the resulting selectivity falls outside the range of existing clusters, SAP HANA compiles a new plan tailored to that selectivity and associates it with a new cluster. Subsequent executions with similar filter values reuse the matching plan instead of forcing the originally cached plan. The primary trade-off is management overhead: each additional plan variant consumes memory in the plan cache and adds bookkeeping complexity. An excessive number of variants can increase cache pressure and make plan selection less predictable. The benefit is that execution times stabilize because each query execution receives a plan matched to its actual data distribution. The source lesson on Plan Variants explicitly describes this cluster-creation process as the core mechanism and notes that the optimizer evaluates predicates, compiles plans, and associates filter values to build these clusters.

Concrete Illustration

Consider a query filtering a large sales table by a date range. Most executions request a single day, yielding high selectivity and favoring an index scan. Occasionally a reporting job requests a full year, producing low selectivity where a column scan with parallelization is far more efficient. Without Plan Variants, SAP HANA caches one plan. If the cached plan is the index scan, the year-long query degrades severely because the index scan must traverse millions of entries. With Plan Variants enabled, the optimizer detects the selectivity mismatch, compiles a second plan optimized for the broad range, and creates a new cluster. The next year-long execution automatically selects the column-scan plan, reducing its runtime from minutes to seconds while the daily queries continue to use the index-scan plan. This scenario directly reflects the performance-fluctuation pattern the Plan Variants lesson describes, where variable data distributions cause suboptimal single-plan caching.

Applicability and Limits

Plan Variants are most useful for parameterized queries where filter selectivity varies by orders of magnitude between executions. They are less beneficial when all executions share similar selectivity, because a single cached plan already performs well. The monitoring views M_SQL_PLAN_VARIANTS and M_SQL_PLAN_VARIANT_STATISTICS let you inspect active clusters and per-variant execution statistics, which is essential for verifying that the mechanism is actually helping. A key limitation is that Plan Variants do not eliminate the need for sound data-model design, proper indexing, or partitioning. If the underlying model is poorly structured, no amount of plan caching will fix it. Additionally, the SQL Analyzer remains the primary tool for diagnosing whether a fluctuation stems from plan changes versus other factors such as engine switching or resource contention. Plan Variants should be treated as one layer in a broader performance-stability strategy, not a standalone fix.

Applicability

  • Are the observed performance fluctuations correlated with specific parameter values rather than with data-volume growth or concurrent load?
  • Does the query plan shape change between fast and slow executions, indicating selectivity-driven plan mismatch?
  • How many plan variants are currently cached for the statement, and does that number remain stable over time?
  • Are the underlying table indexes and partitioning appropriate, or is the fluctuation a symptom of a modeling issue?
  • Do the M_SQL_PLAN_VARIANT_STATISTICS show that each variant is being used by a meaningful share of executions?
  • Has the SAP HANA Cloud version been checked to confirm Plan Variants are enabled and supported in your configuration?

Plan Variants do not substitute for correct data-model design, indexing, or partitioning. An excessive number of variants increases plan-cache memory pressure and management complexity. The feature addresses selectivity-driven plan mismatch specifically; fluctuations caused by engine switching, resource contention, or auto-recompilation require different remediation. Monitoring through M_SQL_PLAN_VARIANTS and M_SQL_PLAN_VARIANT_STATISTICS is required to confirm the mechanism is effective and to detect variant proliferation.

Sources

  1. SAP Learning: Analyzing executions with the SQL Analyzer ↗
  2. SAP Learning: Using plan variants to optimize parameterized queries ↗
Back to top ↑