0% found this document useful (0 votes)
18 views

CIT-503 DAM Week 6

The document provides an overview of query processing and optimization in database management systems (DBMS), detailing the steps involved in transforming high-level queries into efficient execution plans. It covers phases such as parsing, logical and physical optimization, and execution, along with best practices for enhancing query efficiency. Additionally, it discusses challenges in query processing and methods for improving performance, including indexing and avoiding unnecessary data retrieval.

Uploaded by

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

CIT-503 DAM Week 6

The document provides an overview of query processing and optimization in database management systems (DBMS), detailing the steps involved in transforming high-level queries into efficient execution plans. It covers phases such as parsing, logical and physical optimization, and execution, along with best practices for enhancing query efficiency. Additionally, it discusses challenges in query processing and methods for improving performance, including indexing and avoiding unnecessary data retrieval.

Uploaded by

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

1

Database Administration and


Management

CIT- 503 4(3-1)

Major & Compulsory


Database Administration and
Management

 This Week Contents


 Query Processing and Optimization
Query Processing and
Optimization
Intro (Query Processing):
Query processing and optimization
involve the steps taken by a database
management system (DBMS) to
transform a high-level query (e.g., SQL)
into an efficient low-level execution
plan and retrieve the desired results
efficiently.
Query Processing and
Optimization
Intro (Query Processing):
Query processing is the sequence of
activities a Database Management
System (DBMS) performs to execute a
query and retrieve the desired results
efficiently.
It involves transforming a high-level
query, such as SQL, into low-level
operations that the database can
execute.
Query Processing and
Optimization
Phases of Query Processing:
Parsing and Translation:
The DBMS parses the query to check
for syntax errors and transforms it into
an internal representation (usually a
parse tree or query tree).
Validates the query against the
database schema to ensure it
references valid tables and columns.
Query Processing and
Optimization
Phases of Query Processing:
Query Rewrite (Logical Optimization):
 The query is rewritten to apply logical
transformations that improve efficiency
without altering the result.
 Examples:
 Replacing SELECT * with specific column
names.
 Simplifying conditions (e.g., converting
WHERE a = 5 AND a > 3 to WHERE a = 5).
Query Processing and
Optimization
Phases of Query Processing:
Logical Query Plan Generation:
 The DBMS generates a logical query plan
that specifies what operations need to be
performed (e.g., joins, projections) and their
sequence.
Query Processing and
Optimization
Phases of Query Processing:
Physical Query Plan Generation
(Physical Optimization):
 The DBMS generates a physical query plan
specifying how the operations are executed.
 Considers available access paths (e.g.,
indexes, table scans) and join algorithms
(e.g., nested-loop, hash join).
Query Execution:
 The physical query plan is executed by the
DBMS to retrieve the result set.
Query Processing and
Optimization
Key Components in Query
Processing
Relational Algebra:
 A formal system representing queries with
mathematical operators like selection (σ),
projection (π), and join (⋈).
 Example:
SQL Query:
 SELECT Name FROM Students WHERE Age
> 18;
Query Processing and
Optimization
Key Components in Query
Processing
Relational Algebra:
 Relational Algebra:
 πName(σAge > 18(Students))
Query Processing and
Optimization
Key Components in Query Processing
Execution Plan:
 A step-by-step strategy for retrieving results.
 Includes details about access paths (e.g.,
index scans, table scans) and operation orders.
Cost Estimation:
 A DBMS estimates the cost of operations,
considering factors like disk I/O, CPU usage,
and memory.
 The execution plan with the lowest cost is
selected.
Query Processing and
Optimization
Key Components in Query
Processing
Access Methods:
 Sequential Scan: Read the entire table
row by row.
 Index Scan: Use an index to find relevant
rows quickly.
 Hybrid Methods: Combine multiple access
methods for efficiency.
Query Processing and
Optimization
Example of Query Processing
Query:
 SELECT EmployeeName
 FROM Employees
 WHERE Department = 'HR' AND
Salary > 50000;
Query Processing and
Optimization
Example of Query Processing
Steps:
 Parsing:
 Validate syntax and semantics, producing a
parse tree.
 Translation:
 Convert into relational algebra: plaintext
 πEmployeeName(σDepartment = 'HR' AND
Salary > 50000(Employees))
Query Processing and
Optimization
Example of Query Processing
Steps:
 Optimization:
 Apply transformations:
 Push selection conditions down to reduce
intermediate results:
 σDepartment = 'HR'(σSalary > 50000(Employees))

 Choose an index scan on Salary if available.


Query Processing and
Optimization
Example of Query Processing
Steps:
Execution:
 Retrieve rows matching Salary > 50000
using an index.
 Filter rows where Department = 'HR'.
 Return EmployeeName.
Query Processing and
Optimization
Query Processing Challenges
 Handling Complex Queries:
 Nested subqueries or joins involving large datasets can
be computationally expensive.
 Concurrency:
 Queries may compete for resources in multi-user
environments.
 Dynamic Data:
 Data changes during query execution can impact
results.
 Resource Constraints:
 Limited memory or CPU can restrict optimization
opportunities.
Query Processing and
Optimization
Best Practices for Query Efficiency
 Indexing:
 Use indexes on frequently queried columns.
 Avoid SELECT *:
 Specify only the required columns to reduce data
