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

unit-4-dbms

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

unit-4-dbms

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Unit-4

QUERY PROCESSING AND TRANSACTION


PROCESSING
• Translating SQL Queries into Relational Algebra – heuristic
query optimization – Introduction to Transaction Processing
– Transaction and System concepts - Desirable properties of
Transactions – Characterizing schedules based on
recoverability – Characterizing schedules based on
serializability
Click to add text
QUERY PROCESSING
• A database is a system and the users are either another system or application
or a person. The user can request the data in a language that he understands.
But DBMS has its own language (SQL) which it understands. Hence the users
are asked to query the database in its language – SQL. This SQL is a high level
language created to build a bridge between user and DBMS for their
communication. But the underlying systems in the DBMS will not understand
SQL. There has to be some low level language which these systems can
understand. Usually any query written in SQL is converted into low level
language using relational algebra which system can understand. But it will be
difficult for any user to directly write relational algebra kind of queries. It
requires thorough knowledge of it.
Query Processing

Figure. Typical steps when processing a high-level query


Relational Algebra in DBMS:

• Relational algebra is a procedural query


language, which takes instances of relations as
input and yields instances of relations as
output. It uses operators to perform queries.
An operator can be either unary or binary.
They accept relations as their input and yield
relations as their output.
Basic SQL Relational Algebra Operations
• Unary Relational Operations
• SELECT (symbol: σ)
• PROJECT (symbol: π)
• RENAME (symbol: ρ)
• Relational Algebra Operations From Set Theory
• UNION (υ)
• INTERSECTION ( .),
• DIFFERENCE (-)
• CARTESIAN PRODUCT ( x )
• Binary Relational Operations
• JOIN
• DIVISION
SELECT (σ)
• The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition.
• Sigma(σ) Symbol denotes it. It is used as an expression to choose tuples
which meet the selection condition.
• Select operator selects tuples that satisfy a given predicate.
• σp(r)
– σ is the predicate
– r stands for relation which is the name of the table
– p is prepositional logic
• Example 1
– σ topic = "Database" (Course)
• Output - Selects tuples from Course where topic = 'Database'.
• Example 2
– σ sales > 50000 (Customers)
• Output - Selects tuples from Customers where sales is greater than 50000
Projection(π)

• The projection eliminates all attributes of the


input relation but those mentioned in the
projection list.
• This helps to extract the values of specified
attributes to eliminates duplicate values.
• (pi) symbol is used to choose attributes from a
relation. This operator helps you to keep
specific columns from a relation and discards
the other columns.
Example of Projection:
Consider the following table,
CustomerID CustomerName Status

1 Google Active
2 Amazon Active
3 Apple Inactiv
e
4 Alibaba Active

Here, the projection of CustomerName and status will give


CustomerName Status
Π CustomerName, Status
(Customers) Google Active
Amazon Active
Apple Inactive
Alibaba Active
Rename (ρ)

• Rename is a unary operation used for


renaming attributes of a relation.
• ρ (a/b)R will rename the attribute 'b' of
relation by 'a'.
Union operation (υ)

• UNION is symbolized by ∪ symbol. It includes


all tuples that are in tables A or in B. It also
eliminates duplicate tuples. So, set A UNION
set B would be expressed as:
Union operation: Example
Intersection
• An intersection is defined by the symbol ∩
• A ∩ B-Defines a relation consisting of a set of
all tuple that are in both A and B. However, A
and B must be union-compatible.
Cartesian Product(X) in DBMS
• It is an operation used to merge columns from
two relations. Generally, a cartesian product is
never a meaningful operation when it performs
alone. However, it becomes meaningful when it is
followed by other operations. It is also called
Cross Product or Cross Join.
• Example – Cartesian product
– σauthor = ‘Author1'(Books Χ Articles)
• Output – The above example yields a relation,
which shows all the books and articles written by
Author1
Join Operations
• Join operation is essentially a cartesian product followed by a selection
criterion.
• Join operation denoted by ⋈.
• JOIN operation also allows joining variously related tuples from
different relations.
• Types of JOIN:
• Various forms of join operation are:
• Inner Joins:
– Theta join
– EQUI join
– Natural join
• Outer join:
– Left Outer Join
– Right Outer Join
– Full Outer Join
Theta Join:
• The general case of JOIN operation is called a
Theta join. It is denoted by symbol θ
Example
• A ⋈θ B -Theta join can use any conditions in
the selection criteria.
• For example:
EQUI join:

• When a theta join uses only equivalence


condition, it becomes a equi join.
• For example:
NATURAL JOIN (⋈)

• Natural join can only be performed if there is a


common attribute (column) between the
relations. The name and type of the attribute
must be same.
Left Outer Join(A B)

• In the left outer join, operation allows keeping


all tuple in the left relation. However, if there
is no matching tuple is found in right relation,
then the attributes of right relation in the join
result are filled with null values.
Right Outer Join: ( A B)

• In the right outer join, operation allows


keeping all tuple in the right relation.
However, if there is no matching tuple is found
in the left relation, then the attributes of the
left relation in the join result are filled with
null values.
• Note-Do the example as a class work
Translating SQL Queries into Relational Algebra and
Other Operators

• SQL
– Query language used in most RDBMSs
• Query decomposed into query blocks
– Basic units that can be translated into the
algebraic operators
– Contains single SELECT-FROM-WHERE expression
• May contain GROUP BY and HAVING clauses
Translating SQL Queries (cont’d.)
• Example:

