Query Processing
5DV120 — Database System Principles Ume˚ a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner
Query Processing 20130530 Slide 1 of 62
Query Processing 5DV120 Database System Principles Ume a - - PowerPoint PPT Presentation
Query Processing 5DV120 Database System Principles Ume a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner Query Processing 20130530 Slide 1 of 62 Overview Question: How is a
Query Processing 20130530 Slide 1 of 62
Query Processing 20130530 Slide 2 of 62
Query Processing 20130530 Slide 3 of 62
Query Processing 20130530 Slide 4 of 62
Query Processing 20130530 Slide 5 of 62
Query Processing 20130530 Slide 5 of 62
Query Processing 20130530 Slide 6 of 62
Query Processing 20130530 Slide 7 of 62
Query Processing 20130530 Slide 8 of 62
Query Processing 20130530 Slide 9 of 62
Query Processing 20130530 Slide 10 of 62
Query Processing 20130530 Slide 11 of 62
Query Processing 20130530 Slide 12 of 62
Query Processing 20130530 Slide 13 of 62
Query Processing 20130530 Slide 14 of 62
j∈J θj.
Query Processing 20130530 Slide 15 of 62
Query Processing 20130530 Slide 16 of 62
Query Processing 20130530 Slide 17 of 62
Query Processing 20130530 Slide 18 of 62
12 35 68 19 44 88 12 35 68 19 44 88 12 12 35 68 19 44 88 12 12 35 68 19 44 88 12 19 12 35 68 19 44 88 12 19 12 35 68 19 44 88 12 19 35 12 35 68 19 44 88 12 19 35 12 35 68 19 44 88 12 19 35 44 12 35 68 19 44 88 12 19 35 44 12 35 68 19 44 88 12 19 35 44 68 12 35 68 19 44 88 12 19 35 44 68 12 35 68 19 44 88 12 19 35 44 68 88 12 35 68 19 44 88 12 19 35 44 68 88
Query Processing 20130530 Slide 19 of 62
Query Processing 20130530 Slide 20 of 62
Query Processing 20130530 Slide 21 of 62
Query Processing 20130530 Slide 22 of 62
Query Processing 20130530 Slide 23 of 62
Query Processing 20130530 Slide 24 of 62
Query Processing 20130530 Slide 25 of 62
Query Processing 20130530 Slide 26 of 62
Query Processing 20130530 Slide 27 of 62
Query Processing 20130530 Slide 28 of 62
Query Processing 20130530 Slide 29 of 62
Query Processing 20130530 Slide 30 of 62
Query Processing 20130530 Slide 31 of 62
Query Processing 20130530 Slide 32 of 62
Query Processing 20130530 Slide 33 of 62
Query Processing 20130530 Slide 34 of 62
Query Processing 20130530 Slide 35 of 62
Query Processing 20130530 Slide 36 of 62
SELECT dept_name , avg(salary) FROM instructor GROUP BY dept_name;
Query Processing 20130530 Slide 37 of 62
Query Processing 20130530 Slide 38 of 62
πname
department instructor
Query Processing 20130530 Slide 39 of 62
department instructor
Query Processing 20130530 Slide 40 of 62
πname
department instructor
Query Processing 20130530 Slide 41 of 62
Query Processing 20130530 Slide 42 of 62
Query Processing 20130530 Slide 43 of 62
SELECT name FROM department NATURAL JOIN instructor WHERE building=’MIT -huset ’;
instructor
πname
department instructor
Query Processing 20130530 Slide 44 of 62
instructor
πname
department instructor
Query Processing 20130530 Slide 45 of 62
πname
department instructor
πname
department
πname,dept name
instructor
Query Processing 20130530 Slide 46 of 62
πname
department instructor
πname πname,dept name
σbuilding=’MIT-huset’
department
Query Processing 20130530 Slide 47 of 62
πname πname,dept name
σbuilding=’MIT-huset’
department
πname πname,dept name
instructor
σbuilding=’MIT-huset’
department
Query Processing 20130530 Slide 48 of 62
πname πname,dept name
instructor
σbuilding=’MIT-huset’
department
πname
department
πname,dept name
instructor
Query Processing 20130530 Slide 49 of 62
SELECT name SELECT name FROM instructor FROM instructor WHERE (dept_name = ’Comp.Sci.’) WHERE (dept_name = ’Comp.Sci.’) INTERSECT AND (salary >70000); SELECT name FROM instructor WHERE (salary >70000);
instructor
∩ πname σ(dept name=Comp. Sci.)
instructor
πname σ(salary>70000)
instructor
Query Processing 20130530 Slide 50 of 62
university =# EXPLAIN SELECT name FROM instructor WHERE (dept_name = ’Comp.Sci.’) AND (salary >70000);
QUERY PLAN
Seq Scan on instructor (cost =0.00..16.60 rows =1 width =58) Filter: (( salary > 70000:: numeric) AND (( dept_name ):: text = ’Comp.Sci.’:: text )) (2 rows)
Query Processing 20130530 Slide 51 of 62
university =# EXPLAIN SELECT name FROM instructor WHERE (dept_name = ’Comp.Sci.’) INTERSECT SELECT name FROM instructor WHERE (salary >70000);
QUERY PLAN
HashSetOp Intersect (cost =0.00..32.86 rows =1 width =58)
Append (cost =0.00..32.49 rows =149 width =58)
Subquery Scan on "*SELECT*1" (cost =0.00..15.52 rows =2 width =58)
Seq Scan on instructor (cost =0.00..15.50 rows =2 width =58) Filter: (( dept_name ):: text = ’Comp.Sci.’:: text)
Subquery Scan on "*SELECT*2" (cost =0.00..16.97 rows =147 width =58)
Seq Scan on instructor (cost =0.00..15.50 rows =147 width =58) Filter: (salary > 70000:: numeric) (8 rows)
Query Processing 20130530 Slide 52 of 62
university =# EXPLAIN SELECT name FROM department NATURAL JOIN instructor WHERE building=’Watson ’ AND salary >70000;
QUERY PLAN
Hash Join (cost =16.79..32.85 rows =1 width =58) (actual time =0.055..0.069 rows =3 loops =1) Output: instructor.name Hash Cond: (( instructor .dept_name ):: text = (department .dept_name ):: text)
Seq Scan on instructor (cost =0.00..15.50 rows =147 width =116) (actual time =0.018..0.027 rows =8 loops =1) Output: instructor.id , instructor .name , instructor .dept_name , instructor .salary Filter: (salary > 70000:: numeric)
Hash (cost =16.75..16.75 rows =3 width =58) (actual time =0.018..0.018 rows =2 loops =1) Output: department.dept_name
Seq Scan on department (cost =0.00..16.75 rows =3 width =58) (actual time =0.006..0.011 rows =2 loops =1) Output: department.dept_name Filter: (( building ):: text = ’Watson ’:: text) Total runtime: 0.123 ms (12 rows) Query Processing 20130530 Slide 53 of 62
university =# EXPLAIN SELECT name FROM department NATURAL JOIN instructor WHERE building=’Watson ’ INTERSECT SELECT name FROM department NATURAL JOIN instructor WHERE salary >70000;
HashSetOp Intersect (cost =16.79..74.39 rows =2 width =58)
Append (cost =16.79..74.02 rows =149 width =58)
Subquery Scan "*SELECT*1" (cost =16.79..32.88 rows =2 width =58)
Hash Join (cost =16.79..32.86 rows =2 width =58) Hash Cond: (( public.instructor .dept_name ):: text = (public. department .dept_name ):: text)
Seq Scan on instructor (cost =0.00..14.40 rows =440 width =116)
Hash (cost =16.75..16.75 rows =3 width =58)
Seq Scan on department (cost =0.00..16.75 rows =3 width =58) Filter: (( building ):: text = ’Watson ’:: text)
Subquery Scan "*SELECT*2" (cost =22.15..41.14 rows =147 width =58)
Hash Join (cost =22.15..39.67 rows =147 width =58) Hash Cond: (( public.instructor .dept_name ):: text = (public. department .dept_name ):: text)
Seq Scan on instructor (cost =0.00..15.50 rows =147 width =116) Filter: (salary > 70000:: numeric)
Hash (cost =15.40..15.40 rows =540 width =58)
Seq Scan on department (cost =0.00..15.40 rows =540 width =58) (16 rows)
Query Processing 20130530 Slide 54 of 62
Query Processing 20130530 Slide 55 of 62
Query Processing 20130530 Slide 56 of 62
Query Processing 20130530 Slide 57 of 62
Query Processing 20130530 Slide 58 of 62
Query Processing 20130530 Slide 59 of 62
Query Processing 20130530 Slide 60 of 62
Query Processing 20130530 Slide 61 of 62
Query Processing 20130530 Slide 62 of 62