Adb Lecture Fourr
Adb Lecture Fourr
AND OPTIMIZATION
J.Rabera 1
J.Rabera 2
J.Rabera 3
J.Rabera 4
Data Definition Language
J.Rabera 5
Data Manipulation Language
J.Rabera 6
Data Control Language
J.Rabera 7
Cont…
J.Rabera 8
Transaction Control Language
J.Rabera 9
J.Rabera 10
J.Rabera 11
REVIEW OF CREATE, INSERT, &
DELETE
J.Rabera 12
J.Rabera 13
J.Rabera 14
J.Rabera 15
Creating a Schema/Database
J.Rabera 16
CONT….
J.Rabera 17
SQL Script to be Applied on DB
J.Rabera 18
J.Rabera 19
How The Schema Appears
J.Rabera 20
J.Rabera 21
J.Rabera 22
Query to Create Table
J.Rabera 23
Populating The Table
J.Rabera 24
How it Appears on the Schema
J.Rabera 25
Querying from Table Created
J.Rabera 26
Querying Data from Table
Components
J.Rabera 27
J.Rabera 28
J.Rabera 29
Query to Execute Insert
J.Rabera 30
J.Rabera 31
J.Rabera 32
QUERY OPTIMIZATION
Query Optimization in DBMS is the process of selecting the most efficient way to
execute a SQL statement. Because SQL is a nonprocedural language, the
optimizer can merge, restructure, and process data in any sequence.
The query optimizer (also known as the optimizer) is database software that
identifies the most efficient way (like by reducing time) for a SQL statement to
access data
J.Rabera 33
Query Optimization in DBMS
J.Rabera 34
Factors to Consider
• How large is the result set? Should we brace ourselves for a million rows returned,
or just a few?
• Are there any parameters that have limited values? Will a given parameter
always have the same value, or are there other limitations on values that can simplify
our work by eliminating avenues of research.
• How often is the query executed? Something that occurs once a day will be
treated very differently than one that is run every second.
• Are there any invalid or unusual input values that are indicative of an
application problem? Is one input set to NULL, but never should be NULL? Are any
other inputs set to values that make no sense, are contradictory, or otherwise go
against the use-case of the query?
• Are there any obvious logical, syntactical, or optimization problems staring
us in the face? Do we see any immediate performance bombs that will always
perform poorly, regardless of parameter values or other variables?
• What is acceptable query performance? How fast must the query be for its
consumers to be happy? If server performance is poor, how much do we need to
decrease resource consumption for it to be acceptable? Lastly, what is the current
performance of the query? This will provide us with a baseline so we know how much
improvement is needed. J.Rabera 35
Optimizer Components
J.Rabera 36
Query Transformer
• The query transformer determines
whether it is advantageous to
rewrite the original SQL statement
into a semantically equivalent SQL
statement at a lower cost for some
statements.
• When a plausible alternative exists,
the database compares the costs of
each alternative and chooses the
one with the lowest cost. The query
transformer shown in the query
below can be taken as an example of
how query optimization is done by
transforming an OR-based input
query into a UNION ALL-based
output query.
J.Rabera 37
Estimator
• The estimator is the optimizer component that
calculates the total cost of a given execution plan.
• To determine the cost, the estimator employs three
different methods:
• Selectivity: The query picks a percentage of the rows
in the row set, with 0 indicating no rows
and 1 indicating all rows. Selectivity is determined by
a query predicate, such as WHERE the last
name LIKE X%, or by a mix of predicates. As the
selectivity value approaches zero, a predicate gets
more selective, and as the value nears one, it
becomes less selective (or more unselective).
J.Rabera 38
Cont…
• For example, The row set can be a base table, a view,
or the result of a join. The selectivity is tied to a query
predicate, such as last_name = 'Prakash', or a
combination of predicates, such as last_name =
'Prakash' AND job_id = 'SDE'.
J.Rabera 39
Cont…
• Cardinality: The cardinality of an execution plan is the number
of rows returned by each action. This input is shared by all
cost functions and is essential for determining the best
strategy. Cardinality can be calculated using DBMS STATS
table statistics or after taking into account the impact of
predicates (filter, join, and so on), DISTINCT or GROUP BY
operations, and so on. In an execution plan, the Rows column
displays the estimated cardinality.
J.Rabera 41
Plan Generator
• The plan generator investigates
multiple plans for a query block
by experimenting with various
access paths, join methods, and
join orders.
J.Rabera 43
Cost-Based Query Optimization in DBMS
• The Optimizer aims to find the plan with the lowest cost estimate
after evaluating the costs of all feasible plans. As a result, the
Optimizer is sometimes known as the Cost-Based Optimizer.
J.Rabera 44
Execution Plans
• The optimizer can defer the final plan decision for a statement
with an adaptive plan until execution time.
J.Rabera 49
HowAdaptive Query Plans
Work
• An adaptive plan is made up of several predefined sub
plans and an optimizer statistics collector.
J.Rabera 52