LAB: Web-scale Data Management on a Cloud Lab 11. Query Execution Plan 2011/05/27
Announcement HOMEWORK #4 –Due: 6/9 Thur. (2 weeks left) FINAL EXAM –6/14 Tues. 17:00 PROJECT #1 –Due: 6/17 Fri.
movieLens Database 1,000,209 3,883 6,040 idgenderageoccup ation zipcode 1F M M …………… idtitlegenres 1Toy StoryAnimation|Child ren’s|Comedy 21Waiting to Exhale Comedy|Drama 104Get ShortyAction|Comedy| Drama ………. useridmovieidratingts (timestamp) …………
Table Access Full mysql> EXPLAIN SELECT * FROM users; | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 5891 | | row in set (0.00 sec) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.02 sec) users
Index Full Scan mysql> EXPLAIN SELECT * FROM users ORDER BY id; | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | SIMPLE | users | index | NULL | PRIMARY | 4 | NULL | 5891 | | row in set (0.00 sec) (id column is omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.03 sec) users
Index Unique Scan mysql> EXPLAIN SELECT * FROM users WHERE id = 300; | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 | | row in set (0.00 sec) (id column is omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.03 sec) users
Index Range Scan mysql> EXPLAIN SELECT * FROM users WHERE id BETWEEN 100 AND 1000; | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | SIMPLE | users | range | PRIMARY | PRIMARY | 4 | NULL | 1728 | Using | | where | row in set (0.00 sec) (id column is omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.03 sec) users
Index Fast Full Scan mysql> EXPLAIN SELECT id FROM users; | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | SIMPLE | users | index | NULL | PRIMARY | 4 | NULL | 5891 | Using | | index | row in set (0.00 sec) (id column is omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.02 sec) users
Join mysql> EXPLAIN SELECT * FROM users, ratings WHERE users.id = ratings.userid; | select_type | table | type | possible_keys | key | key_len | ref | rows | | SIMPLE | users | ALL | PRIMARY | NULL | NULL | NULL | 5915 | | SIMPLE | ratings | ref | PRIMARY | PRIMARY | 4 | users.id | 61 | rows in set (0.00 sec) (id, extra columns are omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.05 sec) UsersRatings
3-way Join mysql> EXPLAIN SELECT * FROM users, ratings, movies -> WHERE users.id = ratings.userid AND ratings.movieid = movies.id; | select_type | table | type | possible_keys | key | key_len | ref | rows | | SIMPLE | users | ALL | PRIMARY | NULL | NULL | NULL | 5915 | | SIMPLE | ratings | ref | PRIMARY, | PRIMARY | 4 | users.id | 61 | | fk_ratings_movies | | SIMPLE | movies | eq_ref | PRIMARY | PRIMARY | 4 | ratings.movieid | 1 | rows in set (0.00 sec) (id, extra columns are omitted) mysql> SHOW SESSION STATUS LIKE 'Last_query_cost'; | Variable_name | Value | | Last_query_cost | | row in set (0.00 sec) MoviesRatings Users
Oracle with SQL*Plus Oracle Database 10g Express Edition –with SQL*Plus $ sqlplus SQL*Plus: Release Production on Fri May 27 00:04: Copyright (c) 1982, 2005, Oracle. All rights reserved. Enter user-name: student Enter password: csed421 Connected to: Oracle Database 10g Express Edition Release – Production SQL> _
Autotrace in SQL*Plus To get a report on the execution path used by the SQL optimizer and the statement execution statistics – ≒ EXPLAIN in MySQL SQL> set autotrace on –Query result + Execution plan + Statistics SQL> set autotrace traceonly –Execution plan + Statistics ( + Query execution in background) SQL> set autotrace traceonly explain –Execution plan SQL> set autotrace off
Execution Plan CategoriesSubjects Classes
Demos Scan –Table Access Full –Index Full Scan –Index Unique Scan –Index Range Scan –Index Fast Full Scan Join –Hash Join –Nested Loop Join –Sort Merge Join –3-way Join
Table Access Full Users
Index Full Scan Users
Index Unique Scan Users
Index Range Scan Users
Index Fast Full Scan Users
Hash Join Cost: α = 921 UsersRatings
Nested Loop Join Cost: 1000K * α = 1002K UsersRatings
Sort Merge Join Cost: α = 5969 UsersRatings
3-way Join MoviesRatings Users
3-way Outer Join UsersRatings Movies
Internal Sort
External Sort
Analyze Statistics of Table SQL> analyze table table_name compute statistics; SQL> analyze index index_name validate structure;