Download Query Optimization - Data Base Management System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Query Optimization
• The execution of an SQL query:
- Parse and verify the query, create an equivalent query tree
where each node is a relational algebra operation and
each leaf is a table from the query
- Apply heuristics to alter the query tree to find equivalent
queries based on algebraic equivalences
- Generate alternate execution plans by assigning
implementations to operations and orders to joins
- Estimate the cost of each operation based on statistics and available access paths
- Choose the lowest cost execution plan
Query Optimization
SELECT DISTINCT
R.A, S.C, S.D, T.D
FROM R, S, T
WHERE R.A=S.A and S.B=T.B AND R.C < 100 AND S.A > 20
R S T
Join on R.A=S.A
Join on S.B=T.B
Select R.C<100 and S.A>
Project distinct R.A, S.C, S.D, T.D
Query Optimization
Push selections on each relation down, as close to the relation as possible.
R S
T
Join on R.A=S.A
Join on S.B=T.B
Select S.A>
Project distinct R.A, S.C, S.D, T.D
Select R.C<100 and R.A>
Query Optimization
Add projections whenever appropriate
R S
T
Join on R.A=S.A
Join on S.B=T.B
Select S.A>
Project distinct R.A, S.C, S.D, T.D
Select R.C<100 and R.A>
Project distinct R.A Project distinct S.A, S.B, S.C, S.D
Project distinct T.B,T.D
Implementation plans
- Assign implementations to logical operators given memory
limitations
- Join mapped to block-sort join, merge-sort join, etc.
- Selection mapped to table scan or index scan, etc.
- Assign join ordering to joins
R join S join T = (R join S) join T = R join (S join T) = (R join T) join S For each join, inner/outer relations can be changed
- Estimate the size of each relation and cost of each operation
Implementation plans
• Blocking operators require the whole relation to be
present before any output can be computed
- For example grouping, sorting, project distinct
• A non-blocking operator can be pipelined
- As soon as a tuple is found to be in the output of an
operator, it can be pipelined to the next operator
- Hence, the output buffer for an operator serves as the
input buffer of the next operator
Join ordering
Join ordering depends on the size of the output and the access paths available for each relation Table^ CARD^ VALUES (attr A) R (^) 1million 10, S (^) 100,000 100, T (^) 200,000 5,
(R JOIN S ON R.A=S.A) JOIN T ON S.A=T.A
Size of R JOIN S= 1,000,000 * 100,000 * 1/100,000 = 1,000, Size of (R JOIN S) JOIN T= 1,000,000 * 200,000 * 1/10, = 20,000,
R JOIN ON R.A=S.A (S JOIN T ON S.A=T.A)
Size of S JOIN T= 100,000 * 200,000 * 1/100,000 = 200, Size of R JOIN (S JOIN T)= 1,000,000 * 200,000 * 1/10, = 20,000,
Choosing join ordering
- The set of possible join orders is extremely large. Instead concentrate on left deep join orders
- Left join orders make it possible to pipeline the output of one join as input to the other join
JOIN
JOIN
JOIN
R S
T
V
To find all possible left-deep join orders
- First find all possible two way joins over the given relations, estimate the cost of the best implementation plan
- Then, find the next relation to join with the result, estimate the cost
- Remove any joins that are too costly compared to the others
- Keep enumerating all joins!
Relational Calculus (II)
• Of the form:
{ p .PATIENTS_NAME:
p IN PATIENTS and p.DOB > 4/1/70}
- Target (attributes) and Qualifying Statement
- Generalization of algebraic operations obvious except join(existential quantifier) and divide (universal quantifier)
• Existential Quantifier ∃: there exists
(at least one row) { p .PATIENTS_NAME : p in PATIENTS and exists l in LABS (p.pat_num = l.pat_num and l.lab_name = ‘T4’)}
- would involve a join in the relational algebra
Relational Calculus (III)
• Universal Quantifier ∀ (applies for all)
– {p.NAME: p in patients and for every d in
DOCTORS
Exists c in CLINIC_VISIT
(c.PROVIDER_ID = d.PROVIDER_ID and
p.PATIENT_ID = c.PATIENT_ID)
– What is returned?
- All patients who have seen every doctor
- Cognitive Studies: relational algebra easier to comprehend than relational calculus
Transactional Integrity
- A procedure or set of procedures which is guaranteed to
preserve database integrity is a transaction
- Database is consistent before and after a transaction:
atomicity (no intermediate state)
Patient’s Note Joe is sick
Patient’s Note Joe is sick, I have started IV D5NS
Patient’s Note Joe is sick, he should receive intravenou s fluid
Provider 1
Provider 2
Means to Concurrency Control
• Locking table, row, attribute
– (e.g. select for update)
Patient’s Note Joe is sick
Patient’s Note Joe is sick, I have started IV D5NS
Patient’s Note Joe is sick, he should receive intravenou s fluid
Provider 1
Provider 2
Transaction Processing
• Transactions that execute in a database system must
satisfy the following properties:
- Atomicity: each transaction either executes fully, or does
not have any effect
- Consistency: each transaction is a logical combination of
actions that change the database from one consistent
state to another
- Isolation: transactions are written and executed as if they
are executing one at a time
- Durability: the results of successful transactions are never
lost even in the presence of unforeseen failures