Real-time Analytics & OLAP • Approximate Query ProcessingHard⏱️ ~3 min
When to Use AQP vs Exact Queries
The Core Decision Framework:
The choice between approximate and exact queries is not about technical sophistication. It is about matching correctness requirements, latency targets, and cost constraints to the right technique.
Choose AQP When:
First, your queries are exploratory or analytical. Data scientists running ad hoc queries to understand user behavior, product managers checking dashboard metrics, engineers investigating incidents. These scenarios prioritize speed over perfect precision. An analyst exploring feature adoption does not care if the exact number is 10,182,341 versus 10.2 million.
Second, your data volume makes exact queries too slow or expensive. If a query scans 5 PB and takes 10 minutes, users stop exploring. A 1% sample with ±2% error that returns in 5 seconds enables iterative analysis. At cloud pricing of $5 per TB scanned, reducing I/O by 100x saves real money when running thousands of queries per day.
Third, you have aggregate heavy workloads with high cardinality. Distinct counts across millions of groups, percentile calculations over billions of events, top K queries. These benefit most from sketch based techniques. Traditional exact computation becomes I/O bound and memory intensive at scale.
Never Use AQP For:
Financial reporting, billing systems, compliance queries, and user facing Service Level Agreement (SLA) metrics. Any scenario where regulatory requirements or contractual obligations demand exact numbers. An error of ±2% in a billing query is unacceptable, even if it saves 10 seconds.
Highly selective queries or rare event analysis. If you are looking for a specific bug that occurs 100 times in a billion events, a 1% sample will likely miss it entirely or give meaningless estimates. Point lookups and exact ranking also do not benefit from approximation.
Join heavy queries with complex predicates. Sampling can introduce bias when join keys are not uniformly distributed. If your query joins five tables with multiple WHERE clauses, the approximation error becomes hard to reason about.
The Hybrid Middle Ground:
Many production systems use tiered strategies. Run approximate queries by default for interactive exploration. If a result looks surprising or critical, provide a button to "rerun exact" that takes longer but gives ground truth. Netflix might show approximate viewer counts in real time dashboards (using sketches updated every minute), but nightly executive reports use exact batch computations.
Some teams implement automatic fallback. If the query optimizer detects that approximation error would exceed acceptable bounds (for instance, because of high selectivity or skew), it automatically falls back to exact computation even if slower. This requires robust error estimation and monitoring.
Cost Benefit Analysis:
Consider a company running 10,000 analyst queries per day against a 5 PB warehouse. Each exact query scans an average of 1 TB (due to partitioning and predicates) at $5 per TB, costing $50,000 per day. With AQP at 1% sampling, cost drops to $500 per day. The $49,500 daily savings (over $18 million per year) justifies the engineering investment in building and maintaining approximate infrastructure.
However, if queries are already well optimized with materialized views and caching, and p95 latency is 2 seconds, the incremental benefit of AQP may not justify the added complexity. You are trading operational burden for marginal latency improvement.
"You introduce AQP when exact systems cannot meet interactive latency and cost targets, and only for query patterns where bounded error is acceptable."
AQP Territory
Dashboards, exploration, 99% reads, aggregate heavy
vs
Exact Territory
Billing, compliance, rare events, complex joins
💡 Key Takeaways
✓Use AQP for exploratory analytics where 1 to 3 percent error is acceptable but 10 minute queries kill iterative analysis
✓Use exact queries for billing, compliance, financial reporting, and any regulatory or contractual obligations requiring perfect accuracy
✓AQP saves cost at scale: reducing I/O by 100x on 10,000 queries per day can save over $18 million per year at cloud pricing
✓Avoid AQP for rare event analysis: a bug occurring 100 times in 1 billion rows will be absent from 1 percent samples
✓Hybrid strategies work best: approximate by default with "rerun exact" option for critical decisions or surprising results
✓Automatic fallback to exact computation when error bounds exceed thresholds maintains correctness for high selectivity queries
📌 Examples
1Decision tree: Product analytics dashboard (aggregates over millions of users, 99% of queries are exploratory) → use sketch based AQP. Billing system (exact revenue per customer required for invoices) → use exact queries only.
2Netflix approach: real time dashboards show approximate concurrent viewers from sketches updated every minute (sub second latency). Nightly executive reports recompute exact numbers via batch jobs (10 minute latency, perfect accuracy).
3Cost calculation: 10,000 queries per day × 1 TB average scan × $5 per TB = $50K per day exact. With 1% sampling: 10,000 × 0.01 TB × $5 = $500 per day. Savings of $18M per year justifies AQP engineering investment.