20cb402 Dbms Unit 3
20cb402 Dbms Unit 3
2
Please read this disclaimer before proceeding:
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document through
email in error, please notify the system manager. This document contains proprietary
information and is intended only to the respective group / learning community as
intended. If you are not the addressee you should not disseminate, distribute or
copy through e-mail. Please notify the sender immediately by e-mail if you have
received this document by mistake and delete this document from your system. If
you are not the intended recipient you are notified that disclosing, copying,
distributing or taking any action in reliance on the contents of this information is
strictly prohibited.
3
22CB304
Database Management Systems
Created by:
Date: 09.07.2024
Table of Contents
Sl. Topics Page
No. No.
1. Contents 5
2. Course Objectives 6
6. CO-PO/PSO Mapping 10
CO1: Able to design and deploy an efficient & scalable data storage node for varied kind of
application requirements
CO2: Map ER model to Relational model to perform database design effectively
CO3: Write queries using normalization criteria and optimize queries
CO4: Compare and contrast various indexing strategies in different database systems
CO5: Appraise how advanced databases differ from traditional databases.
CO-PO/PSO Mapping
POs/PSOs
PSO3
COs
PO2
PO3
PO1
PO4
PO5
PO6
PO7
PO8
PO9
PSO
PSO
PO1
PO1
PO1
2
0
2
CO1 3 3 3 - 1 - - - 2 - - 3 2 2 2
CO2 3 3 3 3 1 - - - - - - 3 2 2 3
CO3 3 3 3 3 2 - - - - - - 3 2 2 3
CO4 3 3 3 3 3 - - - - - - 3 2 2 3
CO5 3 3 3 3 2 - - - - - - 3 2 2 3
Unit III
QUERY PROCESSING
AND STORAGE
Lecture Plan
UNIT - II
Actual Date Mode of
S. Scheduled Taxono
Topic of CO Deliver
No. Date my Level
Completion y
Query
1 Processing CO3 PPT
Evaluation of PPT
2 CO3
relational algebra
expressions
13
8. ACTIVITY BASED LEARNING
ACROSS:
2. Management Studio
5. The database that is used as a template when creating a new database
6. Using a backup
7. Reporting Services
9. An index that rearranges the structure of a table
12. The underlying structure of an index
13. Common Table Expression
20. Making queries run faster
21. An index that doesn’t change the structure of the table
22. The often misunderstood go-faster switch
Down
1. A data type to hold international strings
2. A type of join where the left and right tables are the same table
3. The option used in a CTE that needs to recurse more than 100 levels
4. A type of join that produces a Cartesian product
8. A derived table
10. Not the consistency checker
11. The concept of everything a query needs being available in the index
14. Sometimes called the upsert
15. High Availability
16. Binary large object
17. Transact SQL
18. the two transactions collide
19. Codename for SQL Server 2012
Print the crossword puzzle and fill it in on paper.
14
3.1. QUERY PROCESSING
Definition:
Query processing refers to the range of activities involved in extracting data from a
database.
Evaluation
The parser checks the query syntax to determine whether it is formulated according to
the syntax rules (rules of grammar) of the query language.
15
The query must also be validated by checking that all attribute and relation
names are valid and semantically meaningful names in the schema of the
particular database being queried
It is also possible to represent the query using a graph data structure called a
query graph.
16
Parsing and translation
Translate the query into its internal form. This is then translated into
relational algebra
Evaluation
The query-execution engine takes a query-evaluation plan, executes that
Optimization
(balance 2500(account))
Query Optimization:
Amongst all equivalent evaluation plans choose the one with lowest cost.
17
Measures of Query Cost
Cost is generally measured as total elapsed time for answering query Many
factors contribute to time cost - disk accesses, CPU, or even network
communication
Typically disk access is the predominant cost, and is also relatively easy to
estimate.
For si mplicity we just use the number of block transfers from dis k and the number
of seeks as the cost measures
b * tT + S * tS
Cost to write a block is greater than cost to read a block data is read back after
being written to ensure that the write was successful.
For simplicity we just use number of block transfers from disk as the cost
measure.
Ignore the difference in cost between sequential and random I/O for simplicity
ignore CPU costs for simplicity.
18
3.2 Evaluation of relational algebra expressions
1. Materialization
2. Pipelining
building"Watson" (department)
then compute the store its join with instructor, and finally compute the projection on
name.
19
Pipelined evaluation : evaluate several operations simultaneously, passing the
results of one operation on to the next.
E.g., in previous expression tree, do not store result of
building"Watson" (department)
instead, pass tuples directly to the join.. Similarly, don’t store result of join,
pass tuples directly to projection.
Much cheaper than materialization: no need to store a temporary relation to disk.
Pipelining may not always be possible – e.g., sort, hash-join.
For pipelining to be effective, use evaluation algorithms that generate output tuples
even as tuples are received for inputs to the operation.
Pipelines can be executed in two ways: demand driven and producer driven
Demand driven or lazy evaluation
system repeatedly requests next tuple from top level operation
Each operation requests next tuple from children operations as required, in
order to output its next tuple
In between calls, operation has to maintain “state” so it knows what to
return next
Producer-driven or eager pipelining
Operators produce tuples eagerly and pass them up to their parents
Buffer maintained between operators, child puts tuples in buffer,
parent removes tuples from buffer
if buffer is full, child waits till there is space in the buffer, and then
generates more tuples
System schedules operations that have space in output buffer and can
process more input tuples
Alternative name: pull and push models of pipelining
20
Implementation of demand-driven pipelining
Each operation is implemented as an iterator implementing the following
operations
open()
E.g. file scan: initialize file scan
state: pointer to beginning of file
E.g.merge join: sort relations;
state: pointers to beginning of sorted relations
next()
E.g. for file scan: Output next tuple, and advance and store file
pointer
E.g. for merge join: continue with merge from earlier state till
next output tuple is found. Save pointers as iterator state.
close()
21
3.2 Query equivalence
Cost difference between evaluation plans for a query can be enormous
E.g. seconds vs. days in some cases
Steps in cost-based query optimization
1. Generate logically equivalent expressions using equivalence rules
2. Annotate resultant expressions to get alternative query plans
3. Choose the cheapest plan based on estimated cost
Estimation of plan cost based on:
Statistical information about relations. Examples: number of tuples, number
of distinct values for an attribute
Statistics estimation for intermediate results to compute cost of complex
expressions
Cost formulae for algorithms, computed using statistics
s q (s q ( E )) = s q (s q ( E ))
1 2 2 1
3. Only the last in a sequence of projection operations is needed, the others can
be omitted.
L1 ( L2 ( ( Ln ( E )) )) L1 ( E )
(b) When 1 involves only the attributes of E1 and 2 involves only the attributes
of E2.
1 E1 E2) = (1(E1)) ( (E2))
L1 L2 ( E1 E2 ) ( L1 ( E1 )) ( L2 ( E2 ))
(b) 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.
Joins:
Outer join:
The outer join operation is an extension of the join
operation to deal with missinginformation.
Example:Consider the relations employee and ft-works as below.
To generate single relation from the above two relations, a possible approach to use is
the natural join operation. The expression is given below.
Employee ft-works.
Left outer Join (ii) Right outer Join (iii) Full outer Join
(i)The left outer join: This takes all tuples in the left relation that did not match
with any tuple in the right relation, pads the tuples with null values for all other
attributes from the right relation, and adds them to the result of the natural join.
The result of employee ft-works is given below
(ii) The right outer join: it is symmetric with the left outer join. It pads tuples
from the right relation that did not match any from the left relation with nulls and
adds them to the result of the natural join. The result of employee ft-works is
given below.
(iii) The full outer join: it does both of the above operations, padding tuples from
the left relation that did not match any from the right relation, as well as tuples
from the right relation that did not match any from the left relation, and adding
them to the result of the join. The below relation shows the result of employee
ft-works.
3.3.1 IMPLEMENTING THE JOIN OPERATION
Many of the join operations encountered in queries are of the EQUIJOIN and NATURAL
JOIN.
Joins involving more than two files are called multiway joins. The number of
possible ways to execute multiway joins grows very rapidly.
Methods for Implementing Joins:
records)
If an index (or hash key) exists for one of the two join attributes, attribute B of file
S- retrieve each record t in R (loop over file R), and then use the access structure to
retrieve directly all matching records s from S that satisfy s [B ] = t [ A ].
J3 - Sort-merge join
If the records of R and S are physically sorted (ordered) by value of the join attributes
A and B, respectively, we can implement the join in the most efficient way
possible.
Both files are scanned concurrently in order of the join attributes, matching the
records that have the same values for A and B. If the files are not sorted, they may
be sorted first by using external sorting
J4 - Partition-hash join
The records of files R and S are partitioned into smaller files. The partitioning of
each file is done using the same hashing function h on the join attribute A of R
(for partitioning file R) and B of S (for partitioning file S).
First, a single pass through the file with fewer records (say, R) hashes its records
to the various partitions of R; this is called the partitioning phase, since the
records of R are partitioned into the hash buckets.
The collection of records with the same value of h(A) are placed in the same
partition, which is a hash bucket in a hash table in main memory. In the second
phase, called the probing phase, a single pass through the other file (S) then
hashes each of its records using the same hash function h(B) to probe the
appropriate bucket, and that record is combined with all matching records from R
in that bucket.
HYBRID HASH-JOIN
The hybrid hash-join algorithm is a variation of partition hash-join, where the
joining phase for one of the partitions is included in the partitioning phase.
To illustrate this, let us assume that the size of a memory buffer is one disk block;
that nB such buffers are available; and that the partitioning hash function used is
h(K) = K mod M, so that M partitions are being created, where M < nB.
This simplified description of partition-hash join assumes that the smaller of the
two files fits entirely into memory buckets after the first phase.
30
3.4 HEURISTICS IN QUERY OPTIMIZATION
A query tree is used to represent a relational algebra or extended relational
algebra expression, whereas
The scanner and parser of an SQL query first generate a data structure that
corresponds to an initial query representation, which is then optimized according
to heuristic rules.
One of the main heuristic rules is to apply SELECT and PROJECT operations
before applying the JOIN or other binary operations, because the size
of the file resulting from a binary operation - such as JOIN - is
usually a multiplicative function of the sizes of the input files.
The SELECT and PROJECT operations reduce the size of a file and hence
should be applied before a join or other binary operation.
A query tree is a tree data structure that corresponds to a relational algebra expression.
It represents the input relations of the query as leaf nodes of the tree, and represents
the relational algebra operations as internal nodes.
31
An execution of the query tree consists of executing an internal node operation
whenever its operands are available and then replacing that internal node by the
relation that results from executing the operation. The order of execution of
operations starts at the leaf nodes, which represents the input database relations
for the query, and ends at the root node, which represents the final operation of the
query.
The execution terminates when the root node operation is executed and produces
the result relation for the query.
From Figure:
a. query tree corresponding to relational algebra for the query Q2
When this query tree is executed, the node marked (1) in Figure must begin execution
before node (2) because some resulting tuples of operation (1) must be available before
we can begin executing operation (2).
The query tree represents a specific order of operations for executing a query.
A more neutral data structure for representation of a query is the query graph
notation.
Relations in the query are represented by relation nodes, which are displayed as single
circles. Constant values, typically from the query selection conditions, are represented by
constant nodes, which are displayed as double circles or ovals. Selection and join
conditions are represented by the graph edges.
32
Fig: Notation for Query Trees and Query Graphs
The heuristic query optimizer will transform this initial query tree into an
equivalent final query tree that is efficient to execute.
The optimizer must include rules for equivalence among relational algebra
expressions that can be applied to transform the initial tree into the final,
optimized query tree.
33
Steps in converting a query tree during heuristic optimization
a. Initial (canonical) query tree for SQL query Q.
a. The initial query tree for Q is shown below. Executing this tree directly first
creates a very large file containing the CARTESIAN PRODUCT of the entire
EMPLOYEE, WORKS_ON, and PROJECT files. That is why the initial query tree is
never executed, but is transformed into another equivalent tree that is efficient
to execute.
34
Fig: CartesianProduct
This uses the information that Pnumber is a key attribute of the PROJECT relation,
and hence the SELECT operation on the PROJECT relation will retrieve a single record
only
35
d ) We can further improve the query tree by replacing any CARTESIAN
PRODUCT operation that is followed by a join condition with a
JOIN operation
This reduces the attributes (columns) of the intermediate relations, whereas the
SELECT operations reduce the number of tuples (records).
36
General Transformation Rules for Relational Algebra Operations.
There are many rules for transforming relational algebra operations into
equivalent ones. For query optimization purposes, we are interested in the
meaning of the operations and the resulting relations.
Hence, if two relations have the same set of attributes in a different order but the
two relations represent the same information, we consider the relations to be
equivalent.
Transformation rules that are useful in query optimization, without proving them:
37
38
Converting Query Trees into Query Execution Plans:
39
To convert this into an execution plan, the optimizer might choose an index
search for the SELECT operation on DEPARTMENT (assuming one exists), a single-
loop join algorithm that loops over the records in the result of the SELECT
operation on DEPARTMENT for the join operation (assuming an index exists on
the Dno attribute of EMPLOYEE), and a scan of the JOIN result for input to the
PROJECT operator.
Additionally, the approach taken for executing the query may specify a
materialized or a pipelined evaluation, although in general a pipelined evaluation
is preferred whenever feasible.
For instance, the JOIN operation can be computed and the entire result stored as
a temporary relation, which is then read as input by the algorithm that computes
the PROJECT operation, which would produce the query result table.
40
3.5 COST OPTIMIZATION IN QUERY OPTIMIZATION
A query optimizer does not depend solely on heuristic rules; it also estimates and
compares the costs of executing a query using different execution strategies and
algorithms, and it then chooses the strategy with the lowest cost estimate.
Hence, this approach is more suitable for compiled queries where the
optimization is done at compile time and the resulting execution strategy code is
stored and executed directly at runtime.
For interpreted queries, where the entire process shown in occurs at runtime, a
full-scale optimization may slow down the response time. A more elaborate
optimization is indicated for compiled queries, whereas a partial, less time-
consuming optimization works best for interpreted queries.
The cost functions used in query optimization are estimates and not exact cost
functions, so the optimization may select a query execution strategy that is not
the optimal (absolute best) one.
41
Cost Components for Query Execution:
This is also known as disk I/O (input/output) cost. The cost of searching for
records in a disk file depends on the type of access structures on that file, such as
ordering, hashing, and primary or secondary indexes.
In addition, factors such as whether the file blocks are allocated contiguously on
the same disk cylinder or scattered on the disk affect the access cost.
3. COMPUTATION COST
This is the cost of performing in-memory operations on the records within the
data buffers during query execution. Such operations include searching for and
sorting records, merging records for a join or a sort operation, and performing
computations on field values.
42
5. COMMUNICATION COST
This is the cost of shipping the query and its results from the database site to the
site or terminal where the query originated.
In distributed databases, it would also include the cost of transferring tables and
results among various computers during query evaluation.
Simple cost functions ignore other factors and compare different query execution
strategies in terms of the number of block transfers between disk and main
memory buffers.
For smaller databases, where most of the data in the files involved in the query
can be completely stored in memory, the emphasis is on minimizing computation
cost. In distributed databases, where many sites are involved, communication
cost must be minimized also.
This information may be stored in the DBMS catalog, where it is accessed by the
query optimizer.
First, we must know the size of each file. For a file whose records are all of the
same type, the number of records (tuples) (r), the (average) record size
(R), and the number of file blocks (b) (or close estimates of them) are
needed. The blocking factor (bfr) for the file may also be needed.
43
We must also keep track of the primary file organization for each file.
The primary file organization records may be unordered, ordered by an attribute
with or without a primary or clustering index, or hashed (static hashing or one of
the dynamic hashing methods) on a key attribute. Information is also kept on all
primary, secondary, or clustering indexes and their indexing attributes.
For a non key attribute, by making an assumption that the d distinct values are
uniformly distributed among the records, we estimate sl = (1/d) and so
s = (r/d).
44
Cost Functions for SELECT
We now give cost functions for the selection algorithms S1 to S8 in terms of number of
block transfers between memory and disk.
These cost functions are estimates that ignore computation time, storage cost, and
other factors.
We search all the file blocks to retrieve all records satisfying the selection
condition; hence, CS1a = b.
For an equality condition on a key attribute, only half the file blocks are searched
on the average before finding the record, so a rough estimate for
if the record is found; if no record is found that satisfies the condition, CS1b = b.
S2 - Binary Search
For a primary index, retrieve one disk block at each index level, plus one disk block
from the data file. Hence, the cost is one more disk block than the number of
index levels: CS3a = x + 1.
45
S3b - Using a hash key to retrieve a single record
For hashing, only one disk block needs to be accessed in most cases. The
cost function is approximately CS3b = 1 for static hashing or linear hashing,
and it is 2 disk block accesses for extendible hashing
If the comparison condition is >, >=, <, or <= on a key field with an
ordering index, roughly half the file records will satisfy the condition.
This gives a cost function of CS4 = x + (b/2). This is a very rough estimate,
and although it may be correct on the average, it may be quite inaccurate in
individual cases. A more accurate estimate is possible if the distribution of
records is stored in a histogram.
This means that 𝖥(s/bfr)⎤ file blocks will be in the cluster of file blocks that
hold all the selected records, giving CS5 = x +[(s/bfr)].
46
If the index is non clustering, each of the records may
reside on a different disk block, so the (worst case) cost
estimate is CS6a = x + 1 + s.
If the comparison condition is >, >=, <, or <= and half the file
records are assumed to satisfy the condition, then (very roughly) half
the first-level index blocks are accessed, plus half the file records via
the index.
S7 - Conjunctive selection
We can use either S1 or one of the methods S2 to S6 discussed above.
In the latter case, we use one condition to retrieve the records and
then check in the main memory buffers whether each retrieved record
satisfies the remaining conditions in the conjunction.
If multiple indexes exist, the search of each index can produce a set of
record pointers (record ids) in the main memory buffers.
47
J1 - Nested-loop Join:
If nB main memory buffers are available to perform the join, the cost formula
becomes:
J2 - Single-loop join:
If an index exists for the join attribute B of S with index levels xB, we can retrieve
each record s in R and then use the index to retrieve all the matching records
t from S that satisfy t[B] = s[A]. The cost depends on the type of index.
J3 - Sort-merge join
If the files are already sorted on the join attributes, the cost function for this method
Is,
J4: Partition-hashjoin
48
3.6 INDEXING TECHNIQUES:
Introduction:
Database system indices play the same role as book indices or card catalogs in
the libraries. For example, to retrieve an account record given the account
number, the database system would look up an index to find on which disk block
the corresponding record resides, and then fetch the disk block, to get the
account record.
Ordered indices:
Hash indices:
Based on a uniform distribution of values across a range of buckets. The bucket
to which a value is assigned is determined by a function called a hash function.
Several techniques exist for both ordered indexing and hashing. No one technique is
the best. Rather, each technique is best suited to particular database applications.
Access types:
Access types can include finding records with a specified attribute value and
finding records, whose attribute values fall in a specified range.
Access time:
The time it takes to find a particular data item, or set of items using the technique in
question.
49
Insertion time:
Deletion time:
Space overhead:
Search Key :
ORDERED INDICES
To gain fast random access to records in a file, an index structure is used. Each
index structure is associated with a particular search key. Just like the index of a
book or a library catalog an ordered index stores the values of the search keys in
sorted order, and associates with each search key the records that contain it.
Ordered indices can be categorized as primary index and secondary index.
Primary indices are also called clustering indices. The search key of a primary
index is usually the primary key, although that is not necessarily so.
Indices whose search key specifies an order different from the sequential order of
the file are called secondary indices, or non clustering indices.
50
PRIMARY INDEX
In this index, it is assumed that all files are ordered sequentially on some search
key. Such files, with a primary index on the search key, are called index-sequential
files. They represent one of the oldest index schemes used in database systems.
They are designed for applications that require both sequential processing of the
entire file and random access to individual records.
The Figure show a sequential file of account records taken from the banking
example. In the example figure, the records are stored in search-key order, with
branch-name used as the search key.
51
Two types of ordered: Dense and Sparse Indices
DENSE INDEX
Dense index: an index record appears for every search-key value in the file. In a
dense primary index, the index record contains the search-key value and a pointer
to the first data record with that search-key value.
Implementations may store a list of pointers to all records with the same search-
key value; doing so is not essential for primary indices. The below figure, show
the dense index for the account file.
SPARSE INDEX:
An index record appears for only some of the search-key values. To locate a
record we find the index entry with the largest search-key value that is less than
or equal to the search key value for which we are looking. We start at the record
pointed to by that index entry, and follow the pointers in the file until we find the
desired record.
52
The below figure show the sparse index for the account file.
Figure:Sparse Index
Suppose that we are looking up records for the Perryridge branch. Using the dense
index we follow the pointer directly to the first Perryridge record. We process this
record and follow the pointer in that record to locate the next record in search-key
for a branch other than Perryridge. If we are using the sparse index, we do not find
an index entry for "Perryridge". Since, the last entry (in alphabetic order) before
"Perryridge" is "Mianus" we follow that pointer. We then read the account file in
sequential order until we find the first Perryridge record, and begin processing at
that point.
Thus, it is generally faster to locate a record in a dense index; rather than a sparse
index. However, sparse indices have advantages over dense indices in that they
require less space and they impose less maintenance overhead for insertions and
deletions.
There is a trade-off that the system designer must make between access time and
space overhead. Although the decision regarding this trade-off depends on the spe-
cific application, a good compromise is to have a sparse index with one index entry
per block. The reason this design is a good trade-off is that the dominant cost in
processing a database request is the time that it takes to bring a block from disk into
main memory. Once we have brought in the block, the time to scan the entire block
is negligible. Using this sparse index, we locate the block containing the record that
53
we are seeking. Thus, unless the record is on an overflow block, we minimize block
with 10 records stored in each block. If we have one index record per block, the
index has 10,000 records. Index records are smaller than data records, so let us
assume that 100 index records fit on a block. Thus, our index occupies 100 blocks.
If an index is sufficiently small to be kept in main memory, the search time to find
an entry is low. However, if the index is so large that it must be kept on disk, a
search for an entry requires several disk block reads. Binary search can be used on
the index file to locate an entry, but the search still has a large cost. If overflow
blocks have been used, binary search will not be possible. In that case, a sequential
search is typically used, and that requires b block reads, which will take even longer.
To deal with this problem, we treat the index just as we would treat any other
sequential file, and construct a sparse index on the primary index, as in the below
figure To locate a record, we first use binary search on the outer index to find the
record for the largest search-key value less than or equal to the one that we desire.
The pointer points to a block of the inner index. We scan this block until we find the
record that has the largest search-key value less than or equal to the one that we
desire. The pointer in this record points to the block of the file that contains the
54
Figure: Two-level Sparse Index
Using the two levels of indexing, we have read only one index block, rather than
the seven we read with binary search, if we assume that the outer index is
already in main memory. If our file is extremely large, even the outer index may
grow too large to fit in main memory. In such a case, we can create yet another
level of index. Indices with two or more levels are called multilevel indices.
Searching for records with a multilevel index requires significantly fewer I/O
operations than does searching for records by binary search.
INDEX UPDATE
Regardless of what form of index is used, every index must be updated whenever
a record is either inserted into or deleted from the file. These are the algorithms
used for updating single level indices.
INSERTION:
First, the system performs a lookup using the search-key value that appears in
the record to be inserted. Again, the actions the system takes next depend on
whether the index is dense or sparse:
55
DENSE INDICES:
If the search-key value does not appear in the index, the system inserts an index
record with the search-key value in the index at the appropriate position.
Otherwise, the index record stores a pointer to only the first record with the
search-key value. The system then places the record being inserted after the
other records with the same search-key values.
SPARSE INDICES:
We assume that the index stores an entry for each block. If the system creates a
new block, it inserts the first search-key value (in search-key order) appearing in
the new block into the index. On the other hand, if the new record has the least
search-key value in its block, the system updates the index entry pointing to the
block; if not, the system makes no change to the index.
DELETION.
To delete a record, the system first looks up the record to be deleted. The actions
the system takes next depend on whether the index is dense or sparse.
DENSE INDICES:
1. If the deleted record was the only record with its particular search-key
value, then the system deletes the corresponding index record from the
index.
56
2. Otherwise the following actions are taken:
If the index record stores pointers to all records with the same search-
key value, the system deletes the pointer to the deleted record from
the index record.
Otherwise, the index record stores a pointer to only the first record with
the search-key value.
SPARSE INDICES:
1. If the index does not contain an index record with the search-key value of
the deleted record, nothing needs to be done to the index.
57
SECONDARY INDICES
Secondary indices must be dense, with an index entry for every search-key value,
and, a pointer to every record in the file. A primary index may be sparse, storing
only some of the search-key values, since it is always possible to find records with
intermediate, search-key values by a sequential access to a part of the file. If a
secondary index stores only some of the search-key values, records with
intermediate search-key values may be anywhere in the file and, in general, we
cannot find them without searching the entire file.
The pointers in such a secondary index do not point directly to the file. Instead,
each points to a bucket that contains pointers to the file. The below figure
shows the structure of a secondary index that uses an extra level of indirection on
the account file, on the search key balance.
SQL on INDEX:
Create an index
create index <index-name> on <relation-name> (<attribute-list>)
E.g.: create index b-index on branch(branch name)
58
3.7 B+ TREE AND B TREE
B+ Trees
Main disadvantage of index sequential file is that performance degrades as file
grows. Frequent reorganizations are undesirable
B+ trees are most widely used index structure that maintains efficiency.
Remember that a tree: Balanced tree: all leafs at the same level:
PROPERTIES OF B+ TREE:
Each node that is not a root or a leaf has between n/2 and n children. A leaf
node has between 2 to m values
59
TWO TYPES OF NODES:
Leaf nodes: Store keys and pointers to data
Index nodes: Store keys and pointers to other nodes Leaf nodes are linked to
each other.
Keys may be duplicated: Every key to the right of a particular key is >= to that
key.
Typical structure of the Node
For i = 1, 2, . . ., n–1, pointer Pi either points to a file record with search -key
value Ki, or to a bucket of pointers to file records, each record having search-key
value Ki.
If Li, Lj are leaf nodes and i <j, Li‘s search-key values are less than Lj‘s search-
key values
Pn points to next leaf node in search-key order The search-keys in a leaf node
are ordered
60
Example For B+ Tree:
UPDATES ON B+TREE
1. Find the leaf node in which the search-key value would appear
2. If the search-key value is already present in the leaf node
add the record to the main file (and create a bucket if necessary)
If there is room in the leaf node, insert (key-value, pointer) pair in the
leaf node
Otherwise, split the node (along with the new (key-value, pointer) entry
Take the n (search-key value, pointer) pairs (including the one being inserted) in
sorted order. Place the first n/2 in the original node, and the rest in a new node.
Let the new node be p, and let k be the least key value in p. Insert (k,p) in
the parent of the node being split.
5. Splitting of nodes proceeds upwards till a node that is not full is found.
In the worst case the root node may be split increasing the height of the tree
by 1.
61
Fig: Splitting A Leaf Node
Result of splitting node containing Brighton and Downtown on inserting Clear view
62
UPDATION OF B+TREE: DELETION
Find the record to be deleted, and remove it from the main file and from the
bucket (if present)
Remove (search-key value, pointer) from the leaf node if there is no bucket or if
the bucket has become empty
If the node has too few entries due to the removal, and the entries in the node
and a sibling fit into a single node, then mergesiblings:
Insert all the search-key values in the two nodes into a single node (the one on
the left), and delete the other node.
Delete the pair (Ki–1, Pi), where Pi is the pointer to the deleted node, from its
parent, recursively using the above procedure.
63
B TREE:
Similar to B+-tree, but B-tree allows search-key values to appear only once; eliminates
redundant storage of search keys.
Search keys in nonleaf nodes appear nowhere else in the B-tree; an additional pointer
field for each search key in a nonleaf node must be included.
Fig: B Tree
64
3.8 HASHING TECHNIQUES:
Hashing is a type of primary file organization, which provides very fast access to
records on certain search conditions. This organization is called as hash file.
The idea behind the hashing is to provide a function h, called a hash function or
randomizing function that is applied to the hash filed value of a record and yields
the address of the disk block in which the record is stored. A search for the record
within the block can be carried out in a main memory buffer.
The hash function is given by:
H(k)=K mod M
M-size of the bucket
In a hash file organization we obtain the bucket of a record directly from its
search-key value using a hash function.
Hash function h is a function from the set of all search-key values K to the set of
all bucket addresses B.
Hash function is used to locate records for access, insertion as well as deletion.
Records with different search-key values may be mapped to the same bucket;
thus entire bucket has to be searched sequentially to locate a record.
Ideal hash function is random, so each bucket will have the same number of
records assigned to it irrespective of the actual distribution of search-key values in
the file.
65
Handling of Bucket Overflows
Overflow chaining – the overflow buckets of a given bucket are chained together
in a linked list. Above scheme is called closed hashing.
An alternative, called open hashing, which does not use overflow buckets, is not
suitable for database applications.
If initial number of buckets is too small, and file grows, performance will degrade
due to too much overflows.
All the entries that point to the same bucket have the same values on the first ij
bits.
Compute h(Kj) = X
Use the first i high order bits of X as a displacement into bucket address table, and
follow the pointer to appropriate bucket
Hash structure after insertion of one Brighton and two Downtown records
67
The main advantage of the extendible hashing is that the performance of the
fields does not degrade as the file grows, as opposed to static external hashing
where collisions increase and the corresponding chaining causes the additional
calculate the hash address. Another technique involves picking some digits of
the hash field value – for example, the third, fifth and eighth digits to form the
hash address.
The problem with most hashing functions is that they do not guarantee that
distinct values will hash to addresses, because the hash field space (the number
of possible values a hash field can take) is usually much larger than the address
A collision occurs when the hash field value of a record that is being inserted
hashes to an address that already contains a different record. In this situation the
new record must be inserted in some other position, since its hash address is
68
Open addressing: proceeding from the occupied position specified by
the hash address, the program checks the subsequent positions in order
until an unused (empty) position is found. The below algorithm may be used
Algorithm:
i hash address (k);
a i;
if location i is occupied
else new_hash_address i;
end;
CHAINING:
For this method, various overflow locations are kept, for extending the array with
a number of overflow positions. In addition a pointer field is added to each record
location. A collision is resolved by placing the new record in an unused overflow
location and setting the pointer of the occupied hash address location to the
address of that overflow location.
MULTIPLE HASHING:
The program applies a second hash function if the first results in a collision, if
another collision results, the program uses open addressing or applies a third
hash function and then uses open addressing if necessary.
69
10. ASSIGNMENTS
INSERT 2
INSERT 4
DELETE 5
DELETE 12
11. Part A Question & Answer
UNIT IV - IMPLEMENTATION TECHNIQUES
S.No Question and Answers CO K
22 CO4 K1
S. No. PART B CO K
Explain in detail about Query optimization using Cost Estimation
1 CO4 K4
Techniques with Examples.
Data Base
Management https://nptel.ac.in/noc/courses/noc18/SEM1/no
1. NPTEL
System c18-cs15/
Database
https://www.coursera.org/learn/database-
2. Coursera Management
management
Essentials
Optimization of
SQL query https://www.coursera.org/projects/optimization-
3. Coursera Tuning and of-sql-query-tuning-and-performance
Performance
Database
Management
Final Part (5): https://www.udemy.com/course/database-
4. Udemy Indexing,B management-indexing-course-btree/
Trees,B+Trees
SQL Tuning
6. Udemy https://www.udemy.com/course/sql-tuning/
14.REAL TIME APPLICATIONS IN DAY TO DAY LIFE AND
TO INDUSTRY
CONTROLLER SPANNING
Controller spanning allows an array to span disks attached to multiple RAID
controllers and, in doing so, allows the creation of very large arrays and provides
high throughput rates. For example, if we have four RAID controllers and each
controller has eight drives attached, controller spanning allows the creation of an
array that spans all thirty-two disk drives. Since performance scales linearly, this
allows an extremely high I/O transfer performance with thirty-two spindles in a
single array.
DISTRIBUTED SPARING
The final enterprise-class RAID feature is distributed sparing, which creates a spare
failover drive without the need to actually include an extra drive. Essentially,
distributed sparing reserves enough disk space on all the included disks so that the
sum of the reserved space is equal to the largest drive in the array. A key advantage
of this approach versus global or dedicated sparing is that all of the drives are
actively used, resulting in significantly better performance. This is different from the
standard, non distributed approach, which sets aside one drive for failover, allowing
the possibility of a silent failure when that drive is not in use.
SUMMARY
There is some serious power in the enterprise-class RAID features that have
traditionally been available only to those with large IT budgets. Now, with the
convergence of RAID and lower priced storage technologies, this same power can be
ADVANCED RAID
Another collection of features, when used in conjunction with each one another,
delivers extra data protection from system threats derived from malicious users,
accidental deletion or viruses. These data protection features include N-way
mirroring, array splitting and array hiding. N-way mirroring goes beyond simple two-
way mirroring, allowing additional mirrors of a data set to be created; array splitting
allows one of those mirrors to be removed from the active array; and array hiding
makes an array invisible to users and the operating system, and accessible only to a
privileged administrator.
When combined, these features allow a system administrator to create a secure
backup of the active data array. To start, the admin creates a three-way mirror using
N-way mirroring. Next, he removes one of the mirrors from the active array through
array splitting. (Note that the active array retains the data integrity provided by a
two-way mirror after one of the mirrors is removed.) Finally, he hides the split mirror
so neither users nor the operating system can see the data. Threats can't attack
what they can't see.
Should a disaster occur in the active array occur, the fix is simple and local. The
admin deletes the data in the corrupted array, unhides the hidden array and
transforms the active array using ORLM to include the good mirror. Please note that
the data was valid as of the time it was split off, so any modifications since that time
would not be reflected in the previously hidden array.
16. ASSESSMENT II SCHEDULE
TEXT BOOKS:
REFERENCES:
1. Raghu Ramakrishnan, Gehrke ―Database Management Systems‖, MCGraw Hill,
3rd Edition 2014.
2. Plunkett T., B. Macdonald, ―Oracle Big Data Hand Book‖ , McGraw Hill, First
Edition, 2013
3. Gupta G K , ―Database Management Systems‖ , Tata McGraw Hill Education
Private Limited, New Delhi, 2011.
2) Inventory Management
The project starts by adding a seller and by adding details of customer. the user can now
purchase new products by the desired seller and then can sell them to the customer, the
purchasing and selling of products is reflected in the inventory section. The main aim of
Inventory Management Mini DBMS project is to add new products and sell them and keep an
inventory to manage them.
Disclaimer:
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document through
email in error, please notify the system manager. This document contains proprietary
information and is intended only to the respective group / learning community as
intended. If you are not the addressee you should not disseminate, distribute or
copy through e-mail. Please notify the sender immediately by e-mail if you have
received this document by mistake and delete this document from your system. If
you are not the intended recipient you are notified that disclosing, copying,
distributing or taking any action in reliance on the contents of this information is
strictly prohibited.
115