Monitoring Plan Variants in SAP HANA Cloud with System Views
Track active Plan Variants in SAP HANA Cloud with M_SQL_PLAN_VARIANTS and their execution metrics via M_SQL_PLAN_VARIANT_STATISTICS, then interpret the data to detect and resolve performance fluctuations caused by suboptimal plan selection.
On this page
Key idea
Monitor active Plan Variants and their execution statistics by querying the system views M_SQL_PLAN_VARIANTS (to list which statement variants exist and when they were created) and M_SQL_PLAN_VARIANT_STATISTICS (to get execution counts, timings, and per-variant performance metrics). This lets you correlate parameter-value changes or data skew with plan selection and performance fluctuation, then decide whether to recompile, adjust parameters, or use SQL Analyzer for deeper plan inspection.
Understanding Plan Variants and the Need for Monitoring
Plan variants address an inherent limitation of caching only a single execution plan for parameterized queries. When a statement such as SELECT ... FROM orders WHERE status = ? is executed with different bind values, the optimizer may choose a plan that works well for one selectivity but poorly for another, leading to performance fluctuations. Plan variants allow SAP HANA to cache multiple plans, one per cluster of filter values that share similar selectivity, so the optimizer can pick the most appropriate one for the current parameter set.
Monitoring plan variants is essential because the correctness of the chosen plan directly affects response time. Data skew, changing parameter distributions, or a newly created variant can cause the cached plan to become suboptimal. By inspecting the system views, you can see when variants were created, how many exist for a given statement, and the historical execution statistics that reveal which variant has been used and how it performed.
Querying M_SQL_PLAN_VARIANTS to List Active Variants
M_SQL_PLAN_VARIANTS gives you the registry of active plan variants in the SAP HANA instance. Every row corresponds to a variant that the optimizer has decided to keep for a particular parameterized SQL statement. Useful columns typically include the statement hash, the variant identifier, the creation timestamp (or last-used flag), the underlying SQL text or hash, and the cluster metadata that associates a variant with a selectivity bucket.
To inspect the current state, run a simple SELECT * FROM M_SQL_PLAN_VARIANTS; and filter by the statement you are interested in. Look for statements with more than one active variant, because that indicates the optimizer is maintaining separate plans for different filter selectivity ranges. Check the creation timestamps to see whether a new variant was introduced around the time performance degradation started. If the view reports that no variants exist for a given parameterized query, the statement may not yet have had enough diverse parameter values to trigger variant generation.
The example query above is illustrative; the exact column names and availability depend on your SAP HANA Cloud version and the privileges granted to your user. In a real monitoring script, you would select specific columns and add a WHERE clause on the statement hash or SQL text to narrow the result set.
SELECT * FROM M_SQL_PLAN_VARIANTS;Querying M_SQL_PLAN_VARIANT_STATISTICS for Execution Metrics
M_SQL_PLAN_VARIANT_STATISTICS contains per-variant execution data: how often each variant was executed, cumulative execution time, the last execution timestamp, and possibly average or recent execution duration. This is the view you use to compare the performance of the different variants associated with a statement.
A practical query filters by the same statement hash as in the first view and orders by execution count or by total time, so you can spot which variant is dominant and whether one variant is disproportionately slow. For example, if variant A has 10,000 executions averaging 5 ms, but variant B has only 20 executions averaging 800 ms, the slow variant might be the one selected whenever a rare, highly selective parameter value appears. That pattern correlates exactly with performance fluctuations.
The statistics are historical and cumulative. To track changes over time, you need to take periodic snapshots and compare deltas, because the view itself does not provide a delta or per-execution breakdown. The example statement SELECT * FROM M_SQL_PLAN_VARIANT_STATISTICS; is meant to illustrate the query pattern; in practice you would aggregate by statement and variant, and you might join with M_SQL_PLAN_VARIANTS to get the SQL text.
SELECT * FROM M_SQL_PLAN_VARIANT_STATISTICS;Interpreting Statistics to Detect Performance Fluctuations
The central interpretation task is to correlate plan variant activity with observed performance degradation. Start by identifying statements that have multiple active variants, then compare their execution statistics to see if one variant dominates while another sporadically appears with much higher execution time. A variant that is rarely used but extremely slow indicates that a particular filter selectivity bucket is being handled poorly, which typically happens when data is skewed or when a new parameter value falls into a cluster that the optimizer has not yet optimized well.
When you detect such a pattern, you have three practical actions: force a recompilation of the statement (for example, by clearing the plan or using ALTER SYSTEM with an appropriate command, depending on your privileges), apply SQL Plan Management to pin a stable plan, or dig deeper with the SQL Analyzer. The SQL Analyzer, available in SAP Business Application Studio as part of the SAP HANA Performance Tools extension, generates a plan (.plv) file that shows operator-level timings, record counts, and execution engines. The source notes that to use it, your development space must include that extension and the user needs TRACE ADMIN and INIFILE ADMIN system privileges. The SQL Analyzer provides the detail that the system views do not: exact step durations, bottleneck operators, and how much parallelization was achieved.
The decision between recompiling and pinning a variant depends on whether the fluctuation is transient or persistent. If new data skew is permanent, recompiling once may fix the immediate issue but the optimizer may just create the same suboptimal variant again. In that case, using a plan stability mechanism or adjusting the parameters that control variant generation is more durable. The statistics view gives you the evidence to choose the right course, but it does not prescribe the action.
Applicability
- Does the query involve parameterized SQL with varying filter selectivity, so plan variants are actually created?
- Do you have the required system privileges to read M_SQL_PLAN_VARIANTS and M_SQL_PLAN_VARIANT_STATISTICS?
- Is the performance fluctuation you are investigating potentially due to plan changes rather than resource contention or application-side factors?
- Are you prepared to poll the statistics views periodically because they capture historical data rather than real-time state?
Where this applies
The system views provide metadata and execution statistics but not the full execution plan details. Use SQL Analyzer for deep plan analysis and operator-level insight. Also, plan variants only exist for parameterized queries with varying filter selectivity; statements without such conditions will not appear. Statistics reflect historical executions and require periodic polling to detect changes.