Use SQL EXPECTED OUTPUT(one table)
Posted: Thu May 05, 2022 12:52 pm
Use SQL
EXPECTED OUTPUT(one table)
7.9 LAB-Query execution plans (Sakila) This lab illustrates how minor changes in a query may have a significant impact on the execution plan. MySQL Workbench exercise Refer to the film, actor, and film_actor tables of the Sakila database. This exercise is based on the initial Sakila installation. If you have altered these tables or their data, your results may be different. Do the following in MySQL Workbench: 1. Enter the following statements: USE sakila; SELECT last_name, first_name, ROUND (AVG (length), 0) AS average FROM actor INNER JOIN film_actor ON film_actor.actor_id = actor.actor_id INNER JOIN film ON film_actor.film_id = film.film_id WHERE title = "ALONE TRIP" GROUP BY last_name, first_name ORDER BY average; 2. Highlight the SELECT query. 3. In the main menu, select Query > Explain Current Statement. 4. In the Display Info box, highlighted in red below, select Data Read per Join.
Workbench displays the following execution plan: ●●● MySQL Workbench Local instance 3306 Q 2 Administration Schemas Query 3 SCHEMAS BZIRO Limit to 2000 rows 8 1. USE sakila; Q Filter objects vsakila 2 3. Tables SELECT last_name, first_name, ROUND (AVG (length), 0) AS average FROM actor 4 > 5 INNER JOIN film_actor ON film_actor.actor_id = actor.actor_id 6 INNER JOIN film ON film_actor.film_id = film.film_id 7 WHERE title = "ALONE TRIP" 8 GROUP BY last_name, first_name 9 ORDER BY average; 1:3 O actor address category > city > country > customer > film > film_actor > film_catego... > film_text > inventory > language > payment > > 13 Session 100% Visual Explain Display Info: Data Read per Join 3 5 rows Overview: 6 GROUP tmp table 5 5 rows Object Infor Schema: sakila nested loop 1 4 1 row 5 rows 1 row 2 Non-Unique Key Lookup Non-Unique Key Lookup Unique Key Lookup film idx_title film actor idx fk film id actor PRIMARY Explain Apply Revert The execution plan depicts the result of EXPLAIN for the SELECT query. The execution plan has seven steps, corresponding to the red numbers on the screenshot: View Source: nested loop Query cost: 3.07 query_block #1 71 ORDER Result Execution Plan filesort
The execution plan depicts the result of EXPLAIN for the SELECT query. The execution plan has seven steps, corresponding to the red numbers on the screenshot: 1. Access a single film row using the idx_title index on the title column. 2. Access matching film_actor rows using the idx_fk_film_id index on the film_id foreign key. 3. Join the results using the nested loop algorithm. 4. Access actor rows via the index on the primary key. 5. Join actor rows with the prior join result using the nested loop algorithm. 6. Store the result in a temporary table and compute the aggregate function. 7. Sort and generate the result table. Refer to MySQL nested loop documentation for an explanation of the nested loop algorithm. Now, replace = in the WHERE clause with < and generate a new execution plan. Step 1 of the execution plan says Index Range Scan. The index scan accesses all films with titles preceding "ALONE TRIP", rather than a single film. Finally, replace in the WHERE clause with > and generate a third execution plan. Step 1 of the execution plan says Full Table Scan and accesses actor rather than film. zyLab coding In the zyLab environment, write EXPLAIN statements for the three queries, in the order described above. Submit the EXPLAIN statements for testing. The zyLab execution plans do not exactly match the Workbench execution plans, since this lab uses a subset of film, actor, and film m_actor rows from the Sakila database. NOTE: In submit-mode tests that generate multiple result tables, the results are merged. Although the tests run correctly, the results appear in one table. 375490.1300766.qx3zqy7 LAB ACTIVITY 7.9.1: LAB-Query execution plans (Sakila) 0/10 Main.sql Load default template... 1 -- Your EXPLAIN statements go here 2
SQL File 5* film_actor film actor Z TRO 13 ✓ 1. SELECT SELECT * FROM sakila.actor; Result Grid Filter Rows: actor_id 1 2 ▶ IMT IN 7 8 9 10 11 12 first_name PENELOPE last_name GUINESS WAHLBERG NICK ED FD CHASE JENNIFER DAVIS DAVIS JOHNNY LOLLOBRIGIDA BETTE NICHOLSON GRACE MATTHEW MOSTEL JOHANSSON SWANK JOE CHRISTIAN GABLE ZERO CAGE KARL BERRY Limit to 1000 rows 2 ✔Q 1 P Edit: & Export/Import: Wrap Cell Content: A last update 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 === Result Grid Form Editor Ho Field Types
SQL File 5* film_actor film X 08 Z TR 1. SELECT * FROM sakila.film; Result Grid Filter Rows: ACADEMY DINOSAUR ACE GOLDFINGER ADAPTATION HOLES AFFAIR PREJUDICE AFRICAN EGG AGENT TRUMAN AIRPLANE SIERRA AIRPORT POLLOCK ALABAMA DEVIL ALADDIN CALENDAR ALAMO VIDEOTAPE film_id title 2 3 4 5 6 7 8 9 10 11 film 1 x Limit to 1000 rows Edit: ✓ 3 2 description A Epic Drama of a Feminist And a Mad Scientist... Astounding Epistle of a Database Administrat... Astounding Reflection of a Lumberjack And a ... A Fanciful Documentary of a Frisbee And a Lum... A Fast-paced Documentary of a Pastry Chef An... A Intrepid Panorama of a Robot And a Boy who... A Touching Saga of a Hunter And a Butler who ... A Epic Tale of a Moose And a Girl who must Con... A Thoughtful Panorama of a Database Administ... A Action-Packed Tale of a Man And a Lumberjac... A Boring Epistle of a Butler And a Cat who must... 11 Ey rental_duration NULL 6 Export/Import:Wrap Cell Content: IA | Fetch rows: release_year language_id original_language_id 2006 1 2006 1 2006 1 2006 1 2006 1 HULL 3 NULL NULL NULL HULL NULL 2006 1 2006 1 2006 1 NULL NULL 2006 1 2006 1 HULL NULL 2006 1 7 5 6 3 6 6 3 6 6 rental_rate length replacement_cos 0.99 86 20.99 48 50 4.99 12.99 2.99 18.99 2.99 117 26.99 2.99 130 22.99 2.99 169 17.99 4.99 62 28.99 4.99 54 15.99 2.99 114 21.99 4.99 63 24.99 0.99 16.99 126 Apply
SQL File 5* film_actor x film A 5 TQ 1. SELECT * FROM sakila.film_actor; Result Grid E Filter Rows: actor_id film_id last_update 1 1 23 1 25 1 106 1 140 1 166 1 277 1 361 1 438 1 499 1 506 1 509 film_actor 1 x 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 Limit to 1000 rows Edit: 4 Export/Import: Wrap Cell Content: A Fetch rows:
t id select_type table partitions type possible_keys key key_len 1 SIMPLE film actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL ALL PRIMARY NULL NULL 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 id select_type table partitions type possible_keys key key_len 1 SIMPLE film_actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL eq_ref PRIMARY PRIMARY 2 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 id select_type table partitions type possible_keys key key_len 1 SIMPLE film_actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL eq_ref PRIMARY PRIMARY 2 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 ref rows filtered Extra Using index; Using 169 100.00 temporary; Using filesort Using where; Using join 30 3.33 buffer (Block Nested Loop) 1 100.00 NULL rows filtered Extral Using index; Using 169 100.00 temporary; Using filesort Using 33.33 where 100.00 NULL rows filtered Extra Using index; Using 169 100.00 temporary; Using filesort Using 33.33 where 100.00 NULL NULL NULL test.film_actor.actor_id ref NULL test.film_actor.film_id 1 test.film_actor.actor_id 1 ref NULL test.film_actor.film_id 1 test.film_actor.actor_id 1
EXPECTED OUTPUT(one table)
7.9 LAB-Query execution plans (Sakila) This lab illustrates how minor changes in a query may have a significant impact on the execution plan. MySQL Workbench exercise Refer to the film, actor, and film_actor tables of the Sakila database. This exercise is based on the initial Sakila installation. If you have altered these tables or their data, your results may be different. Do the following in MySQL Workbench: 1. Enter the following statements: USE sakila; SELECT last_name, first_name, ROUND (AVG (length), 0) AS average FROM actor INNER JOIN film_actor ON film_actor.actor_id = actor.actor_id INNER JOIN film ON film_actor.film_id = film.film_id WHERE title = "ALONE TRIP" GROUP BY last_name, first_name ORDER BY average; 2. Highlight the SELECT query. 3. In the main menu, select Query > Explain Current Statement. 4. In the Display Info box, highlighted in red below, select Data Read per Join.
Workbench displays the following execution plan: ●●● MySQL Workbench Local instance 3306 Q 2 Administration Schemas Query 3 SCHEMAS BZIRO Limit to 2000 rows 8 1. USE sakila; Q Filter objects vsakila 2 3. Tables SELECT last_name, first_name, ROUND (AVG (length), 0) AS average FROM actor 4 > 5 INNER JOIN film_actor ON film_actor.actor_id = actor.actor_id 6 INNER JOIN film ON film_actor.film_id = film.film_id 7 WHERE title = "ALONE TRIP" 8 GROUP BY last_name, first_name 9 ORDER BY average; 1:3 O actor address category > city > country > customer > film > film_actor > film_catego... > film_text > inventory > language > payment > > 13 Session 100% Visual Explain Display Info: Data Read per Join 3 5 rows Overview: 6 GROUP tmp table 5 5 rows Object Infor Schema: sakila nested loop 1 4 1 row 5 rows 1 row 2 Non-Unique Key Lookup Non-Unique Key Lookup Unique Key Lookup film idx_title film actor idx fk film id actor PRIMARY Explain Apply Revert The execution plan depicts the result of EXPLAIN for the SELECT query. The execution plan has seven steps, corresponding to the red numbers on the screenshot: View Source: nested loop Query cost: 3.07 query_block #1 71 ORDER Result Execution Plan filesort
The execution plan depicts the result of EXPLAIN for the SELECT query. The execution plan has seven steps, corresponding to the red numbers on the screenshot: 1. Access a single film row using the idx_title index on the title column. 2. Access matching film_actor rows using the idx_fk_film_id index on the film_id foreign key. 3. Join the results using the nested loop algorithm. 4. Access actor rows via the index on the primary key. 5. Join actor rows with the prior join result using the nested loop algorithm. 6. Store the result in a temporary table and compute the aggregate function. 7. Sort and generate the result table. Refer to MySQL nested loop documentation for an explanation of the nested loop algorithm. Now, replace = in the WHERE clause with < and generate a new execution plan. Step 1 of the execution plan says Index Range Scan. The index scan accesses all films with titles preceding "ALONE TRIP", rather than a single film. Finally, replace in the WHERE clause with > and generate a third execution plan. Step 1 of the execution plan says Full Table Scan and accesses actor rather than film. zyLab coding In the zyLab environment, write EXPLAIN statements for the three queries, in the order described above. Submit the EXPLAIN statements for testing. The zyLab execution plans do not exactly match the Workbench execution plans, since this lab uses a subset of film, actor, and film m_actor rows from the Sakila database. NOTE: In submit-mode tests that generate multiple result tables, the results are merged. Although the tests run correctly, the results appear in one table. 375490.1300766.qx3zqy7 LAB ACTIVITY 7.9.1: LAB-Query execution plans (Sakila) 0/10 Main.sql Load default template... 1 -- Your EXPLAIN statements go here 2
SQL File 5* film_actor film actor Z TRO 13 ✓ 1. SELECT SELECT * FROM sakila.actor; Result Grid Filter Rows: actor_id 1 2 ▶ IMT IN 7 8 9 10 11 12 first_name PENELOPE last_name GUINESS WAHLBERG NICK ED FD CHASE JENNIFER DAVIS DAVIS JOHNNY LOLLOBRIGIDA BETTE NICHOLSON GRACE MATTHEW MOSTEL JOHANSSON SWANK JOE CHRISTIAN GABLE ZERO CAGE KARL BERRY Limit to 1000 rows 2 ✔Q 1 P Edit: & Export/Import: Wrap Cell Content: A last update 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 2006-02-15 04:34:33 === Result Grid Form Editor Ho Field Types
SQL File 5* film_actor film X 08 Z TR 1. SELECT * FROM sakila.film; Result Grid Filter Rows: ACADEMY DINOSAUR ACE GOLDFINGER ADAPTATION HOLES AFFAIR PREJUDICE AFRICAN EGG AGENT TRUMAN AIRPLANE SIERRA AIRPORT POLLOCK ALABAMA DEVIL ALADDIN CALENDAR ALAMO VIDEOTAPE film_id title 2 3 4 5 6 7 8 9 10 11 film 1 x Limit to 1000 rows Edit: ✓ 3 2 description A Epic Drama of a Feminist And a Mad Scientist... Astounding Epistle of a Database Administrat... Astounding Reflection of a Lumberjack And a ... A Fanciful Documentary of a Frisbee And a Lum... A Fast-paced Documentary of a Pastry Chef An... A Intrepid Panorama of a Robot And a Boy who... A Touching Saga of a Hunter And a Butler who ... A Epic Tale of a Moose And a Girl who must Con... A Thoughtful Panorama of a Database Administ... A Action-Packed Tale of a Man And a Lumberjac... A Boring Epistle of a Butler And a Cat who must... 11 Ey rental_duration NULL 6 Export/Import:Wrap Cell Content: IA | Fetch rows: release_year language_id original_language_id 2006 1 2006 1 2006 1 2006 1 2006 1 HULL 3 NULL NULL NULL HULL NULL 2006 1 2006 1 2006 1 NULL NULL 2006 1 2006 1 HULL NULL 2006 1 7 5 6 3 6 6 3 6 6 rental_rate length replacement_cos 0.99 86 20.99 48 50 4.99 12.99 2.99 18.99 2.99 117 26.99 2.99 130 22.99 2.99 169 17.99 4.99 62 28.99 4.99 54 15.99 2.99 114 21.99 4.99 63 24.99 0.99 16.99 126 Apply
SQL File 5* film_actor x film A 5 TQ 1. SELECT * FROM sakila.film_actor; Result Grid E Filter Rows: actor_id film_id last_update 1 1 23 1 25 1 106 1 140 1 166 1 277 1 361 1 438 1 499 1 506 1 509 film_actor 1 x 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 2006-02-15 05:05:03 Limit to 1000 rows Edit: 4 Export/Import: Wrap Cell Content: A Fetch rows:
t id select_type table partitions type possible_keys key key_len 1 SIMPLE film actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL ALL PRIMARY NULL NULL 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 id select_type table partitions type possible_keys key key_len 1 SIMPLE film_actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL eq_ref PRIMARY PRIMARY 2 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 id select_type table partitions type possible_keys key key_len 1 SIMPLE film_actor NULL index PRIMARY,idx_fk_film_id idx_fk_film_id 2 1 SIMPLE film NULL eq_ref PRIMARY PRIMARY 2 1 SIMPLE actor NULL eq_ref PRIMARY PRIMARY 2 ref rows filtered Extra Using index; Using 169 100.00 temporary; Using filesort Using where; Using join 30 3.33 buffer (Block Nested Loop) 1 100.00 NULL rows filtered Extral Using index; Using 169 100.00 temporary; Using filesort Using 33.33 where 100.00 NULL rows filtered Extra Using index; Using 169 100.00 temporary; Using filesort Using 33.33 where 100.00 NULL NULL NULL test.film_actor.actor_id ref NULL test.film_actor.film_id 1 test.film_actor.actor_id 1 ref NULL test.film_actor.film_id 1 test.film_actor.actor_id 1