Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views6 pages

Week05 Tutorial

lon

Uploaded by

Duy Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Week05 Tutorial

lon

Uploaded by

Duy Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorial: Analyzing Query Execution

Plans and Index Creation


Data file: CH11_SaleCo_MySQL.txt

1. Introduction
 Objective: Explain the importance of query execution plans and how they
help in optimizing database performance.
 Overview: Briefly introduce the main activities of the session: analyzing
execution plans, creating indexes, and optimizing queries.

2. Analyzing Query Execution Plans (20 minutes)


 Concepts:
o Query Execution Plan: A roadmap used by the DBMS to execute a
SQL query.
o Key Components: Type, Possible Keys, Key, Rows, Extra.
 Example:
o Query: SELECT * FROM QOPRODUCT WHERE P_PRICE < 20;
o Execution Plan: EXPLAIN SELECT * FROM QOPRODUCT WHERE P_PRICE
< 20;
o Explanation:
 Type: ALL (full table scan)
 Possible Keys: NULL (no index suggested)
 Key: NULL (no index used)
 Rows: High number (indicates inefficiency)
 Extra: Using where (filtering rows)
 Activity:
o Run provided SQL queries.
SELECT * FROM QOPRODUCT WHERE P_PRICE < 20;
SELECT V_NAME FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT P_DESCRIPT, P_PRICE FROM QOPRODUCT WHERE P_QOH
< 10;

o Use EXPLAIN to analyze execution plans. Use the EXPLAIN keyword


before each query:
EXPLAIN SELECT * FROM QOPRODUCT WHERE P_PRICE < 20;
EXPLAIN SELECT V_NAME FROM QOVENDOR WHERE V_STATE =
'TN';
EXPLAIN SELECT P_DESCRIPT, P_PRICE FROM QOPRODUCT
WHERE P_QOH < 10;
o Analyze Execution Plan: Examine key components:
 Type: The type of access method used (e.g., ALL, index).
 Possible Keys: Indexes that could be used for optimization.
 Key: The actual index used.
 Rows: Estimated number of rows examined.
 Extra: Additional information like whether a temporary table is
used.
 Discuss common terms found in execution plans:
o Full Table Scan: Indicates that no index was used; all rows were
scanned.
o Index Usage: Shows whether an index was utilized and how it
improved performance.
o Join Types: Different types of joins (nested loop, hash join) and their
implications on performance.
 Additional Example Queries:
o Use EXPLAIN to analyze execution plans. Use the EXPLAIN keyword
before each query:
SELECT * FROM QOPRODUCT WHERE P_DISCOUNT > 0;
SELECT COUNT(*) FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT AVG(P_PRICE) FROM QOPRODUCT WHERE P_QOH > 5;
o Activity: After executing each query with EXPLAIN, students should
document:
 Whether any indexes were used.
 The estimated number of rows scanned for each query.
 Any unexpected results that indicate a need for optimization.

3. Index Creation Exercise (20 minutes)


 Concepts:
o Indexing: A performance optimization technique that improves data
retrieval speed by creating a data structure that allows for faster
searches.
o Types of Indexes:
 Single-Column Index: An index on a single column.
 Composite Index: An index on multiple columns.
 Unique Index: Ensures that all values in the indexed column(s)
are unique.
 Example:
o Discuss Scenarios for Indexing:
 Searching for products by price or description.
 Filtering vendors by state.
o Queries: Provide queries that would benefit from indexing:
SELECT * FROM QOPRODUCT WHERE P_PRICE < 20;
SELECT V_NAME FROM QOVENDOR WHERE V_STATE = 'TN';
 Step-by-Step Instructions:
o Step 1: Identify Indexing Opportunities
 Review previous query performance analysis to identify columns
that would benefit from indexing.
 For example, consider
indexing P_PRICE in QOPRODUCT and V_STATE in QOVENDOR.
o Step 2: Create Indexes
 Write SQL commands to create indexes on identified columns:
CREATE INDEX idx_product_price ON QOPRODUCT (P_PRICE);
CREATE INDEX idx_vendor_state ON QOVENDOR (V_STATE);
o Step 3: Execute Index Creation Commands
 Open MySQL Workbench and execute the index creation
commands.
o Step 4: Verify Index Creation
 Use the following command to verify that the indexes were
created successfully:
SHOW INDEX FROM QOPRODUCT;
SHOW INDEX FROM QOVENDOR;
 Explanation: Discuss the impact of indexes on query performance:
o How indexes can reduce scan times and improve query execution
speed.
o Potential downsides, such as increased write times due to maintaining
indexes.
 Additional Example Queries:
o Execute these queries to further analyze the need for indexing:
SELECT AVG(P_PRICE) FROM QOPRODUCT WHERE P_QOH > 5;
SELECT COUNT(*) FROM QOVENDOR WHERE V_STATE = 'TN';
o Activity: After executing each query, students should document:
 Whether any indexes were used.
 The estimated number of rows scanned for each query.
 Create Composite Indexes: For example, if frequently filtering products
by both price and quantity on hand:
CREATE INDEX idx_product_price_qoh ON QOPRODUCT (P_PRICE,
P_QOH);

4. Query Optimization Challenge (20 minutes)


 Concepts:
o Query Optimization: The process of modifying a query to improve
its performance.
o Importance: Efficient queries reduce resource consumption and
improve response times.
 Example Queries: poorly written SQL queries that need optimization:
SELECT * FROM QOPRODUCT WHERE P_QOH < 10;
SELECT * FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT COUNT(*) FROM QOPRODUCT WHERE P_PRICE < 20;
 Step-by-Step Instructions:
o Step 1: Identify Inefficient Queries
 Review the provided queries and identify inefficiencies, such as:
 Using SELECT * instead of specifying columns.
 Lack of filtering conditions that could be optimized.
