Future Optimization Opportunities
This document tracks potential performance optimizations that are not critical given the current implementation efficiency.
✅ Completed Optimizations
Stack Overflow Fix (Phase 2.2 - Completed)
Problem: Thread stack overflow when processing wide tables or large batches.
Solution Implemented: - Eliminated duplicate Arrow builder code - Optimized string handling (removed nested closures) - Increased tokio worker stack to 16MB - Added panic recovery in UI server - Result: Handles 200+ column tables, server stays up on errors
IN Clause Predicates (Phase 2.2a - Completed)
Problem: Massive OR predicates caused stack overflow in datafusion query planner.
- Before: WHERE (id=1) OR (id=2) OR ... OR (id=10000) = 20+ chunked operations
- Root cause: Datafusion parser can't handle 10K+ OR conditions
Solution Implemented:
- Use datafusion Expr::InList API: WHERE id IN (1, 2, ..., 10000)
- Build type-safe predicates with col("pk").in_list(vec![lit(...)], false)
- Composite keys: (pk1 IN (...) AND pk2 IN (...))
- Result: Single transaction instead of 20+ chunks
Performance Impact: | Operation | Before (Chunked OR) | After (IN Clause) | Improvement | |-----------|---------------------|-------------------|-------------| | Hash-based upsert | 20 deletes + 1 insert | 1 delete + 1 insert | 21 → 2 tx | | CDC/CT upsert | 20 deletes + 1 insert | 1 delete + 1 insert | 21 → 2 tx | | CDC/CT delete | 20 deletes | 1 delete | 20 → 1 tx |
🔮 Future Optimization: Native MERGE API
Overview
Current implementation uses separate delete + insert operations (2 transactions per upsert). Native Delta Lake MERGE API could reduce to 1 transaction.
Current Performance
- Hash-based upserts: 1 delete + 1 insert = 2 transactions
- CDC/CT upserts: 1 delete + 1 insert = 2 transactions
- CDC/CT deletes: 1 delete = 1 transaction ✅ Already optimal
Potential with Native MERGE
// Single MERGE operation per batch
table.merge(source_df, predicate)
.when_matched_update(|update| { /* set all columns */ })
.when_matched_delete(|delete| { /* __$operation = 1 (CDC) */ })
.when_not_matched_insert(|insert| { /* insert all columns */ })
.execute()
.await?;
Impact: 2 transactions → 1 transaction (50% reduction)
Why Not Implemented Yet?
- Diminishing Returns: IN clause fix already gave us 90% of the benefit (21 → 2 transactions)
- API Stability: deltalake 0.30 merge API has some rough edges:
- Requires DataFrame (not RecordBatch) conversion
- Closure-based builder pattern makes error handling complex
- API may evolve in future versions (0.31+)
- Code Complexity: Native MERGE requires significant refactoring:
- Convert JSON → Arrow → DataFrame pipeline
- Handle operation dispatch in closures
- Test all three CDC/CT/Hash paths
- Current Performance Good Enough:
- 10K row batch = 2 transactions (vs 21 before)
- No stack overflow risk
- Clean, maintainable code
When to Revisit?
Consider native MERGE if: - ✅ deltalake 0.31+ stabilizes MERGE API - ✅ User reports transaction overhead as bottleneck - ✅ Need atomic guarantees (currently delete + insert = 2-phase) - ✅ Significant performance regression in write path
Prerequisites for Implementation
Infrastructure already in place:
- ✅ rows_to_dataframe() - Converts JSON rows to DataFrame
- ✅ build_merge_predicate() - Generates PK join conditions
- ✅ IN clause predicates - No predicate size issues
- ✅ Operation parsing - CDC/CT operation detection
Code marked for future use:
#[allow(dead_code)] // Helper for future native merge implementation (Phase 2.2b)
pub fn rows_to_dataframe(rows: &[Value], fields: &[Field]) -> Result<RecordBatch> {
// Ready for DeltaTable.merge(dataframe, predicate)
}
Estimated Effort
- Time: 2-3 days (refactoring + testing)
- Risk: Medium (API stability, error handling complexity)
- Benefit: 50% transaction reduction (2 → 1 per batch)
- Priority: Low (current perf acceptable)
Other Future Optimizations
Parallel Table Extraction
Current: Sequential table extraction Potential: Extract multiple tables concurrently Benefit: Faster full catalog extraction Effort: Medium (need connection pooling, state management)
Adaptive Batch Sizing
Current: Fixed 10K row batches Potential: Adjust batch size based on row width, memory usage Benefit: Better memory efficiency for wide/narrow tables Effort: Low (dynamic batch size calculation)
Incremental Schema Evolution
Current: Full schema refresh on each run Potential: Track schema changes, only update modified tables Benefit: Faster startup time for large catalogs Effort: Medium (need schema versioning, change detection)
Bottom Line: With IN clause predicates, we have excellent performance. Native MERGE is the final 10% optimization that can wait for deltalake API maturity.