Docsity

Databases Design - Lecture Slides | CMSC 424, Study notes of Principles of Database Management

Toggle sidebar
bg1
11/4/08
1
CMSC424: Database
Design
Instructor: Amol Deshpande
 Data$Models$
 Conceptual$representa1on$of$the$data$
 Data$Retrieval$
 How$to$ask$ques1ons$of$the$database$
 How$to$answer$those$ques1ons$
 Data$Storage$
 How/where$to$store$data,$how$to$access$it$
 Data$Integrity$
 Manage$crashes,$concurrency$
 Manage$seman1c$inconsistencies$
Databases
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Databases Design - Lecture Slides | CMSC 424 and more Study notes Principles of Database Management in PDF only on Docsity!

CMSC424: Database

Design

Instructor: Amol Deshpande

[email protected]

 Data Models

 Conceptual representa1on of the data

 Data Retrieval

 How to ask ques1ons of the database

 How to answer those ques1ons

 Data Storage

 How/where to store data, how to access it

 Data Integrity

 Manage crashes, concurrency

 Manage seman1c inconsistencies

Databases

Query Processing

 Overview

 Selection operation

 Join operators

 Sorting

 Other operators

 Putting it all together…

Overview

User select * from R, S where … R, B+Tree on R.a S, Hash Index on S.a … Results

Query Parser

Resolve the references, Syntax errors etc. Converts the query to an internal format relational algebra like

Query Optimizer

Find the best way to evaluate the query Which index to use? What join method to use? …

Query Processor

Read the data from the files Do the query processing joins, selections, aggregates …

Selection Operation

 select * from person where SSN = “123”
 Option 1: Sequential Scan

 Read the relation start to end and look for “123”  Can always be used (not true for the other options)  Cost?  Let br = Number of relation blocks  Then:  1 seek and br block transfers  So:  tS + br * tT secImprovements:If SSN is a key, then can stop when foundSo on average, br/2 blocks accessed

Selection Operation

 select * from person where SSN = “123”
 Option 2 : Binary Search:

 Pre-condition:  The relation is sorted on SSNSelection condition is an equality  E.g. can’t apply to “ Name like ‘%424%’”  Do binary search  Cost of finding the first tuple that matches  log 2 ( br)  * ( tT + tS )  All I/Os are random, so need a seek for all  The last few are closeby, but we ignore such small effects  Not quite: What if 10000 tuples match the condition?  Incurs additional cost

Selection Operation

 select * from person where SSN = “123”
 Option 3 : Use Index
 Pre-condition:
 An appropriate index must exist
 Use the index
 Find the first leaf page that contains the search key

 Retrieve all the tuples that match by following the pointers  If primary index, the relation is sorted by the search key  Go to the relation and read blocks sequentially  If secondary index, must follow all pointers using the index

Selection w/ B+-Tree Indexes

n * (tT + tS) n = number of records that match This can be bad secondary index, not a hi * (tT + tS) key, equality secondary index, hi * (tT + tS) 1 * (tT + tS) candidate key, equality 1 * (tT + tS) + (b – 1) * tT Note: primary == sorted b = number of pages that contain the matches primary index, not a key, hi * (tT + tS) equality primary index, candidate hi * (tT + tS) 1 * (tT + tS) key, equality

cost of retrieving

the tuples

cost of finding the

first leaf

hi = height of the index

Selection Operation

 Complex selections  Conjunctive: select * from accounts where balance > 100000 and SSN = “123”  Disjunctive: select * from accounts where balance > 100000 or SSN = “123”  Option 4: Conjunction or disjunction of record identifiers  Use indexes to find all RIDs that match each of the conditions  Do an intersection (for conjunction) or a union (for disjunction)  Sort the records and fetch them in one shot  Called “Index-ANDing” or “Index-ORing”  Heavily used in commercial systems

Query Processing

 Overview

 Selection operation

 Join operators

 Sorting

 Other operators

 Putting it all together…

Join

 select * from R, S where R.a = S.a

 Called an “ equi-join”

 select * from R, S where |R.a – S.a | < 0.

Not an “equi-join”

 Option 1: Nested-loops
for each tuple r in R
for each tuple s in S
check if r.a = s.a (or whether |r.a – s.a| < 0.5)
 Can be used for any join condition

 As opposed to some algorithms we will see later

 R called outer relation
 S called inner relation

Nested-loops Join

 Cost? Depends on the actual values of parameters, especially memory
 br, bs  Number of blocks of R and S
 nr, ns  Number of tuples of R and S
 Case 1: Minimum memory required = 3 blocks

 One to hold the current R block, one for current S block, one for the result being produced  Blocks transferred:  Must scan R tuples once: br  For each R tuple, must scan S: nr * bs  Seeks?  nr + br

Block Nested-loops Join

 Simple modification to “nested-loops join”

 Block at a time for each block Br in R for each block Bs in S for each tuple r in Br for each tuple s in Bs check if r.a = s.a (or whether |r.a – s.a| < 0.5)

 Case 1: Minimum memory required = 3 blocks

 Blocks transferred: br * bs + br  Seeks: 2 * br

 For the example:

 blocks: 40400, seeks: 800

Block Nested-loops Join

 Case 1: Minimum memory required = 3 blocks

 Blocks transferred: br * bs + br
 Seeks: 2 * br

 Case 2: S fits in memory

 Blocks transferred: bs + br
 Seeks: 2

 What about in between?

 Say there are 50 blocks, but S is 100 blocks
 Why not use all the memory that we can…

Block Nested-loops Join

 Case 3: 50 blocks (S = 100 blocks)?
