Professional Documents
Culture Documents
Introduction
Cost-based optimization
Equivalence Expressions
Database System Concepts - 7th Edition 16.2 ©Silberschatz, Korth and Sudarshan
Introduction
Database System Concepts - 7th Edition 16.3 ©Silberschatz, Korth and Sudarshan
Introduction (Cont.)
Find out how to view query execution plans on your favorite database
Database System Concepts - 7th Edition 16.4 ©Silberschatz, Korth and Sudarshan
Equivalence of Expressions
Database System Concepts - 7th Edition 16.5 ©Silberschatz, Korth and Sudarshan
cost-based query optimization
Database System Concepts - 7th Edition 16.6 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.7 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.8 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.9 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.10 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.11 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.12 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.13 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.14 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.15 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 16.16 ©Silberschatz, Korth and Sudarshan
Semi Join
Database System Concepts - 7th Edition 16.17 ©Silberschatz, Korth and Sudarshan
Semi Join - Example
Database System Concepts - 7th Edition 16.18 ©Silberschatz, Korth and Sudarshan
Equivalence Rules ( 16 Rules ..)
a. σ (E1 x E2) ≡ E1 ⨝ E2
Database System Concepts - 7th Edition 16.19 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
E1 ⨝ E2 ≡ E2 ⨝ E1
Database System Concepts - 7th Edition 16.20 ©Silberschatz, Korth and Sudarshan
Pictorial Depiction of Equivalence Rules
Database System Concepts - 7th Edition 16.21 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
Database System Concepts - 7th Edition 16.22 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
8. The projection operation distributes over the theta join operation as follows:
(a) if involves only attributes from L1 L2:
L1 L2(E1 ⨝ E2) ≡ L1(E1) ⨝ L2(E2)
(b) In general, consider a join E1 ⨝ E2.
• Let L1 and L2 be sets of attributes from E1 and E2, respectively.
• Let L3 be attributes of E1 that are involved in join condition , but are
not in L1 L2, and
• let L4 be attributes of E2 that are involved in join condition , but are
not in L1 L2.
L1 L2(E1 ⨝ E2) ≡ L1 L2( L1 L3(E1) ⨝ L2 L4(E2))
Database System Concepts - 7th Edition 16.23 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
Database System Concepts - 7th Edition 16.24 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
13. Selection distributes over aggregation as below
(G𝛾A(E)) ≡ G𝛾A((E))
provided only involves attributes in G
14. a. Full outerjoin is commutative:
E1 ⟗ E2 ≡ E2 ⟗ E1
b. Left and right outerjoin are not commutative, but:
E1 ⟕ E2 ≡ E2 ⟖ E1
15. Selection distributes over left and right outerjoins as below, provided 1
only involves attributes of E1
a. 1 (E1 ⟕ E2) ≡ (1 (E1)) ⟕ E2
b. 1 (E1 ⟖ E2) ≡ E2 ⟕ (1 (E1))
16. Outerjoins can be replaced by inner joins under some conditions
a. 1 (E1 ⟕ E2) ≡ 1 (E1 ⨝ E2)
b. 1 (E1 ⟖ E1) ≡ 1 (E1 ⨝ E2)
provided 1 is null rejecting on E2
Database System Concepts - 7th Edition 16.25 ©Silberschatz, Korth and Sudarshan
Transformation Example: Pushing Selections
Query: Find the names of all instructors in the Music department, along
with the titles of the courses that they teach
• name, title(dept_name= ‘Music’
(instructor ⨝ (teaches ⨝ course_id, title (course))))
Transformation using rule 7a.
• name, title((dept_name= ‘Music’(instructor)) ⨝
(teaches ⨝ course_id, title (course)))
Performing the selection as early as possible reduces the size of the
relation to be joined.
Database System Concepts - 7th Edition 16.26 ©Silberschatz, Korth and Sudarshan
Multiple Transformations (Cont.)
Database System Concepts - 7th Edition 16.27 ©Silberschatz, Korth and Sudarshan
Join Ordering Example
(Join Associativity) ⨝
(r1 ⨝ r2) ⨝ r3
Database System Concepts - 7th Edition 16.28 ©Silberschatz, Korth and Sudarshan
Join Ordering Example (Cont.)
Database System Concepts - 7th Edition 16.29 ©Silberschatz, Korth and Sudarshan
Cost Estimation
Database System Concepts - 7th Edition 16.30 ©Silberschatz, Korth and Sudarshan
Statistical Information for Cost Estimation
fr: blocking factor of r — i.e., the number of tuples of r that fit into one block.
V(A, r): number of distinct values that appear in r for attribute A; same as
the size of A(r).
nr ùú
é
br = ê
f r úú
ê
ê
Database System Concepts - 7th Edition 16.31 ©Silberschatz, Korth and Sudarshan
Join Operation: Running Example
Running example:
student ⨝ takes
nstudent = 5,000.
ntakes = 10000.
V(ID, takes) = 2500, which implies that on average, each student who has
taken a course has taken 4 courses.
Database System Concepts - 7th Edition 16.32 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins
Database System Concepts - 7th Edition 16.33 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins (Cont.)
Database System Concepts - 7th Edition 16.34 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins (Cont.)
Compute the size estimates for student ⨝ take without using information
about foreign keys:
• We choose the lower estimate, which in this case, is the same as our
earlier computation using foreign keys.
Database System Concepts - 7th Edition 16.35 ©Silberschatz, Korth and Sudarshan
chapter 17: Transactions
Transaction Concept
Transaction State
Concurrent Executions
Serializability
Recoverability
Transaction Definition in SQL
Database System Concepts - 7th Edition 17.2 ©Silberschatz, Korth and Sudarshan
Transaction Concept
Database System Concepts - 7th Edition 17.3 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer
Database System Concepts - 7th Edition 17.4 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer (Cont.)
Database System Concepts - 7th Edition 17.5 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer (Cont.)
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B)
Isolation can be ensured trivially by running transactions serially
• That is, one after the other.
However, executing multiple transactions concurrently has significant
benefits, as we will see later.
Database System Concepts - 7th Edition 17.6 ©Silberschatz, Korth and Sudarshan
ACID Properties
A transaction is a unit of program execution that accesses and possibly
updates various data items. To preserve the integrity of data the database
system must ensure:
Atomicity. Either all operations of the transaction are properly reflected in
the database or none are.
Consistency. Execution of a transaction in isolation preserves the
consistency of the database.
Isolation. Although multiple transactions may execute concurrently, each
transaction must be unaware of other concurrently executing transactions.
Intermediate transaction results must be hidden from other concurrently
executed transactions.
• That is, for every pair of transactions Ti and Tj, it appears to Ti that
either Tj, finished execution before Ti started, or Tj started execution
after Ti finished.
Durability. After a transaction completes successfully, the changes it has
made to the database persist, even if there are system failures.
Database System Concepts - 7th Edition 17.7 ©Silberschatz, Korth and Sudarshan
Transaction State
Active – the initial state; the transaction stays in this state while it is
executing
Partially committed – after the final statement has been executed.
Failed -- after the discovery that normal execution can no longer proceed.
Aborted – after the transaction has been rolled back and the database
restored to its state prior to the start of the transaction. Two options after it
has been aborted:
• Restart the transaction
Can be done only if no internal logical error
• Kill the transaction
Committed – after successful completion.
Database System Concepts - 7th Edition 17.8 ©Silberschatz, Korth and Sudarshan
Transaction State (Cont.)
Database System Concepts - 7th Edition 17.9 ©Silberschatz, Korth and Sudarshan
Schedules
Database System Concepts - 7th Edition 17.10 ©Silberschatz, Korth and Sudarshan
Schedule 1 – serial
Each serial schedule consists of a sequence of instructions from
various transactions, where the instructions belonging to one single
transaction appear together in that schedule.
Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance from A
to B.
A serial schedule in which T1 is followed by T2 :
Precedence graph
Database System Concepts - 7th Edition 17.11 ©Silberschatz, Korth and Sudarshan
Schedule 2
A serial schedule where T2 is followed by T1
Precedence graph
Database System Concepts - 7th Edition 17.12 ©Silberschatz, Korth and Sudarshan
Concurrent transactions
Database System Concepts - 7th Edition 17.13 ©Silberschatz, Korth and Sudarshan
Concurrent Executions
Multiple transactions are allowed to run concurrently in the system.
Advantages are:
• Increased processor and disk utilization, leading to better
transaction throughput
E.g., one transaction can be using the CPU while another is
reading from or writing to the disk
• Reduced average response time for transactions: short
transactions need not wait behind long ones.
Concurrency control schemes – mechanisms to achieve isolation
• That is, to control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database.
• It is the job of the database system to ensure that any schedule
that is executed will leave the database in a consistent state.
Database System Concepts - 7th Edition 17.14 ©Silberschatz, Korth and Sudarshan
Schedule 3
Database System Concepts - 7th Edition 17.15 ©Silberschatz, Korth and Sudarshan
Schedule 4
The following concurrent schedule does not preserve the value of (A and B ).
Database System Concepts - 7th Edition 17.16 ©Silberschatz, Korth and Sudarshan
Serializability
Database System Concepts - 7th Edition 17.17 ©Silberschatz, Korth and Sudarshan
conflict serializability
Database System Concepts - 7th Edition 17.19 ©Silberschatz, Korth and Sudarshan
Precedence graph
Consider a schedule S.
Precedence graph consists of a pair G = (V, E), where V is a set of
vertices and E is a set of edges. The set of vertices consists of all the
transactions participating in the schedule S.
The set of edges consists of all edges Ti → Tj for which one of three
conditions holds:
1. Ti executes write(Q) before Tj executes read(Q).
2. Ti executes read(Q) before Tj executes write(Q).
3. Ti executes write(Q) before Tj executes write(Q).
If an edge Ti →Tj exists in the precedence graph, then, in any serial
schedule S′ equivalent to S, Ti must appear before Tj.
Database System Concepts - 7th Edition 17.20 ©Silberschatz, Korth and Sudarshan
Test for Conflict Serializability
Database System Concepts - 7th Edition 17.21 ©Silberschatz, Korth and Sudarshan
Transaction Definition in SQL
Database System Concepts - 7th Edition 17.22 ©Silberschatz, Korth and Sudarshan
Implementation of Isolation Levels
Locking
• Lock on whole database vs lock on items
• How long to hold lock?
• Shared vs exclusive locks
Timestamps
• Transaction timestamp assigned e.g. when a transaction begins
• Data items store two timestamps
Read timestamp
Write timestamp
• Timestamps are used to detect out of order accesses
Multiple versions of each data item
• Allow transactions to read from a “snapshot” of the database
Database System Concepts - 7th Edition 17.23 ©Silberschatz, Korth and Sudarshan
Transactions as SQL Statements
E.g., Transaction 1:
select ID, name from instructor where salary > 90000
E.g., Transaction 2:
insert into instructor values ('11111', 'James', 'Marketing', 100000)
Suppose
• T1 starts, finds tuples salary > 90000 using index and locks them
• And then T2 executes.
• Do T1 and T2 conflict? Does tuple level locking detect the conflict?
• Instance of the phantom phenomenon
Also consider T3 below, with Wu’s salary = 90000
update instructor
set salary = salary * 0.9
where name = 'Wu’
Key idea: Detect “predicate” conflicts, and use some form of “predicate
locking”
Database System Concepts - 7th Edition 17.24 ©Silberschatz, Korth and Sudarshan
End of Chapter 17
Database System Concepts - 7th Edition 17.25 ©Silberschatz, Korth and Sudarshan