Introduction To Database Systems CSE 344: Basic Query Evaluation and Indexes
Introduction To Database Systems CSE 344: Basic Query Evaluation and Indexes
CSE 344
Lecture 6:
Basic Query Evaluation and Indexes
Query Execution
Return Results
4
Example
Student Takes Course
ID
fName
lName
studentID
courseID
courseID
name
SELECT
*
FROM
Student
x,
Takes
y
WHERE
x.ID=y.studentID
AND
y.courseID
>
300
Nested-‐
for
y
in
Takes
loop
join
if
courseID
>
300
then
for
x
in
Student
if
x.ID=y.studentID
output
*
Merge join
create
a
hash-‐table
for
x
in
Student
insert
x
in
the
hash-‐table
on
x.ID
• Hash join for
y
in
Takes
if
courseID
>
300
then
probe
y.courseID
in
hash-‐table
if
match
found
and
addi[onal
condi[ons
then
return
match
9
Discussion
Which plan is best? Choose one: for
y
in
Takes
if
courseID
>
300
then
• Nested loop join: O(N2)
for
y
in
Takes
if
x.ID=y.studentID
– Could be O(N)
output
*
10
Student
ID fName lName
220
block 3
a set of tuples 240
420
800
ID fName lName
Example 1: ID fName
10
Tom
lName
Hanks
Index on ID 20
Amy
Hanks
10 Tom Hanks
10
20 20 Amy Hanks
50
50 … …
200
200 …
220
240 220
420
240
800
420
950
… 800
Example 2: ID fName
10
Tom
lName
Hanks
Index on fName 20
Amy
Hanks
Index Student_fName
on Student.fName Data File Student …
10 Tom Hanks
Amy
Ann 20 Amy Hanks
Bob
50 … …
Cho
200 …
…
… 220
…
240
…
420
…
… 800
Tom
80
40 ≤ 80
20 < 40 ≤ 60
10 15 18 20 30 40 50 60 65 80 85 90
30 < 40 ≤ 40
10 15 18 20 30 40 50 60 65 80 85 90
CSE 344 - Winter 2013 18
Clustered vs Unclustered
B+ Tree B+ Tree
Data entries
Data entries
(Index File)
(Data file)
Index selec[on
ID fName lName
ID fName lName
What indexes ?
CSE 344 - Winter 2013 27
The Index Selection Problem 1
V(M, N, P);
What indexes ?
CSE 344 - Winter 2013 29
The Index Selection Problem 2
V(M, N, P);
What indexes ?
CSE 344 - Winter 2013 31
The Index Selection Problem 3
V(M, N, P);
A: V(N, P)
CSE 344 - Winter 2013 32
The Index Selection Problem 4
V(M, N, P);
What indexes ?
CSE 344 - Winter 2013 33
The Index Selection Problem 4
V(M, N, P);
0 100
Percentage tuples retrieved
CSE 344 - Winter 2013 38