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 20160502 Slide 1 of 67
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 20160502 Slide 1 of 67 Overview Question: How is a
Query Processing 20160502 Slide 1 of 67
Query Processing 20160502 Slide 2 of 67
Query Processing 20160502 Slide 3 of 67
Query Processing 20160502 Slide 4 of 67
Query Processing 20160502 Slide 5 of 67
Query Processing 20160502 Slide 5 of 67
Query Processing 20160502 Slide 6 of 67
Query Processing 20160502 Slide 7 of 67
Query Processing 20160502 Slide 8 of 67
Query Processing 20160502 Slide 9 of 67
Query Processing 20160502 Slide 10 of 67
Query Processing 20160502 Slide 11 of 67
Query Processing 20160502 Slide 12 of 67
Query Processing 20160502 Slide 13 of 67
Query Processing 20160502 Slide 14 of 67
j∈J θj.
Query Processing 20160502 Slide 15 of 67
Query Processing 20160502 Slide 16 of 67
Query Processing 20160502 Slide 17 of 67
Query Processing 20160502 Slide 18 of 67
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 20160502 Slide 19 of 67
Query Processing 20160502 Slide 20 of 67
Query Processing 20160502 Slide 21 of 67
Query Processing 20160502 Slide 22 of 67
Query Processing 20160502 Slide 23 of 67
Query Processing 20160502 Slide 24 of 67
Query Processing 20160502 Slide 25 of 67
Query Processing 20160502 Slide 26 of 67
Query Processing 20160502 Slide 27 of 67
Query Processing 20160502 Slide 28 of 67
Query Processing 20160502 Slide 29 of 67
Query Processing 20160502 Slide 30 of 67
Query Processing 20160502 Slide 31 of 67
Query Processing 20160502 Slide 32 of 67
Query Processing 20160502 Slide 33 of 67
20160502 Slide 34 of 67
Query Processing 20160502 Slide 35 of 67
Query Processing 20160502 Slide 36 of 67
Query Processing 20160502 Slide 37 of 67
Query Processing 20160502 Slide 38 of 67
Query Processing 20160502 Slide 39 of 67
Query Processing 20160502 Slide 40 of 67
Query Processing 20160502 Slide 41 of 67
SELECT dept_name , avg(salary) FROM instructor GROUP BY dept_name;
Query Processing 20160502 Slide 42 of 67
Query Processing 20160502 Slide 43 of 67
πname
department instructor
Query Processing 20160502 Slide 44 of 67
department instructor
Query Processing 20160502 Slide 45 of 67
πname
department instructor
Query Processing 20160502 Slide 46 of 67
Query Processing 20160502 Slide 47 of 67
Query Processing 20160502 Slide 48 of 67
SELECT name FROM department NATURAL JOIN instructor WHERE building=’MIT -huset ’;
instructor
πname
department instructor
Query Processing 20160502 Slide 49 of 67
instructor
πname
department instructor
Query Processing 20160502 Slide 50 of 67
πname
department instructor
πname
department
πname,dept name
instructor
Query Processing 20160502 Slide 51 of 67
πname
department instructor
πname πname,dept name
σbuilding=’MIT-huset’
department
Query Processing 20160502 Slide 52 of 67
πname πname,dept name
σbuilding=’MIT-huset’
department
πname πname,dept name
instructor
σbuilding=’MIT-huset’
department
Query Processing 20160502 Slide 53 of 67
πname πname,dept name
instructor
σbuilding=’MIT-huset’
department
πname
department
πname,dept name
instructor
Query Processing 20160502 Slide 54 of 67
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 20160502 Slide 55 of 67
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 20160502 Slide 56 of 67
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 20160502 Slide 57 of 67
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 20160502 Slide 58 of 67
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 20160502 Slide 59 of 67
Query Processing 20160502 Slide 60 of 67
Query Processing 20160502 Slide 61 of 67
Query Processing 20160502 Slide 62 of 67
Query Processing 20160502 Slide 63 of 67
Query Processing 20160502 Slide 64 of 67
Query Processing 20160502 Slide 65 of 67
Query Processing 20160502 Slide 66 of 67
Query Processing 20160502 Slide 67 of 67