transfer.
 Filter Early:
 Use WHERE conditions to reduce the dataset
before joins or aggregations.
 Analyze Execution Plans:
 Use tools like EXPLAIN in MySQL or PostgreSQL to
understand query performance.
Query Processing and
Optimization
Intro (Query Optimization)
Query optimization is the process of
improving the performance of a
database query by modifying its
execution plan or rewriting it without
changing the results.
It ensures that queries run efficiently
by using the least amount of resources
(e.g., time, memory, CPU, etc.).
Query Processing and
Optimization
Examples of Query Optimization
Use Proper Indexing
Indexes help speed up data retrieval.
Without indexes, the database may perform
a full table scan.
Example Without Index:
SELECT * FROM employees WHERE
last_name = 'Smith';
If there is no index on the last_name
column, the database will scan the entire
employees table.
Query Processing and
Optimization
Examples of Query Optimization
Use Proper Indexing
Optimized:
CREATE INDEX idx_last_name ON
employees(last_name);
SELECT * FROM employees WHERE
last_name = 'Smith';
With an index, the query retrieves data
faster because it can directly access
the relevant rows.
Query Processing and
Optimization
Examples of Query Optimization
Avoid SELECT * (Select Specific
Columns)
Fetching unnecessary columns increases
the amount of data transferred and
processed.
Example Without Optimization:
SELECT * FROM employees WHERE
department_id = 10;
This fetches all columns, even those not
needed.
Query Processing and
Optimization
Examples of Query Optimization
Avoid SELECT * (Select Specific
Columns)
Optimized:
SELECT employee_id, first_name,
last_name FROM employees WHERE
department_id = 10;
By specifying only required columns,
the query processes less data.
Query Processing and
Optimization
Examples of Query Optimization
Filter Early with WHERE Clauses
Filtering data early reduces the number
of rows processed in later operations.
Example Without Optimization:
SELECT employee_id, department_id
FROM employees ORDER BY
department_id;
This retrieves all rows and then orders
them.
Query Processing and
Optimization
Examples of Query Optimization
Filter Early with WHERE Clauses
Optimized:
SELECT employee_id, department_id
FROM employees WHERE
department_id = 10 ORDER BY
department_id;
Adding a WHERE clause first reduces
the data set before sorting.
Query Processing and
Optimization
Examples of Query Optimization
Avoid Using Functions on Indexed
Columns
Using a function on an indexed column
prevents the database from using the
index.
Example Without Optimization:
SELECT * FROM employees WHERE
UPPER(last_name) = 'SMITH';
Query Processing and
Optimization
Examples of Query Optimization
Avoid Using Functions on Indexed
Columns
Optimized:
SELECT * FROM employees WHERE
last_name = 'Smith';
Ensure the query uses the indexed
column directly without
transformations.
Query Processing and
Optimization
Examples of Query Optimization
Use Joins Instead of Subqueries
Joins are often more efficient than
correlated subqueries.
Example Without Optimization:
SELECT employee_id, first_name
FROM employees
WHERE department_id IN (SELECT
department_id FROM departments
WHERE location_id = 1700);
Query Processing and
Optimization
Examples of Query Optimization
Use Joins Instead of Subqueries
Optimized:
SELECT e.employee_id, e.first_name
FROM employees e
JOIN departments d ON e.department_id
= d.department_id
WHERE d.location_id = 1700;
Joins eliminate the need for repeated
execution of subqueries.
Query Processing and
Optimization
Examples of Query Optimization
Use LIMIT for Large Data Sets
If you only need a subset of results, use
LIMIT to avoid fetching unnecessary
rows.
Example Without Optimization:
SELECT * FROM employees ORDER BY
hire_date;
This retrieves all rows, even if you only
need the top 10.
Query Processing and
Optimization
Examples of Query Optimization
Use LIMIT for Large Data Sets
Optimized:
SELECT * FROM employees ORDER BY
hire_date LIMIT 10;
Query Processing and
Optimization
Examples of Query Optimization
Analyze and Use Query Execution Plans
Use tools like EXPLAIN or EXPLAIN PLAN to
understand how your query is executed. Look
for potential bottlenecks, such as full table
scans or large temporary tables, and optimize
accordingly.
Example Execution Plan Check:
EXPLAIN SELECT * FROM employees WHERE
last_name = 'Smith';
These techniques ensure better performance
and resource utilization for database queries.
Query Processing and
Optimization

Topic 07

Completed
34
Database Administration and
Management
Course Contents Course Contents
Completed Completed
 Introduction to  Recovery
advance data models techniques
such as object,
 Query processing
relational
 object oriented and optimization
 File organizations  Database
concepts Programming
 Transactional  Integrity and
processing and security
Concurrency control
techniques 35
Database Administration and
Management
Course Contents Course Contents
Completed Completed
 Database  Emerging
Administration research trends
 Physical database in database
design and tuning systems.
 Distributed
database systems

36
Database Administration and
Management
Total No of Per Week
Topics Topics
Coverage
Rate
12
0.75
37
Database Administration and
Management
Marks %Age
Division 5%
 Attendance 5%
 Assignments 5%
 Quiz 5%
 Presentation 30%
 Mid Term Exam 50%
 Final Term Exam

38
39

You might also like