Relational Algebra

Part 1 The basics

X - cartesian product : joins each row in both tables, the joining of selected tables ‘from’ SQL

σ - select : the where command, ‘where’ command from SQL

П - project : the showing of the rows selected, ‘select’ command from SQL

⋈ - theta join : inner join, ‘join’ command from SQL

SELECT c.row1, d.row1

FROM table1 c JOIN table2 d ON c.row1 = d.row1

WHERE c.row2 > 100;

П c.row1, d.row1 (

((σ c.row2 > 100 (table1 c)))

⋈ c.row1 = d.row1 (table2 d)

)

Part 2 Query Trees

Rules:

  1. Selects next to each other can merge
  1. Select can commute with Project
  1. Select can commute with other Selects
  1. Theta Join and Cartesian Product and Select and project can commute
  1. When multiple Projects in a row, only the last one is needed

Best order of operation

  1. Selects
  1. Cartesian Product
  1. Join
  1. Project

Part 3 Basics of Cost Estimation

What are the main factors?

Quick facts just to note

Attributes to note

R is a table

Part 4 Select (linear search)

Cost:

Worst - nBlocks(R)

Avg - 0.5 nBlocks(R)

Cost PK:

equality - nLevels a(I) + 1

inequality - nLevels a(I) + nBlocks(R)/2

Secondary index equality - nLevels a(I) + SC a(R) / bFactor(R)

Part 5 Join

Cost:

Block nested loop join - nBlocks(R) + nBlocks(R) * nBlocks(S)

Index nested join - nBlocks(R) + nRows(R) * (nLevels a(I) + 1)

Part 6 Projection

Cost:

read - nBlocks(R)

remove duplicates - nBlocks(R) * log2( nBlocks(R) )

total - nBlocks(R) + nBlocks(R) * log2( nBlocks(R) )