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

Module-4

The document outlines the concepts of SQL, query processing, and optimization in database management systems, detailing the steps involved in translating SQL queries to relational algebra and the optimization process. It covers the roles of parsing, optimization, and execution in query processing, as well as the basic operations of relational algebra. Additionally, it discusses the challenges of query optimization in distributed database management systems and provides examples of SQL to relational algebra translations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module-4

The document outlines the concepts of SQL, query processing, and optimization in database management systems, detailing the steps involved in translating SQL queries to relational algebra and the optimization process. It covers the roles of parsing, optimization, and execution in query processing, as well as the basic operations of relational algebra. Additionally, it discusses the challenges of query optimization in distributed database management systems and provides examples of SQL to relational algebra translations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

DATABASE MANAGEMENT SYSTEMS

COURSE CODE: CSE-2007


MODULE – 4
(SQL, Query Processing and Optimization)

By:
Dr. Nagendra Panini Challa
Assistant Professor, Senior Grade 2
SCOPE, VIT-AP University, India
AGENDA
 SQL
 Steps in Query Processing
 Transforming SQL queries to Relational
Algebra
 Heuristic Query Optimization

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 2


QUERY PROCESSING
 Query Processing includes
translations on high level
Queries into low level
expressions that can be used at
physical level of file system,
query optimization and actual
execution of query to get the
actual result.
 Block Diagram of Query
Processing is as:

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 3


Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 4
It is done in the following steps:
Step-1:
Parser: During parse call, the database performs the following checks- Syntax
check, Semantic check and Shared pool check, after converting the query into
relational algebra.
Parser performs the following checks as (refer detailed diagram):
 Syntax check – concludes SQL syntactic validity.

Example: SELECT * FORM employee


Here error of wrong spelling of FROM is given by this check.
 Semantic check – determines whether the statement is meaningful or not.
Example: query contains a tablename which does not exist is checked by this
check.
 Shared Pool check – Every query possess a hash code during its execution. So,
this check determines existence of written hash code in shared pool if code
exists in shared pool then database will not take additional steps for optimization
and execution.

02/03/2025 5
 Hard Parse and Soft Parse –
If there is a fresh query and its hash code does not exist in shared pool then that query has
to pass through from the additional steps known as hard parsing otherwise if hash code
exists then query does not passes through additional steps. It just passes directly to
execution engine. This is known as soft parsing.
Hard Parse includes following steps – Optimizer and Row source generation.
Step-2:
Optimizer: During optimization stage, database must perform a hard parse atleast for one
unique DML statement and perform optimization during this parse. This database never
optimizes DDL unless it includes a DML component such as subquery that require
optimization. It is a process in which multiple query execution plan for satisfying a query are
examined and most efficient query plan is satisfied for execution.
Database catalog stores the execution plans and then optimizer passes the lowest cost plan
for execution.
Row Source Generation –
The Row Source Generation is a software that receives a optimal execution plan from the
optimizer and produces an iterative execution plan that is usable by the rest of the
database. the iterative plan is the binary program that when executes by the sql engine
produces the result set.
Step-3:
Execution Engine: Finally runs the query and display the required result.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 6
RELATIONAL ALGEBRA FOR
QUERY OPTIMIZATION
 When a query is placed, it is at first scanned, parsed and validated. An internal
representation of the query is then created such as a query tree or a query graph.
Then alternative execution strategies are devised for retrieving results from the
database tables. The process of choosing the most appropriate execution strategy
for query processing is called query optimization.
Query Optimization Issues in DDBMS
In DDBMS, query optimization is a crucial task. The complexity is high since number
of alternative strategies may increase exponentially due to the following factors −
 The presence of a number of fragments.
 Distribution of the fragments or tables across various sites.
 The speed of communication links.
 Disparity in local processing capabilities.

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 7


Hence, in a distributed system, the target is often to find a good execution
strategy for query processing rather than the best one. The time to execute
a query is the sum of the following −
 Time to communicate queries to databases.
 Time to execute local query fragments.
 Time to assemble data from different sites.
 Time to display results to the application.

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 8


Relational Algebra
Relational algebra defines the basic set of operations of relational database
model. A sequence of relational algebra operations forms a relational algebra
expression. The result of this expression represents the result of a database query.
The basic operations are −
 Projection
 Selection
 Union
 Intersection
 Minus
 Join

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 9


Projection (Column selection – pi operator)
 Projection operation displays a subset of fields of a
table. This gives a vertical partition of the table.
 Syntax in Relational Algebra
 $$\pi_{<{AttributeList}>}{(<{Table Name}>)}$$
 For example, let us consider the following Student
database −
 If we want to display the names and courses of all
students, we will use the following relational algebra
expression −
 $$\pi_{Name, Course}{(STUDENT)}$$

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 10


Example: CUSTOMER RELATION

∏ NAME, CITY (CUSTOMER)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 11


Selection (row selection – sigma operator)
Selection operation displays a subset of tuples of a table that satisfies certain
conditions. This gives a horizontal partition of the table.
 Syntax in Relational Algebra
 $$\sigma_{<{Conditions}>}{(<{Table Name}>)}$$

 For example, in the Student table, if we want to display the details of all