– Inner block

– Outer block
Translating SQL Queries (cont’d.)
• Example (cont’d.)
– Inner block translated into:

– Outer block translated into:


Click to add text

• Query optimizer chooses execution plan for


each query block
Class work
Outline
1 Introduction to Transaction Processing
2 Transaction and System Concepts
3 Desirable Properties of Transactions
4 Characterizing Schedules based on
Recoverability
5 Characterizing Schedules based on
Serializability
1 Introduction to Transaction Processing (1)

• Single-User System:
– At most one user at a time can use the system.
• Multiuser System:
– Many users can access the system concurrently.
• Concurrency
– Interleaved processing:
• Concurrent execution of processes is interleaved in a
single CPU
– Parallel processing:
• Processes are concurrently executed in multiple CPUs.
Introduction to Transaction Processing (2)

• A Transaction:
– Logical unit of database processing that includes one or more
access operations (read -retrieval, write - insert or update, delete).
• A transaction (set of operations) may be stand-alone specified
in a high level language like SQL submitted interactively, or
may be embedded within a program.
• After a transaction is committed, database reaches to a new
consistent state.
• It may be rolled back or undone.
• Transaction boundaries:
– Begin and End transaction.
• An application program may contain several transactions
separated by the Begin and End transaction boundaries.
Introduction to Transaction Processing (3)

SIMPLE MODEL OF A DATABASE (for purposes of discussing


transactions):
• A database is a collection of named data items
• Basic operations are read and write
– read_item(X): Reads a database item named X into a
program variable. To simplify our notation, we assume that
the program variable is also named X.
– write_item(X): Writes the value of program variable X into
the database item named X.
2 Transaction and System Concepts (1)

• A transaction is an atomic unit of work that is


either completed in its entirety or not done at
all.
– For recovery purposes, the system needs to keep
track of when the transaction starts, terminates, and
commits or aborts.
• Transaction states:
– Active state
– Partially committed state
– Committed state
– Failed state
– Terminated State
State transition diagram illustrating the
states for transaction execution
Transaction and System Concepts (2)

• Recovery manager keeps track of the following operations:


– begin_transaction: This marks the beginning of transaction
execution.
– read or write: These specify read or write operations on the
database items that are executed as part of a transaction.
– end_transaction: This specifies that read and write transaction
operations have ended and marks the end limit of transaction
execution.
• At this point it may be necessary to check whether the changes
introduced by the transaction can be permanently applied to the
database or whether the transaction has to be aborted because it
violates concurrency control or for some other reason.
Transaction and System Concepts (3)

• Recovery manager keeps track of the following


operations (cont):
– commit_transaction: This signals a successful end
of the transaction so that any changes (updates)
executed by the transaction can be safely
committed to the database and will not be undone.
– rollback (or abort): This signals that the transaction
has ended unsuccessfully, so that any changes or
effects that the transaction may have applied to
the database must be undone.
Transaction and System Concepts (6)

• The System Log


– Log or Journal: The log keeps track of all
transaction operations that affect the values of
database items.
• This information may be needed to permit recovery
from transaction failures.
• The log is kept on disk, so it is not affected by any type
of failure except for disk or catastrophic failure.
• In addition, the log is periodically backed up to archival
storage (tape) to guard against such catastrophic
failures.
3 Desirable Properties of Transactions (1)

ACID properties:
• Atomicity: A transaction is an atomic unit of processing; it is either
performed in its entirety or not performed at all.
• Consistency preservation: A correct execution of the transaction must
take the database from one consistent state to another.
• Isolation: A transaction should not make its updates visible to other
transactions until it is committed; this property, when enforced strictly,
solves the temporary update problem and makes cascading rollbacks of
transactions unnecessary (see Chapter 21).
• Durability or permanency: Once a transaction changes the database and
the changes are committed, these changes must never be lost because of
subsequent failure.
4 Characterizing Schedules based on
Recoverability (1)
• Transaction schedule or history:
– When transactions are executing concurrently in an interleaved
fashion, the order of execution of operations from the various
transactions forms what is known as a transaction schedule (or
history).
• A schedule (or history) S of n transactions T1, T2, …, Tn:
– It is an ordering of the operations of the transactions subject to the
constraint that, for each transaction Ti that participates in S, the
operations of T1 in S must appear in the same order in which they
occur in T1.
– Note, however, that operations from other transactions Tj can be
interleaved with the operations of Ti in S.
5 Characterizing Schedules based on
Serializability (1)
• Serial schedule:
– A schedule S is serial if, for every transaction T
participating in the schedule, all the operations of
T are executed consecutively in the schedule.(ie.
No interleaved schedule)
• Otherwise, the schedule is called nonserial schedule.
(ie. Schedule is interleaved)
• Serializable schedule:
– A schedule S is serializable if it is equivalent to
some serial schedule of the same n transactions.
Testing of Serializability
• There will be a directed graph(N,E) constructed with the
folloing serializability
• Create a node for each transaction
• Create a directed edge from Ti -> Tj, if Tj reads the value of an
item written by Ti
• Create a directed edge from Ti -> Tj,if Tj writes the value of an
item after Ti reads it.
• Create a directed edge from Ti -> Tj, if Tj writes a value of an
item after Ti wrote it
• Ti(write(x))->Tj(read(x))
• Ti(R/W(x)-> Tj(W(X))
Example of Serializability Testing
Another Example of Serializability Testing

You might also like