Docsity

Nested Loop Join: Implementation and Analysis, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Nested Loop Join: Implementation and Analysis and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Advanced Database Systems

Notes 06:Query Execution

(Sort and Join operators)

parse

Query rewriting

Physical plan generation

execute result

SQL query

parse tree

statistics logical query plan

physical query plan

Query Processing - In class order

2; 16.

3; 16.2,16.

1; 13, 15

4; 16.4—16.

  • NLJ (conceptually)

for each r ∈ R1 do

for each s ∈ R2 do

if r.C = s.C then output r,s pair

Nested Loop Join (NLJ)

B C a 10 a 20 b 10 d 30

C D

10 cat 40 dog 15 bat 20 rat

R1 R

Nested Loop Join (contd.)

  • Tuple-based
  • Block-based
  • Asymmetric

Roadmap

  • A simple operator: Nested Loop Join
  • Preliminaries
    • Cost model
    • Clustering
    • Operator classes
  • Operator implementation (with examples from joins)
    • Scan-based
    • Sort-based
    • Using existing indexes
    • Hash-based
  • Buffer Management
  • Parallel Processing

Operator Cost Model

  • Simplest: Count # of disk blocks read and

written during operator execution

  • Extends to query plans
    • Cost of query plan = Sum of operator costs
  • Caution: Ignoring CPU costs

Parameters used in Cost Model

B(R) = # blocks storing R tuples

T(R) = # tuples in R

V(R,A) = # distinct values of attr A in R

M = # memory blocks available

Roadmap

  • A simple operator: Nested Loop Join
  • Preliminaries
    • Cost model
    • Clustering
    • Operator classes
  • Operator implementation (with examples from joins)
    • Scan-based
    • Sort-based
    • Using existing indexes
    • Hash-based
  • Buffer Management
  • Parallel Processing

Clustering Index

Tuples with a given value of the search

key packed in as few blocks as possible

A index

10 10 35 19 19 19 19 42 37

Examples

T(R) = 10,

B(R) = 200

If R is clustered, then # R tuples per block =

Let V(R,A) = 40

If I is a clustering index on R.A, then # IOs to

access σR.A = “a” (R) = 250/50 = 5

 If I is a non-clustering index on R.A, then

IOs to access σR.A = “a” (R) = 250 ( > B(R))

Roadmap

  • A simple operator: Nested Loop Join
  • Preliminaries
    • Cost model
    • Clustering
    • Operator classes
  • Operator implementation (with examples from joins)
    • Scan-based
    • Sort-based
    • Using existing indexes
    • Hash-based
  • Buffer Management
  • Parallel Processing

Implementing Tuple-at-a-time Operators

  • One pass algorithm:
    • Scan
    • Process tuples one by one
    • Write output
  • Cost = B(R)
    • Remember: Cost = # IOs, and we ignore the cost to write output

Implementing a Full-Relation Operator, Ex: Sort

  • Suppose R won’t fit within M blocks
  • Consider a two-pass algorithm for Sort;

generalizes to a multi-pass algorithm

  • Read R into memory in M-sized chunks
  • Sort each chunk in memory and write out

to disk as a sorted sublist

  • Merge all sorted sublists
  • Write output

Two-phase Sort: Phase 1

Suppose B(R) = 1000, R is clustered, and M = 100

1 2 3 4 5

999 1000

1 2 3 4 5

96 97 98 99 100 Memory

1 101

100 200 201 300

801 900 901^1000

Sorted Sublists

R