You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Small data (< 1 GB)spark.conf.set("spark.sql.shuffle.partitions", 8)
# Medium data (1-100 GB)spark.conf.set("spark.sql.shuffle.partitions", 200) # default# Large data (> 100 GB)spark.conf.set("spark.sql.shuffle.partitions", 2000)
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")
OPTIMIZE / VACUUM Guidelines
OPTIMIZE Frequency
Write Pattern
OPTIMIZE Frequency
Batch (daily)
After each batch
Streaming (continuous)
Every 1-4 hours (or use auto-compact)
Streaming (micro-batch)
Every 6-24 hours
Infrequent writes
Weekly or before heavy reads
VACUUM Retention
Scenario
Minimum Retention
Default (safe)
168 hours (7 days)
Active time travel needs
720 hours (30 days)
Aggressive cleanup
24 hours (requires safety check disabled)
Compliance requirements
Check data retention policy
-- Standard VACUUM
VACUUM table_name RETAIN 168 HOURS;
-- Preview what will be deleted
VACUUM table_name DRY RUN;
-- Check table file metrics
DESCRIBE DETAIL table_name;
Join Optimization Quick Reference
Join Type
When to Use
How to Trigger
Broadcast Hash
Small table (< broadcast threshold)
Auto or broadcast() / BROADCAST hint
Sort Merge
Both tables large
Default for large joins
Shuffle Hash
Medium table fits in memory per partition
AQE may choose, or SHUFFLE_HASH hint
Skewed join
One partition is disproportionately large
AQE auto-splits (skew join), or manual salting
Local Shuffle Read
Shuffle data already on same node
AQE automatically enables
# Force broadcastfrompyspark.sql.functionsimportbroadcastresult=large_df.join(broadcast(small_df), "key")
Switches sort-merge → broadcast at runtime if table fits
autoBroadcastJoinThreshold (default: 30MB)
Cost Optimization Checklist
Cluster Selection
Use Job Clusters for scheduled jobs (40-60% cheaper than all-purpose)
Use Spot Instances for fault-tolerant workloads (up to 90% savings)
Enable Autoscaling with appropriate min/max workers
Use Instance Pools for frequently started clusters
Compute Right-Sizing
Start small, scale based on metrics
Monitor cluster utilization in Ganglia/Metrics
Use Photon for supported workloads (better price/performance)
Consider Serverless SQL for ad-hoc queries
Storage and I/O
Run OPTIMIZE on frequently queried tables
Use appropriate file format (Delta preferred)
Partition large tables by query patterns
Cache frequently accessed DataFrames
Common Anti-Patterns
Anti-Pattern
Problem
Solution
Too many shuffle partitions
Overhead, small files
Use AQE or set based on data size
Over-partitioning
Too many small files
Max 10K partitions, use Z-ORDER
Broadcasting large tables
OOM errors
Set threshold or disable broadcast
Caching everything
Memory pressure
Cache only reused DataFrames
OPTIMIZE too frequently
Write amplification
Balance with read patterns
Z-ORDER on too many columns
Diminishing returns
Max 3-4 columns
Skewed keys without salting
One executor handles most data
Enable AQE skew join, or manually salt keys
Disabling AQE
Loses partition coalescing, skew handling
Keep spark.sql.adaptive.enabled=true
Performance Diagnostics Commands
-- Check table statistics
DESCRIBE DETAIL table_name;
-- View table history
DESCRIBE HISTORY table_name;
-- Analyze query plan
EXPLAIN EXTENDED SELECT*FROM table_name WHERE col ='value';
-- Check file count and sizesSELECTCOUNT(*) as num_files,
SUM(size)/1024/1024/1024as size_gb
FROM (DESCRIBE DETAIL table_name);