for each group of 48 blocks in R
for each block Bs in S
for each tuple r in the group of 48 blocks
for each tuple s in Bs
check if r.a = s.a (or whether |r.a – s.a| < 0.5)
 Why is this good?

 We only have to read S a total of br/48 times (instead of br times)  Blocks transferred: br * bs / 48 + br  Seeks: 2 * br / 48

Index Nested-loops Join

 select * from R, S where R.a = S.a
 Called an “ equi-join”
 Nested-loops
for each tuple r in R
for each tuple s in S
check if r.a = s.a (or whether |r.a – s.a| < 0.5)
 Suppose there is an index on S.a
 Why not use the index instead of the inner loop?
for each tuple r in R
use the index to find S tuples with S.a = r.a

So far…

 Block Nested-loops join

 Can always be applied irrespective of the join condition  If the smaller relation fits in memory, then cost:  br + bs  This is the best we can hope if we have to read the relations once each  CPU cost of the inner loop is high  Typically used when the smaller relation is really small (few tuples) and index nested-loops can’t be used

 Index Nested-loops join

 Only applies if an appropriate index exists  Very useful when we have selections that return small number of tuples  select balance from customer, accounts where customer.name = “j. s.” and customer.SSN = accounts.SSN

Hash Join

 Case 1: Smaller relation (S) fits in memory
 Nested-loops join:
for each tuple r in R
for each tuple s in S
check if r.a = s.a
 Cost: br + bs transfers, 2 seeks
 The inner loop is not exactly cheap (high CPU cost)
 Hash join:
read S in memory and build a hash index on it
for each tuple r in R
use the hash index on S to find tuples such that S.a = r.a

Hash Join

 Case 1: Smaller relation (S) fits in memory
 Hash join:
read S in memory and build a hash index on it
for each tuple r in R
use the hash index on S to find tuples such that S.a = r.a
 Cost: br + bs transfers, 2 seeks (unchanged)
 Why good?

 CPU cost is much better (even though we don’t care about it too much)  Performs much better than nested-loops join when S doesn’t fit in memory (next)

Hash Join

 Case 2: Smaller relation (S) doesn’t fit in memory
 Two “phases”
 Phase 1:

 Read the relation R block by block and partition it using a hash function, h1(a)  Create one partition for each possible value of h1(a)  Write the partitions to disk  R gets partitioned into R1, R2, …, Rk  Similarly, read and partition S, and write partitions S1, S2, …, Sk to disk  Only requirement:  Each S partition fits in memory

Hash Join

Hash Join: Issues

 How to guarantee that the partitions of S all fit in memory?

 Say S = 10000 blocks, Memory = M = 100 blocks  Use a hash function that hashes to 100 different values?  Eg. h1(a) = a % 100?  Problem: Impossible to guarantee uniform split  Some partitions will be larger than 100 blocks, some will be smaller  Use a hash function that hashes to 100*f different values  f is called fudge factor, typically around 1.  So we may consider h1(a) = a % 120.  This is okay IF a is uniformly distributed

Hash Join: Issues

 Memory required?  Say S = 10000 blocks, Memory = M = 100 blocks  So 120 different partitions  During phase 1:  Need 1 block for storing R  Need 120 blocks for storing each partition of R  So must have at least 121 blocks of memory  We only have 100 blocks  Typically need SQRT(|S| * f) blocks of memory  So if S is 10000 blocks, and f = 1.2, need 110 blocks of memory  If memory = 10000 blocks = 10000 * 4 KB = 40MB (not unreasonable)  Then, S can be as large as 10000*10000/1.2 blocks = 333 GB

Hash Join: Issues

 What if we don’t have enough memory?  Recursive Partitioning  Rarely used, but can be done  What if the hash function turns out to be bad?  We used h1(a) = a % 100  Turns out all values of a are multiple of 100  So h1(a) is always = 0  Called hash-table overflow  Overflow avoidance: Use a good hash function  Overflow resolution: Repartition using a different hash function

So far…

 Block Nested-loops join

 Can always be applied irrespective of the join condition

 Index Nested-loops join

 Only applies if an appropriate index exists  Very useful when we have selections that return small number of tuples  select balance from customer, accounts where customer.name = “j. s.” and customer.SSN = accounts.SSN

 Hash joins

 Join algorithm of choice when the relations are large  Only applies to equi-joins (since it is hash-based)

 Hybrid hash join

 An optimization on hash join that is always implemented

Merge-Join (Sort-merge join)

 Pre-condition:
 The relations must be sorted by the join attribute
 If not sorted, can sort first, and then use this algorithms
 Called “sort-merge join” sometimes
select *
from r, s
where r.a1 = s.a

Step:

_1. Compare the tuples at pr and ps

  1. Move pointers down the list_ _- Depending on the join condition
  2. Repeat_

Merge-Join (Sort-merge join)

 Cost:
 If the relations sorted, then just

 br + bs block transfers, some seeks depending on memory size

 What if not sorted?

 Then sort the relations first  In many cases, still very good performance  Typically comparable to hash join

 Observation:
 The final join result will also be sorted on a
 This might make further operations easier to do

 E.g. duplicate elimination

Joins: Summary

 Block Nested-loops join

 Can always be applied irrespective of the join condition

 Index Nested-loops join

 Only applies if an appropriate index exists

 Hash joins – only for equi-joins

 Join algorithm of choice when the relations are large

 Hybrid hash join

 An optimization on hash join that is always implemented

 Sort-merge join

 Very commonly used – especially since relations are typically sorted  Sorted results commonly desired at the output  To answer group by queries, for duplicate elimination, because of ASC/DSC