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:
- Selects next to each other can merge
- Select can commute with Project
- Select can commute with other Selects
- Theta Join and Cartesian Product and Select and project can commute
- When multiple Projects in a row, only the last one is needed
Best order of operation
- Selects
- Cartesian Product
- Join
- Project
Part 3 Basics of Cost Estimation
What are the main factors?
- Disk IO
- Computation cost
- Memory usage
- Communication costs over a distributed system
Quick facts just to note
- Calculations are done for blocks
- tuples - row
- table - relation
Attributes to note
R is a table
- nRows(R) - number of rows
- bFactor(R) - number of rows in a block
- nBlocks(R) - number of blocks needed to store R
- nDistinct a(R) - number of rows that have a certain value ‘a’
- SC a(R) - number of rows that satisfy the condition ‘a’
- nLevels a(I) - number of levels of a multi-level index on a
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) )