Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Parallel and distributed query processing

Notes that explain principles, laws, algorithms and costs for parallel and distributed query processing, with definitions, formulas, worked examples and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Parallel and distributed query processing lets databases answer large queries faster and serve many users by using many CPUs and many machines. Engineers use these ideas when designing data warehouses, distributed OLAP/OLTP systems, cloud databases and parallelising expensive joins and aggregations.

Key ideas

Start from first principles.

  • Parallel query processing: processing a single query using multiple processors (cores or machines) concurrently to reduce response time. Define each processor as a separate execution context.

  • Distributed query processing: executing queries when the data and/or compute are located on multiple networked machines; query execution must move data, code or both between machines.

  • Shared-nothing architecture: each node has private CPU, memory and disk; nodes communicate only by messages (network). This is the common architecture for scalable parallel databases.

  • Shared-disk architecture: all nodes access a common disk storage (e.g. SAN); coordination is needed for concurrent access.

  • Shared-memory architecture: multiple processors access the same RAM; typical on single multi-core servers.

  • Data partitioning (fragmentation): dividing a relation across nodes. Common methods:

    • Hash partitioning: apply a hash function on partitioning attribute(s) and assign tuples to nodes by hash value.
    • Range partitioning: assign contiguous ranges of attribute values to different nodes.
    • Round-robin: assign tuples in cyclic order, used for load balancing but not locality.
  • Replication: storing copies of the same data on multiple nodes to reduce network reads and increase availability.

  • Types of parallelism:

    • Inter-query parallelism: different queries run on different processors.
    • Intra-query parallelism: a single query is executed in parallel; includes pipeline parallelism and partitioned parallelism.
    • Pipeline parallelism: operators stream tuples between processors so one operator can start before its inputs are fully produced.
    • Partitioned parallelism: input is partitioned and independent worker tasks run the same operator on different partitions.
  • Operator placement: decide on which node each relational operator (scan, join, aggregate) runs. Placement affects network cost. Also known as data shipping (move data to operator), query shipping (send operator to data), or hybrid shipping.

  • Parallel join algorithms (common exam topics):

    • Partitioned Hash Join: both relations are hash-partitioned on join key across nodes; each node builds hash table on its local partition of one relation and probes with the local partition of the other.
    • Broadcast (replicate) Join: small relation is sent (broadcast) to all nodes holding parts of the large relation; join is done locally. Preferable when the small relation fits in memory on each node and broadcasting it is cheaper than repartitioning the large relation.
    • Parallel Sort-Merge Join: range partition both relations on join-key and sort locally, then merge-join per partition.
    • Parallel Aggregation (two-phase): perform local pre-aggregation on each partition; then repartition by the group-by key and perform final aggregation. Using combiners/partial aggregates reduces network volume.
  • Communication patterns: broadcast (send to all), repartition (all-to-all shuffle), gather (many-to-one) and scatter (one-to-many). These dominate cost in distributed systems.

  • Cost components: for a distributed/parallel operator, cost includes CPU time, disk I/O, and network transfer time. Network cost often dominates for wide-area or busy clusters.

  • Speed-up and efficiency: exact definitions universities expect:

    • Speed-up S = time of best sequential execution / time of parallel execution.
    • Efficiency E = S / p where p is number of processors.
    • Scale-up: increase both dataset size and system size (processors/nodes) by a factor k while aiming to keep response time roughly constant; batch scale-up and transactional scale-up are commonly examined.
    • Size-up: increase dataset size by a factor k on a fixed system; ideal behavior is linear growth in time.
  • Amdahl's Law (exact statement and assumptions):

    • Statement: If fraction P of a program is perfectly parallelisable and fraction (1−P) is strictly serial, then the maximum speed-up using p processors is S(p) = 1 / ((1−P) + P/p).
    • Assumptions: fixed total problem size; parallel part scales perfectly with p, serial part does not change; no extra parallelisation overhead (idealised).
    • Derivation (step-by-step):
      1. Let total sequential time be T_seq = T_serial + T_parallel.
      2. Let fraction P = T_parallel / T_seq, so T_serial = (1−P)·T_seq.
      3. On p processors the parallel part takes T_parallel / p = P·T_seq / p.
      4. So T_par = (1−P)·T_seq + P·T_seq / p = T_seq·((1−P) + P/p).
      5. Speed-up S = T_seq / T_par = 1 / ((1−P) + P/p).
  • Gustafson's Law (exact statement): For fixed execution time, if the parallel fraction scales with problem size, scaled speed-up S(p) = (1−P) + p·P. Assumes problem size grows so parallel work increases while serial part stays constant.

  • Skew and load balance: if buckets/partitions are uneven, some workers will be idle; skew is often the main practical limit to speed-up. Techniques to mitigate skew include histogram-aware range partitioning, using more (virtual) buckets than workers and mapping them dynamically, and splitting heavy-hitter keys via salting.

  • Distributed query optimisation techniques:

    • Localization: try to execute as much as possible where data resides to reduce transfers.
    • Semi-join: send only join-key values to another site, receive matching tuples—reduces network cost when selectivity is low.
    • Bloom filters: compact probabilistic filters sent across network to filter remote tuples before transfer (introduces false positives but no false negatives).

Where this relates to other topics in the course: refer to Data fragmentation and replication (for partitioning/replication), Parallel database architectures (for shared-nothing / shared-disk), Query optimisation and evaluation plans (for operator placement and cost), and Parallel databases: speed-up and parallelism (for Amdahl/Gustafson).

Drawings to include in answers (describe exactly):

  • For shared-nothing: draw 3 boxes labelled Node 1..3, each with CPU, RAM, Disk; arrows between boxes labelled Network. For a partitioned hash join: draw R and S each split into two partitions by hash function, arrows crossing to nodes, then local build/probe boxes.