o Step 2: Rewrite Queries:
 Rewrite the queries for better performance:
-- Optimized query example
SELECT P_CODE, P_DESCRIPT, P_PRICE FROM QOPRODUCT WHERE P_QOH
< 10;
SELECT V_NAME FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT P_CODE, COUNT(*) FROM QOPRODUCT WHERE P_PRICE < 20
GROUP BY P_CODE;
o Step 3: Execute and Compare Performance
 Execute both the original and optimized queries in MySQL
Workbench.
 Use EXPLAIN to analyze the execution plans for each version to
see improvements.
 Explanation:
o Discuss common optimization techniques:
o Avoiding SELECT *: Specify only necessary columns to reduce data
retrieval time.
o Using indexed columns in WHERE clauses to speed up searches.
o Utilizing JOINs efficiently to minimize data processing.
 Additional Example Queries:
o Provide additional poorly written queries:
SELECT * FROM LINE WHERE LINE_PRICE < 10;
SELECT * FROM INVOICE WHERE CUS_CODE = '10012';
 Activity: Students should rewrite these additional queries for optimization:
SELECT INV_NUMBER, LINE_NUMBER, P_CODE FROM LINE WHERE
LINE_PRICE < 10;
SELECT INV_NUMBER, INV_DATE FROM INVOICE WHERE CUS_CODE =
'10012';

5. Conclusion (10 minutes)


In this session, we explored the critical concepts of query execution
plans and index creation, which are essential for optimizing database
performance. Here’s a summary of what we covered:
1. Understanding Query Execution Plans:
o We learned that a query execution plan is a detailed roadmap used by
the database management system (DBMS) to execute SQL queries. It
provides insights into how data is accessed, including the methods
used (e.g., full table scans, index usage) and the estimated cost of
operations.
o By analyzing execution plans, students gained the ability to identify
potential performance bottlenecks in their queries. This skill is crucial
for making informed decisions about how to optimize SQL statements
for better efficiency.
2. Hands-On Analysis:
o Students executed various queries against the SaleCo database and
utilized the EXPLAIN command to view execution plans. This practical
experience allowed them to observe key components such as access
types, possible keys, actual keys used, and the number of rows
processed.
o The analysis highlighted the importance of avoiding full table scans
and leveraging indexes effectively to improve query performance.
3. Index Creation:
o We discussed the role of indexing in enhancing data retrieval speeds.
Students learned about different types of indexes—single-column,
composite, and unique—and when to apply them.
o Through hands-on exercises, students identified opportunities for
indexing based on their query analysis and created indexes on
relevant columns in the SaleCo database. They executed these
commands and verified successful index creation.
4. Optimization Techniques:
o The session emphasized practical optimization techniques such as
avoiding SELECT *, using indexed columns in WHERE clauses, and
writing efficient JOIN statements.
o Students engaged in rewriting poorly structured queries to enhance
their performance, demonstrating their understanding of optimization
principles.
Next Steps
In the upcoming session, we will build upon these foundational skills by
exploring advanced tuning techniques, including monitoring database
performance, transaction management, and real-world case studies of successful
performance tuning. Students will have further opportunities to apply what
they’ve learned through practical exercises that reinforce their understanding of
database optimization strategies.
By mastering these concepts, you will be well-equipped to tackle performance
issues in real-world database environments, ensuring efficient data management
and retrieval in their future projects.

Home Practice Exercises


Exercise 1: Analyze Execution Plans
1. Objective: Use the EXPLAIN command to analyze the execution plans of
various queries.
2. Instructions:
o Write and execute the following queries against the SaleCo database:
EXPLAIN SELECT P_DESCRIPT, P_PRICE FROM QOPRODUCT WHERE
P_QOH < 15;
EXPLAIN SELECT V_NAME FROM QOVENDOR WHERE V_STATE =
'GA';
EXPLAIN SELECT COUNT(*) FROM QOPRODUCT WHERE
P_DISCOUNT > 0;
o Document the execution plans, focusing on:
o The type of access method used (e.g., ALL, index).
o The estimated number of rows scanned.
o Any indexes that were utilized.
Exercise 2: Create Indexes
1. Objective: Identify opportunities for indexing based on query performance
analysis.
2. Instructions:
o Review your previous execution plan analyses.
o Create indexes for columns that would benefit from them. For example:
CREATE INDEX idx_product_discount ON QOPRODUCT
(P_DISCOUNT);
CREATE INDEX idx_vendor_state ON QOVENDOR (V_STATE);
o Execute the index creation commands in MySQL Workbench.
o Verify the creation of indexes using:
SHOW INDEX FROM QOPRODUCT;
SHOW INDEX FROM QOVENDOR;

Exercise 3: Optimize Queries


1. Objective: Rewrite poorly structured queries to improve their performance.
2. Instructions:
o Take the following queries and optimize them:
SELECT * FROM QOPRODUCT WHERE P_QOH < 10;
SELECT * FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT COUNT(*) FROM LINE WHERE LINE_PRICE < 10;
o Rewrite these queries to specify only necessary columns and utilize indexes
effectively. For example:
SELECT P_CODE, P_DESCRIPT, P_PRICE FROM QOPRODUCT WHERE
P_QOH < 10;
SELECT V_NAME FROM QOVENDOR WHERE V_STATE = 'TN';
SELECT LINE_NUMBER, P_CODE FROM LINE WHERE LINE_PRICE <
10;

Exercise 4: Performance Comparison


1. Objective: Compare the performance of original versus optimized queries.
2. Instructions:
o Execute both the original and optimized versions of your queries from
Exercise 3.
o Use EXPLAIN to analyze the execution plans for both versions.
o Document any improvements in performance metrics such as reduced row
scans or improved access methods.

You might also like