CSIS 3714 - MasterKick
CSIS 3714 - MasterKick
Thabo
“Do” or “die”
CHAPTER 12
What is a transaction?
• Most real-world database transactions are formed by two or more database requests
Transaction properties:
• Atomicity
• Consistency
• Isolation
– Data used during transaction cannot be used by second transaction until the first is completed
• Durability
• Serializability
– All changes are aborted and the database is rolled back to its previous state
– Equivalent to a COMMIT
– Equivalent to a ROLLBACK
• A DBMS uses a transaction log to keep track of all transactions that update the database
• Information is used for recovery triggered by a ROLLBACK, abnormal termination or system failure
• Some RDBMs use the log to recover a database forward to a consistent state
Concurrency Control
– Lost updates
– Uncommitted data
– Inconsistent retrievals
Uncommitted Data
– First transaction rolled back after second already accessed uncommitted data
Inconsistent Retrievals
• Inconsistent retrievals:
• Transaction might read some data before they are changed and other data after changed
– Purpose is to establish order of operations within which concurrent transactions are executed
– Ensures serializability
– Ensures isolation
• Serializable schedule
• Lock
– Pessimistic locking
• Use of locks based on the assumption that conflict between transactions is likely
– Lock manager
– Database
– Table
– Page
– Row
– Field (attribute)
• Database-level lock
• Table-level lock
• Page-level lock
– Table can span several pages and a page can contain several rows of one or more tables
• Row-level lock
• Field-level lock
Lock Types
• Binary lock
• Exclusive lock
• Shared lock
• Exclusive lock
– Issued when a transaction wants to update a data item and there is no current lock on the item
– Mutual exclusion rule: only one transaction at a time can hold an exclusive lock on the same object
• Shared lock
– Issued when a transaction wants to read data from the database and there is no exclusive lock on
the data item
– Growing phase
– Shrinking phase
• Transaction releases all locks and cannot obtain any new lock
• Condition that occurs when two transactions wait for each other to unlock data
• Possible only if one of the transactions wants to obtain an exclusive lock on a data item
– Prevention
• A transaction requesting a lock is aborted when there is a possibility that a deadlock can
occur
– Detection
• DBMS periodically tests for deadlocks. If a deadlock is detected, one of the transactions is
aborted and the other continues
– Avoidance
• Time stamps
– Uniqueness
– Monotonicity
• Wait/die
– Older transaction requests a lock, it will wait (wait) until the other transaction is completed and the
lock is released
– Younger transaction requesting a lock, it will die (rolled back) and be rescheduled
• Wound/wait
– Older transaction requests a lock, is will preempt (wound) the younger transaction by rolling it back
– Younger transaction requesting a lock, it will wait until the other transaction is completed and locks
are released
• Optimistic approach
• Optimistic approach
– Read
• Transaction reads the database, executes the needed computations and makes the updates
to a private copy of the database values
– Validation
• Transaction is validated to ensure changes made will not affect the integrity and consistency
of the database
– Write
– Transaction aborted
• Levels of backup
– Full
– Differential
– Transaction log
Transaction Recovery
• Write-ahead-log protocol: ensures transaction logs are written before data is updated
• Redundant transaction logs: ensure physical disk failure will not impair ability to recover
• Checkpoints: operations in which DBMS writes all its updated buffers to disk
• Deferred-write technique
• Do nothing
• Do nothing
• Write-through technique
• Do nothing
– The DBMS sends the resulting data set to the end user
– Set of activities and procedures designed to reduce response time of database system
• Good database performance starts with good database design – no amount of fine-tuning will make a poorly
designed database perform as well as a well-designed database
• Client side
– Generate SQL query that returns correct answer in least amount of time
• Server side
DBMS Architecture
• Data files
• SQL cache or procedure cache: stores most recently executed SQL statements
• Most of the algorithms for query optimization are based on two principles:
– DBMS finds the most cost-effective access path without user intervention
– Requires that the optimization be selected and scheduled by the end user or programmer
• Query optimization algorithms can be classified according to when the optimization is done:
• Takes place at compilation time (when the query is compiled by the DBMS)
• Takes place at execution time (Database access strategy is defined when the program is
executed)
– Dynamic query optimization is efficient but its cost is measured by run-time processing overhead
– The best strategy is determined every time the query is executed, which could happen several times
in the same program
– Based on a set of user-defined rules to determine the best query access strategy
Database Statistics
– Tables
– Indexes
– Processor speed
– Parsing
• Transform original SQL query into slightly different version of original SQL code that is fully equivalent but
more efficient
• Statistics are used to determine the most efficient way to execute the query
• Query optimizer analyses SQL query and finds most efficient way to access data
• Validated against data dictionary: Tables and column names are correct, User has proper access rights
• Once the SQL statement is transformed, the DBMS creates an access or execution plan
• DBMS checks if access plan already exists for query in SQL cache. DBMS reuses the access plan to save
time. If not, optimizer evaluates various plans
– Execution
• Locks acquired
– Fetching
• All rows matching the specified condition are retrieved, sorted, grouped and/or aggregated
• Delay introduced in the processing of an I/O operation that slows the system
– CPU
– RAM
– Hard disk
– Network
– Application code
• Indexes
– Facilitate searching, sorting, and using aggregate functions as well as join operations
More efficient to use index to access table than to scan all rows in table sequentially
– Hash indexes
– B-tree indexes
– Bitmap indexes
Optimizer Choices
• Rule-based optimizer
• Cost-based optimizer
– Adds up processing cost, I/O costs, resource costs to derive total cost
• Optimizer hints: special instructions for the optimizer embedded in the SQL command text
SQL Performance Tuning
– Most current relational DBMSs perform automatic query optimization at the server end
• Rarely portable
Index Selectivity
– Create indexes for each attribute in WHERE, HAVING, ORDER BY, or GROUP BY clause
– Declare primary and foreign keys so optimizer can use indexes in join operations
• Includes managing DBMS processes in primary memory and structures in physical storage
• DBMS performance tuning at server end focuses on setting parameters used for the:
– Data cache
– SQL cache
– Sort cache
– Use RAID (Redundant Array of Independent Disks) to provide balance between performance and
fault tolerance
– Minimize disk contention: use multiple, independent storage volumes with independent spindles to
minimize hard disk cycles
– Assign separate data files in separate storage volumes for indexes, system, and high-usage tables
– Take advantage of table storage organizations in database, for example use the index-organized
tables
• A DDBMS governs the storage and processing of logically related data over interconnected computer
systems in which both data and processing functions are distributed among several sites.
• Centralized database
• required that corporate data be stored in a single central site, usually a mainframe or midrange
computer
• Fell short when quickly moving events required faster response times and equally quick access to
information.
• Quick unstructured access to databases, using ad hoc queries to generate on-the-spot information was
required.
• Database management systems based on the relational model could provide the environment in which
unstructured information needs would be met by employing ad hoc queries.
• Over the last two decades there has been a series of crucial social and technological changes that affected
database development and design:
• Low-cost, smart mobile devices increased the demand for complex and fast networks to
interconnect them
• Applications must manage multiple types of data, such as voice, video, music and images
• As large business units restructured in response, two database requirements became obvious:
• Rapid ad-hoc data access was crucial in the quick-response decision-making environment
• The way these factors were addressed was strongly influenced by:
• High costs associated with maintaining and operating central database systems
• Scalability problems associated with physical limits of single site, eg space, power consumption
• Organizational rigidity: the database might not support the flexibility required by modern global
organizations
DDBMS Advantages
• Faster data access as end users often only work with a locally stored subset
• Growth facilitation: new sites can be added without affecting the operations of other sites
• Improved communications: local sites are smaller and situated closed to the customer base
• Reduced operating costs: more cost-effective to add workstations to a network than to update a mainframe
• User friendly interface as PCs and workstations are usually equipped with easy to use GUIs
• Less danger of a single-point failure: When one of the computers fails, the workload is picked up by other
workstations
• Processor independence: the end user is able to access any available copy of the data, and the request is
processed by any processor at the data location.
DDBMS Disadvantages
• Complexity of management and control: applications must recognize data location and be able to merge
data from different sites. Transaction management, concurrency control, security etc must all be addressed
• Security: the probability of security lapses increases when data are located at multiple sites and
responsibility is shared at different sites
• Lack of standards since there are no standard communication protocols at the database level (although
TCP/IP is the de facto standard)
• Increased storage requirements: Multiple copies of data are stored at different sites, thus requiring more
storage space
• Increased training cost: training costs are usually higher than with a centralized database
• Costs: distributed solutions require duplicated infrastructure, such as physical location, personnel, software
and licensing
• In distributed processing, a database’s logical processing is shared among two or more physically
independent sites that are connected through a network.
• A distributed database, stores a logically related database over two or more physically independent sites.
• Both distributed processing and distributed databases require a network of interconnected components
Characteristics OF DDBMS
– Transformation to determine which data request components are distributed and which are local
– Backup and recovery to ensure the availability and recoverability of the database in case of a failure
– DB administration features
– Concurrency control to manage simultaneous data access and ensure data consistency across data
fragments
– Transaction management to ensure the data moves from one consistent state to another
• A fully distributed database management system must perform all the functions of a centralized DBMS:
• In addition, a DDBMS must handle the necessary functions imposed by the distribution of data and
processing
DDBMS COMPONENTS
• Computer workstations that form the network system. The distributed database system must be
independent of the computer system hardware
• Network hardware and software components that reside in each workstation. The network components
allow all sites to interact and exchange data
• Communications media that carry the data from one workstation to another. It must be able to support
several types of communication media.
• The transaction processor (TP) which is the software component found in each computer that requests data.
It receives and processes the application’s data requests.
• The data processor (DP) which is the software component residing on each computer that stores and
retrieves data located at the site.
• Interface with the network to transport data and commands between the DPs
• Synchronize all data received from the DPs and route retrieved data to the TPs
• Ensure common database functions in a distributed system, such as security, concurrency control,
backup and recovery
• Database systems are classified on the basis of how process distribution and data distribution are supported
– The DBMS is situated on the host computer and dumb terminals connect to it
– The functions of the TP and the DP are embedded within the DBMS
– DBMS usually runs under a time-sharing, multitasking operating system, which allows several
processes to run simultaneously
– Typically there is a network file server running conventional applications that are accessed over LAN
– The TP on each workstation acts only as a redirector to route all network data requests to the file
server
– The end user sees the file server as just another hard disk
– The end user must make a direct reference to the file server in order to access remote data
• Multiple-site processing, single-site data (MPSD)
– All data selection, search and update functions take place at the workstation. This increases network
traffic, slows response time and increases communication costs
• All database processing is done at the server site, thus reducing network traffic
– Fully distributed DBMS with support for multiple data processors and transaction processors at
multiple sites
– Depending on the level of support for different types of centralized DBMSs, DDBMSs are classified as
either homogenous or heterogeneous
– Homogenous DDBMSs integrate only one type of centralized DBMS over a network
– Heterogeneous DDBMSs support different DBMSs that may even support different data models
• A distributed database system requires functional characteristics that can be grouped and described as
transparency features.
• The features allow the end user to feel like the database’s only user
• Failure transparency ensures that the system will continue to operate in the event of a node failure.
Functions that were lost because of the failure will be picked up by another network node
• Performance transparency allows the system to perform as if it were a centralized DBMS. This also
ensures that the system will find the most cost-effective path to access remote data
• Heterogeneity transparency allows the integration of several different local DBMSs under a common
schema. The DDBMS is responsible for translating the data requests from the global schema to the
local DBMS schema
• Distribution transparency allows a physically dispersed database to be managed as though it were a
centralized database.
• The level of transparency supported by the DDBMS varies from system to system.
Distribution transparency
• The level of transparency supported by the DDBMS varies from system to system.
• Fragmentation transparency is the highest level of transparency. The end user or programmer does
not need to know that a database is portioned. Therefore, neither fragment name nor locations are
specified prior to data access
• Location transparency exists when the end user or programmer must specify the database fragment
names but does not need to specify where those fragments are located
• Local mapping transparency exists when the end user or programmer must specify both the
fragment names and their locations
• Distribution transparency is supported by the distributed data dictionary (DDD) or a distributed data catalog
(DDC).
• The DDC contains the description of the entire database as seen by the database administrator.
• The database description, known as the distributed global schema, is the common schema used by local TPs
to translate user requests into subqueries that are processed by different DPs.
Transaction transparency
• Transaction transparency is a DDBMS property that ensures that database transactions maintain the
distributed database’s integrity and consistency.
• A transaction can update data stored on many different computers connected in a network.
• Transaction transparency ensures that the transactions are completed only when all database sites involved
in the transaction complete their part of the transaction.
• The basic difference between a non-distributed transaction and a distributed transaction is that the
latter can update or request data from several different remote sites on a network
• A remote request lets a single SQL statement access the data that are to be processed by a single
remote database processor, that is, the statement can reference data at only a single site
• A remote transaction composed of several requests, accesses data at a single remote site
• Each single request can reference only one local or remote DP site, but the transaction as a whole
can reference multiple DP sites
• In order for a request to be able to access data from more than one remote site, the DBMS must
support distributed requests
• A distributed request lets a single SQL statement reference data located at several different local or
remote DP sites.
• Multi-site, multi-process operations are more likely to create data inconsistencies and deadlocked
transactions than a single-site system
• If each transaction was committed by a local DP, but one of the DPs could not commit then the data would
not be consistent.
• A final commit must not be issued until all sites have committed their parts of the transaction
• The two-phase commit protocol guarantees that if a portion of a transaction operation cannot be committed,
all changes at all sites will be rolled back.
• The two-phase commit protocol requires that the transaction entry log for each DP be written before the
database fragment is actually updated.
• The two-phase commit protocol requires a DO-UNDO-REDO protocol and a write-ahead protocol
• The DO-UNDO-REDO protocol is used by the DP to roll back and/or roll forward transactions with the help of
the transaction log entries:
• DO performs the operation and records the before and after values in the transaction log
• UNDO reverses an operation, using the log entries written by the DO portion of the sequence
• REDO redoes an operation, using the log entries written by the DO portion of the sequence
• To ensure that the DO, UNDO and REDO operations can survive a crash while they are being executed, a
write-ahead protocol is used.
• The write-ahead protocol forces the log entry to be written to permanent storage before the actual
operation takes place.
• The two-phase commit protocol defines the operations between two types of nodes: the coordinator and
one of more subordinates.
• Phase 1: preparation
• The coordinator sends a PREPARE TO COMMIT message to all subordinates
• The subordinates receive the message, write the transaction log, using the write-ahead protocol,
and send an acknowledgement (YES or NO) message
• The coordinator makes sure that all nodes are ready to commit, or it aborts the action
• If one or more nodes reply NO or NOT PREPARED, the coordinator broadcasts an ABORT message to all
subordinates
• The coordinator broadcasts a COMMIT message to all subordinates and waits for the replies
• Each subordinate receives the COMMIT message, then updates the database using the DO protocol
• The subordinates reply with a COMMITTED or NOT COMMITTED message to the coordinator
• If one or more subordinates did not commit, the coordinator sends ABORT message, thereby forcing them to
UNDO all changes.
• The objective of the two-phase protocol is to ensure that all nodes commit their part of the transaction;
otherwise the transaction is aborted
• If one of the nodes fails to commit, the information required to recover the database is in the transaction
log, and can be recovered using the DO-UNDO-REDO protocol
• Failure transparency ensures that the system will continue to operate in the case of a node or network
failure
• The objective of query optimization is to minimize the cost associated with the execution of a request. The
costs are a function of:
– Access (I/O) time cost involved in accessing the physical data stored on disk
– Communication cost associated with the transmission of data among nodes in distributed data
systems
– CPU time cost associated with the processing overhead of managing distributed transactions
• Resolving data requests in a distributed data environment must take the following points into consideration
– Data replication: the data may be replicated at several sites. The database must ensure that all
copies are consistent, which makes the access process more complex
– Network and node availability: response time can not be easily predetermined because some nodes
finish their part of the query in less time than others and network path performance varies.
• The design principles and concepts are still applicable to the design of a distributed database.
Data fragmentation
• Data fragmentation allows you to break a single object into two or more segments or fragments.
• Horizontal fragmentation
• Vertical fragmentation
– Each fragment has unique columns – with the exception of the key column
• Mixed fragmentation
– A table may be divided into several horizontal subsets, each one having a subset of the attributes
Horizontal fragmentation
– Round-robin partitioning
• A partition key is one or more attributes in a table that determine the fragment in which a
row will be stored.
• For example, for location awareness, a good partition key could be the state
Mixed fragmentation
– Horizontal fragmentation
Data replication
• Data replication refers to the storage of data copies at multiple sites served by a computer network
• Fragment copies can be stored at several sites to serve specific information requirements
• Data copies can help to reduce communication and total query costs
• The mutual consistency rule requires that all copies of data fragments be identical
• The DDBMS must ensure that a database update is performed at all sites where replicas exist
• Push replication
• Pull replication
• Push replication
• After a data update, the originating DP node sends the changes to the replica nodes to ensure that
data are immediately updated
• Decreases data availability due to the latency involved in ensuring data consistency
• Pull replication
• After a data update, the originating DP node sends the messages to the replica nodes to notify them
of the change
• If the database is fragmented, the DDBMS must decompose a query into subqueries to access appropriate
fragments
• If the database is replicated, the DDBMS must decide which copy to access. A READ operation selected the
nearest copy to satisfy the transaction. A WRITE operation requires that all copies be selected and updated
to satisfy the mutual consistency rule.
• The DP receives and executes each request and sends the data back to the TP.
• Unreplicated database
• Database size: the amount of data to be replicated impacts storage requirements and data
transmission costs
• Usage frequency: the frequency of data usage determines how frequently the data needs to be
updated.
• Costs: performance, software overhead and management associated with synchronizing transactions
and their components.
Data Allocation
– With centralized data allocation the entire database is stored at one site
– With partitioned data allocation the database is divided into several disjointed parts and stored at
several sites
– With replicated data allocation copies of one or more database fragments are stored at several sites
– Size, number of rows and number of relations that an entity maintains with other entities
– Types of transactions to be applied to the database and the attributes accessed by each of those
transactions
– Consistency
– Availability
– Partition tolerance
• Consistency
– All nodes should see the same data at the same time
• Availability
• Partition tolerance
– The system continues to operate even in the event of a node failure
Database security
• Password authentication is provided as is additional security, such as authentication through database links
• Cloud computing is a style of delivering applications, data and resources to users over the web
• Provides an alternative for organizations who do not wish to provide their own IT infrastructure to host
databases and software
• Cost effectiveness: only one infrastructure used to host services for multiple organizations, thus
reducing the cost for individual organizations
• Latest software: most cloud providers will ensure that their software is always the latest version
• Scalable architecture: it is easy to increase the database capacity and/or change the underlying
model
• Mobile access: data and software in the cloud can be accessed from anywhere
• Column stores
• For large scale distributed systems which store petrabytes of data across hundreds of servers. For
example, Google
• Document stores
• Move away from storing data in tables. Instead, each document is stored differently depending on
its size and format
• Failure independence
• Location transparency
• Fragmentation transparency
• Replication transparency
• Network independence
• Database independence