students who have opted for MCA course, we will use the following relational
algebra expression −

 $$\sigma_{Course} = {\small "BCA"}^{(STUDENT)}$$

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 12


For example: LOAN
Relation

σ BRANCH_NAME="perryride"(LOAN)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 13


Examples:

Output – Selects tuples from Tutorials where topic =


‘Database’.

Output – Selects tuples from Tutorials where the topic is ‘Database’ and ‘author’ is
guru99.

Output – Selects tuples from Customers where sales is greater than


50000

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 14


Combination of Projection and Selection Operations
 For most queries, we need a combination of projection and selection operations.

 Using sequence of projection and selection operations.

 For example, to display names of all female students of the BCA course −
 Relational algebra expression using sequence of projection and selection operations

 $$\pi_{Name}(\sigma_{Gender = \small "Female" AND \: Course = \small "BCA"}


{(STUDENT)})$$

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 15


Union Operation:
 Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
 It eliminates the duplicate tuples. It is denoted by ∪.

Notation: R ∪ S
A union operation must hold the following condition:
 R and S must have the attribute of the same number.
 Duplicate tuples are eliminated automatically.

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 16


∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR
)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 17


Set Intersection: (print common from both tables)
 Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
 It is denoted by intersection ∩.

Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table

Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 18


Set Difference: (Remove common and print both the tables)
 Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
 It is denoted by intersection minus (-).

Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 19


Cartesian product
 The Cartesian product is used to combine each row in one table with each
row in the other table. It is also known as a cross product.
 It is denoted by X.

Notation: E X D

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 20


Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 21
SQL TO RA
 An SQL query is first translated into an equivalent extended relational
algebra expression—represented as a query tree data structure—that is
then optimized.
 Typically, SQL queries are decomposed into query blocks, which form the
basic units that can be translated into the algebraic operators and
optimized.
 A query block contains a single SELECT-FROM-WHERE expression, as well
as GROUP BY and HAVING clauses if these are part of the block.
 Hence, nested queries within a query are identified as separate query
blocks.
 Because SQL includes aggregate operators—such as MAX, MIN, SUM,
and COUNT—these operators must also be included in the
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 22
extended algebra
 Consider the following SQL query on the EMPLOYEE relation

SELECT Lname, Fname


FROM EMPLOYEE
WHERE Salary > ( SELECT MAX (Salary)
FROM EMPLOYEE
WHERE Dno=5 );

 This query retrieves the names of employees (from any department in the
company) who earn a salary that is greater than the highest salary in
department 5. The query includes a nested subquery and hence would be
decomposed into two blocks. The inner block is:

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 23


 SELECT MAX (Salary)
where c represents the result returned from the
inner block. The inner block could be translated
 FROM EMPLOYEE into the following extended relational algebra
expression:

 WHERE Dno=5 πMAX Salary(σDno=5(EMPLOYEE))

and the outer block into the expression:


This retrieves the highest salary in department
5. πLname,Fname(σSalary>c(EMPLOYEE))
The outer query block is:
The query optimizer would then choose an
execution plan for each query block.
 SELECT Lname, Fname

 FROM EMPLOYEE

 WHERE Salary > c

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 24


Examples
 Write a query for titles and years of movies made by Disney which are
atleast 100 minutes long
Movie(name, length, year, studio_name)
 Write a query to print all the employees where DOJ is greater than DOJ of
manager
Employee(name, salary, DOJ, aadhar, manager_aadhar)
 List the names of students registered in database course

Students, Registration, Course

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 25


HEURISTIC QUERY Object Graph – Relational Calculus
OPTIMIZATION Operator Graph – Relational Algebra

Operator graphs is nothing but


sequence of operators

Steps in converting a query tree during heuristic optimization.


 Initial (canonical) query tree for SQL query Q.
 Moving SELECT operations down the query tree.
 Applying the more restrictive SELECT operation first.
 Replacing CARTESIAN PRODUCT and SELECT with JOIN operations.
 Moving PROJECT operations down the query tree.

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 26


Query:

Select S.name, S.address, E.grade


FROM Course C, Student S, Enrollment E
Where S.rollno=E.rollno AND C.Cno = E.cno
AND Cname = ‘PDBD)

Relation Schemas

Course(Cno, Cname, Credits)


Student(rollno, Name, Address, Sex)
Enrollment(Eno, rollno, Grade)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 27


Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 28
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 29
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 30
Example Problem

1) SELECT PNUMBER, DNUM, LNAME FROM PROJECT, DEPARTMENT,


EMPLOYEE WHERE DNUM=DNUMBER and MGREMPID=EMPID and
PLOCATION = 'Stafford’;
EMPLOYEE (FNAME, MI, LNAME, EMPID, BDATE, ADDRESS, Sex,
SALARY, SUPERMPID, DNO)
DEPARTMENT (DNAME, DNUMBER, MGREMPID, MGRSTARTD)
PROJECT (PNAME, PNUMBER, PLOCATION, DNUM)

Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 31


Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 32
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 02/03/2025 33

You might also like