Presto: Technical Handbook/Ch 1. Introduction to Distributed SQL Engines

Chapter 1

Introduction to Distributed SQL Engines

15 min read

0:00
1x

Chapter Summary

# Chapter 1: Introduction to Distributed SQL Engines In the modern data landscape, the concept of "Big Data" has shifted from a storage challenge to a computation challenge. As organizations move petabytes of data into data lakes (S3, GCS, ADLS) and high-performance storage layers, the traditional ...

Chapter 1: Introduction to Distributed SQL Engines

In the modern data landscape, the concept of "Big Data" has shifted from a storage challenge to a computation challenge. As organizations move petabytes of data into data lakes (S3, GCS, ADLS) and high-performance storage layers, the traditional monolithic database architecture has become a bottleneck. Enter the Distributed SQL Engine.

Presto (now Trino) stands as the industry standard for high-performance, distributed SQL query processing. This chapter explores the architecture, philosophy, and operational reality of these engines.


The Distributed SQL Paradigm

A distributed SQL engine separates storage from compute. Unlike a legacy RDBMS like PostgreSQL or Oracle, where the data resides on the same disk as the query processor, a distributed engine like Presto treats the data source as a remote entity.

The Architecture of Scale

Presto operates on a Coordinator-Worker model:

  1. Coordinator: The brain of the operation. It receives the SQL statement, parses it, creates an execution plan, and schedules tasks across workers.
  2. Workers: The muscle. They pull data from connectors, perform local computations (joins, aggregations, filters), and pass results to the next stage of the pipeline.

This architecture allows you to scale compute independently of storage. If your queries are slow, you add more workers. If your data grows, you simply add more space to your object storage.

"The true power of a distributed SQL engine is not just its ability to process massive datasets, but its ability to join disparate data sources—a SQL query spanning an S3 data lake, a Kafka stream, and an operational MySQL database—without moving the data first."


Key Concepts: Beyond Basic SQL

To master Presto at an advanced level, you must understand how the engine optimizes execution across a cluster.

1. Pipelined Execution

Unlike traditional MapReduce engines (like older versions of Hive) that write intermediate results to disk at every step, Presto executes queries in a pipeline. Data is streamed between stages in memory. This is why Presto feels like an interactive database rather than a batch-processing system.

2. Connector Abstraction

Presto uses a SPI (Service Provider Interface) to talk to storage. Whether it is Hive, Iceberg, Delta Lake, or Cassandra, Presto sees them all as tables.

Example: Querying S3 via Hive Connector

sql
-- Querying data stored as Parquet on S3
SELECT 
    customer_id, 
    COUNT(*) as order_count 
FROM hive.sales.orders 
WHERE order_date > DATE '2023-01-01'
GROUP BY customer_id
ORDER BY order_count DESC
LIMIT 100;

Practical Examples: Understanding Distributed Joins

The "Advanced" challenge in distributed SQL is the Join. When joining two large tables, the engine must shuffle data across the network so that matching keys reside on the same worker node.

Broadcast vs. Partitioned Joins

  • Broadcast Join: If one table is small (e.g., a lookup table), Presto sends a copy of that entire table to every worker node. This is lightning fast because it avoids a network shuffle of the large table.
  • Partitioned Join: If both tables are large, Presto uses a hashing algorithm to re-distribute both tables across the cluster nodes based on the join key.

Pro-Tip: If your query performance is lagging during a join, check the

text
EXPLAIN (TYPE DISTRIBUTED)
output to see if the engine is choosing a Broadcast or Partitioned join.

sql
-- You can force a broadcast join using hints
SELECT /*+ BROADCAST(dim_table) */ 
    fact.id, dim.name
FROM large_fact_table fact
JOIN small_dim_table dim ON fact.key = dim.key;

Real-World Use Case: The Federated Query

Imagine an e-commerce company that stores:

  1. Historical Sales: Parquet files on S3 (Hive/Iceberg).
  2. Current Session Data: Real-time stream in Kafka.
  3. User Profiles: PostgreSQL database.

A distributed SQL engine allows a data analyst to run a single query:

sql
SELECT 
    u.name, 
    s.total_spent, 
    k.last_action
FROM postgres.users.profiles u
JOIN hive.sales.historical_stats s ON u.id = s.user_id
JOIN kafka.streams.user_activity k ON u.id = k.user_id
WHERE k.event_time > NOW() - INTERVAL '1' HOUR;

This query runs across three different physical infrastructures, returning the result in seconds.


Best Practices for Advanced Users

1. Optimize Data Layout

Even the best engine cannot overcome poor physical data design.

  • Partitioning: Always partition your tables by frequently queried columns (e.g.,
    text
    date
    ).
  • File Format: Prefer columnar formats like Apache Parquet or ORC. These allow Presto to perform "predicate pushdown," skipping entire chunks of data that don't match your
    text
    WHERE
    clauses.

2. Monitor Cluster Health

Distributed systems fail gracefully, but they do fail. Monitor the following metrics:

  • Worker Memory Usage: If you frequently hit OOM (Out Of Memory) errors, you may need more workers or better data partitioning.
  • Queue Depth: High queue depths indicate the cluster is saturated.

3. Use
text
EXPLAIN
Effectively

Never run a massive, long-running query on production data without running

text
EXPLAIN
first. It reveals the plan:

sql
-- View the physical execution plan
EXPLAIN (TYPE DISTRIBUTED, FORMAT GRAPHVIZ)
SELECT * FROM large_table WHERE status = 'ACTIVE';

Summary and Next Steps

Distributed SQL engines have bridged the gap between traditional analytical databases and the vast, unorganized reaches of data lakes. As you progress through this book, keep the "Principle of Data Locality" in mind: minimize the distance data travels, and maximize the work done in parallel.

In the next chapter, we will dive deep into the Presto query planner and how to tune complex joins for optimal throughput.

Checklist for Chapter 1:

  • [ ] Understand the role of the Coordinator vs. Worker.
  • [ ] Differentiate between Pipelined and Batch execution.
  • [ ] Know when to force a Broadcast Join hint.
  • [ ] Recognize why columnar file formats are essential for performance.

Are you ready to optimize your queries? Let's move to Chapter 2: The Anatomy of a Query Plan.

What You Learned

  • 1

    Distributed SQL engines decouple compute from storage to overcome the scalability bottlenecks inherent in traditional monolithic database architectures.

  • 2

    The engine architecture relies on a Coordinator-Worker model where the coordinator orchestrates query planning while workers execute data processing tasks in parallel.

  • 3

    This paradigm allows organizations to scale computational power independently of their data storage layers by simply adding or removing worker nodes as demand shifts.

📖 Key Terms

Coordinator

The primary node in a Presto cluster that receives queries, parses them, creates the execution plan, and schedules tasks for worker nodes.

Connector

An abstraction layer in Presto that allows the engine to communicate with external data sources like S3, Hive, Kafka, or MySQL via a standard API.

Worker Node

A node in a Presto cluster responsible for executing tasks assigned by the coordinator and processing data segments.

MPP (Massively Parallel Processing)

A distributed computing architecture where data is processed in parallel across multiple independent nodes to achieve high-performance analytical queries.

Stage

A logical unit of execution in a Presto query plan, consisting of multiple tasks that run in parallel on different worker nodes.

Quiz requires login

Sign in to take the quiz and track your progress.