Formulas

S = T_seq / T_par — S is speed-up (dimensionless); T_seq is best sequential execution time (s); T_par is observed parallel execution time (s). Applies to comparing same work.

E = S / p — E is efficiency (dimensionless); p is number of processors (count, no units). Applies when measuring resource use.

S(p) = 1 / ((1 - P) + P / p) — Amdahl's Law; P is parallel fraction (0..1), p is processors. Assumes fixed problem size and ideal parallelisation.

S_gustafson(p) = (1 - P) + p * P — Gustafson's Law; same P and p; applies when problem size scales with p.

Cost_total = Cost_CPU + Cost_IO + Cost_network — total execution time when phases do not overlap; if there is overlap, use the critical-path time rather than a simple sum. Units: seconds for each term.

Cost_network = data_size_transferred / network_bandwidth + network_latency_overheads — data_size_transferred in bytes, bandwidth in bytes/s, latency in s. Use when estimating transfer time.

Speed-up_limit (p -> infinity) by Amdahl = 1 / (1 - P) — upper bound for large p.

Worked examples

  1. Amdahl numeric example (exam-style) Given: A query plan has 75% of its time perfectly parallelisable (P = 0.75). We run on p = 8 processors. Sequential time T_seq = 200 s (for clarity). Steps:
  1. Use Amdahl: S(p) = 1 / ((1−P) + P/p) = 1 / (0.25 + 0.75/8).
  2. Compute 0.75/8 = 0.09375. So denominator = 0.25 + 0.09375 = 0.34375.
  3. S = 1 / 0.34375 = 2.90909.
  4. Parallel time T_par = T_seq / S = 200 / 2.90909 = 68.75 s.
  5. Efficiency E = S / p = 2.90909 / 8 = 0.36364 (36.36%). Final answers: Speed-up = 2.909 (dimensionless), Parallel time = 68.75 s, Efficiency = 36.36%.
  1. Partitioned Hash Join trace (algorithmic example) Given small tables (join on A): R = {(1,a),(2,b),(3,c)} located on Node1; S = {(2,x),(1,y),(4,z)} located on Node2. Use 2-node partitioned hash join with hash = value of A mod 2. Steps (draw nodes and arrows as described):
  1. Both nodes compute hash(A) for their tuples: Node1:R hashes -> 1->1,2->0,3->1 => partitions: p0:{(2,b)}, p1:{(1,a),(3,c)}. Node2:S hashes -> 2->0,1->1,4->0 => p0:{(2,x),(4,z)}, p1:{(1,y)}.
  2. Repartition (all-to-all): Node1 sends its p0 to Node with id 0 (say NodeA), p1 to NodeB; Node2 does same. With 2 nodes they exchange p0 and p1.
  3. On each node join local partitions: Node for p0 has R-p0 {(2,b)} and S-p0 {(2,x),(4,z)}; build hash on smaller (R-p0) and probe: match (2,b) with (2,x) => output (2,b,x). Node for p1 has R-p1 {(1,a),(3,c)} and S-p1 {(1,y)}; matches (1,a) with (1,y) => output (1,a,y).
  4. Final result union of outputs: {(2,b,x),(1,a,y)}. Node-local order arbitrary. This trace shows local hashing and network repartition step.
  1. Semi-join cost comparison (short numeric) Given relations A (large) at Site1 size 1 GB, B (smaller) at Site2 size 100 MB. Join selectivity is 1:10 (only 10% of B matches A). Network bandwidth = 100 MB/s. Compare naive shipping B to Site1 vs semi-join. Steps:
  1. Naive: send 100 MB over network => time = 100 MB / 100 MB/s = 1 s.
  2. Semi-join: send join keys from A or B? Efficiently send projection of B's join keys (assume B's keys compressed to 10 MB). Send 10 MB => 0.1 s, get matching keys back causing transfer of 10% of A or B tuples? Assuming we then fetch matching tuples totaling 10 MB => another 0.1 s. Total ~0.2 s. Savings significant when keys are small and selectivity low. Final: semi-join reduces network time from 1.0 s to ~0.2 s in this scenario.

Common mistakes

  • Confusing shared-nothing with shared-disk or shared-memory; each has different failure and coordination properties.
  • Not accounting for skew (uneven partition sizes) which kills speed-up; assuming perfect balance by default.
  • Using Bloom filters without noting false positives; they reduce but do not eliminate network transfer.
  • For partitioned joins, forgetting the cost of the all-to-all shuffle step.

For your exam

Question types you will see: definitions (define the architectures, partitioning methods), short notes (compare shared-nothing and shared-disk), derivations (derive Amdahl's law), numericals (compute speed-up, efficiency, network cost), trace an algorithm (partitioned hash join), draw diagrams (nodes, data flow), and short design problems (choose join strategy for given sizes and network).

What a full-marks answer includes:

  • Precise definitions of terms used (bold them when writing notes).
  • Clear assumptions stated (e.g., fixed problem size, network bandwidth known).
  • Correct formulae with units and substitution of numbers step-by-step.
  • Diagrams where asked: label nodes, data partitions, network links and arrows showing data flow.
  • Discussion of practical issues: skew, replication overhead, and when semi-join or broadcast is preferable.

Quick check

  1. State Amdahl's Law formula for speed-up.
  2. Name two partitioning methods and a case when you use each.
  3. What is the dominant cost when joining large relations across racks in a datacenter? Answers: 1) S(p) = 1 / ((1−P) + P/p). 2) Hash partitioning (when you need even split for joins); Range partitioning (when range queries and local sorts are common). 3) Network transfer cost (network bandwidth and latency).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?