How Outdated Stats on Large Partitioned Tables Break Query Plans in GaussDB
This article explains how GaussDB's auto-statistics collection thresholds fail to update global stats for large partitioned tables when new partitions receive insufficient data changes, causing the optimizer to choose nested-loop joins over hash joins and full scans over index scans, and outlines four mitigation strategies including adaptive thresholds and manual collection.
Reading font
Partition Table Statistics Auto-Collection Strategy
Partitioned tables split data horizontally by a partition key. In GaussDB, statistics are maintained at two levels:
Partition table level : When total inserts, updates, and deletes across all partitions exceed the auto-collection threshold (default: 50 rows + 10% of table size), statistics are gathered for all qualifying partitions and aggregated into the global catalog views pg_class and pg_statistic.
Individual partition level : If a single partition's changes exceed the threshold while the global table has not, only that partition's statistics are refreshed and stored in pg_partition — but the global statistics are not updated.
Because the global threshold is based on the entire table's row count, a massive partitioned table may never reach it even when new partitions receive substantial data.
Concrete Scenario: Monthly Sales Log Table
Consider a table sales_log partitioned by month, with 36 partitions covering three years and 1 billion total rows. The global auto-analyze threshold is 10% (100 million rows). If 1 million rows are inserted daily into the newest partition, three months of new data (≈90 million rows) still falls short of the 100 million threshold. Consequently, global statistics — total row count, index cardinality, column value distributions — remain stale, and the optimizer cannot see the latest data distribution.
Impact of Inaccurate Statistics
2.1 Dynamic Partition Pruning
Dynamic partition pruning occurs when the partition key filter comes from a join result, e.g.:
SELECT s.*
FROM sales_log s
JOIN promotion_campaigns p ON s.campaign_id = p.id
WHERE p.campaign_type = 'NewYearSpecial';The optimizer must estimate:
Join cardinality : rows in promotion_campaigns matching campaign_type = 'NewYearSpecial'.
Join cost : using global statistics of sales_log. Because stats are stale, the optimizer does not know that the latest partition (e.g., p_2026_03) contains many rows for the promotion.
Plan choice : it may assume sales_log is small and pick a Nested Loop Join , probing sales_log once per promotion ID. Even though each probe prunes partitions, the overall I/O and CPU cost becomes huge if the driving table is chosen incorrectly.
2.2 Cross-Partition Access
Analytical queries often scan multiple partitions, e.g.:
SELECT product_category, SUM(sale_amount) AS total_sales
FROM sales_log
WHERE status = 'T'
AND sale_date >= '2026-01-01' AND sale_date < '2026-04-01'
GROUP BY product_category
ORDER BY total_sales DESC;This query accesses partitions p_2026_01, p_2026_02, p_2026_03. Since their statistics are missing from the global catalog, the optimizer may underestimate the row count and choose a full partition scan over an index scan, degrading performance.
Solutions for Stale Statistics on Large Partitioned Tables
Adaptive collection thresholds : Database engines should adjust the auto-analyze threshold based on table size (e.g., 10% for 10M rows, 1% for 100M rows).
Per-table threshold configuration : GaussDB allows ALTER TABLE xx SET (autovacuum_analyze_scale_factor=0.01) so a 100M-row table triggers auto-analyze after 1M changes. This requires evaluating the trade-off between timely stats and the overhead of frequent analysis during peak hours.
Scheduled manual collection : Identify partitions with fresh local stats but stale global stats, then run ANALYZE during off-peak windows. This adds operational overhead but is a common practice in Oracle and DB2 environments.
Hints or plan binding : Use optimizer hints (e.g., /*+ INDEX(sales_log idx_date) */) or capture and bind a known-good plan. Both approaches are invasive, require code changes or DBA intervention, and carry risk if data distribution shifts later.
The article concludes that no universal standard exists; vendors recommend different combinations of the above. The fundamental shift needed is from passive reliance on auto-collection to proactive statistics management.
Reference:
GaussDB数据库统计信息自收集机制: https://mp.weixin.qq.com/s?__biz=MzI3MzUzMjIxNA==&mid=2247486425&idx=1&sn=4e823cc895a32fb34c06b068036d786c&scene=21#wechat_redirect
For questions about attribution, corrections or content rights, please contactand we will review it promptly.