Docsity

Dbms notes Study material-ssit-unit-5, Lecture notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
PROF. KAVYA R
UNIT-5
Strategies for Query Processing: Translating SQL Queries into Relational Algebra and Other
Operators, Algorithms for External Sorting, Algorithms for SELECT Operation, Implementing
the JOIN Operation, Algorithms for PROJECT and Set Operations, Implementing Aggregate
Operations and Different Types of JOINs, Combining Operations Using Pipelining, Parallel
Algorithms for Query Processing.
(Text1: 18.1 to 18.8)
• The scanner identifies the query tokens—such as SQL key words, attribute names, and
relation names—that appear in the text of the query,
• whereas the parser checks the query syntax to determine whether it is formulated
according to the syntax rules (rules of grammar) of the query language.
• 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. An internal representation of the query is then created, usually as a tree data
structure called a query tree.
• It is also possible to represent the query using a graph data structure called a query
graph, which is generally a directed acyclic graph (DAG).
• The DBMS must then devise an execution strategy or query plan for retrieving the
results of the query from the database files.
• A query has many possible execution strategies, and the process of choosing a suitable
one for processing a query is known as query optimization.
• The query optimizer module has the task of producing a good execution plan, and the
code generator generates the code to execute that plan.
• The runtime database processor has the task of running (executing) the query code,
whether in compiled or interpreted mode, to produce the query result.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dbms notes Study material-ssit-unit-5 and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

UNIT- 5

Strategies for Query Processing: Translating SQL Queries into Relational Algebra and Other Operators, Algorithms for External Sorting, Algorithms for SELECT Operation, Implementing the JOIN Operation, Algorithms for PROJECT and Set Operations, Implementing Aggregate Operations and Different Types of JOINs, Combining Operations Using Pipelining, Parallel Algorithms for Query Processing. (Text1: 18.1 to 18.8)

  • The scanner identifies the query tokens—such as SQL key words, attribute names, and relation names—that appear in the text of the query,
  • whereas the parser checks the query syntax to determine whether it is formulated according to the syntax rules (rules of grammar) of the query language.
  • 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. An internal representation of the query is then created, usually as a tree data structure called a query tree.
  • It is also possible to represent the query using a graph data structure called a query graph , which is generally a directed acyclic graph (DAG).
  • The DBMS must then devise an execution strategy or query plan for retrieving the results of the query from the database files.
  • A query has many possible execution strategies, and the process of choosing a suitable one for processing a query is known as query optimization.
  • The query optimizer module has the task of producing a good execution plan, and the code generator generates the code to execute that plan.
  • The runtime database processor has the task of running (executing) the query code, whether in compiled or interpreted mode, to produce the query result.

18.1 Translating SQL Queries into Relational Algebra and Other Operators 18.1.1 Additional Operators Semi-Join and Anti-Join Consider a slightly modified version of the schema in Figure 5.5 as follows: EMPLOYEE ( Ssn, Bdate, Address, Sex, Salary, Dno) DEPARTMENT ( Dnumber, Dname, Dmgrssn, Zipcode) where a department is located in a specific zip code. Let us consider the following query: Here we have a nested query which is joined by the connector IN. To remove the nested query: is called as unnesting. It leads to the following query with an operation called semi-join, which we show with a non-standard notation “S=” below: In algebra, alternate notations exist. One common notation is shown in the following figure. QUERY: query counts the number of employees who do not work in departments located in zip code 30332

  • Here, the operation is to find the employee tuples whose Dno attribute does not match the value(s) for the Dnumber attribute in DEPARTMENT for the given zip code.
  • We are only interested in producing a count of such employees, and performing an inner join of the two tables would, of course, produce wrong results.
  • In this case, therefore, the anti-join operator is used while unnesting this query.
  • Anti-join is used for unnesting NOT EXISTS, NOT IN, and ALL subqueries.
  • We represent anti-join by the following nonstandard syntax: T1.x A = T2.y, where T is the left table and T2 is the right table of the anti-join.

(2 * b) + (2 * b * (logdM nR))

  • The first term (2 * b) represents the number of block accesses for the sorting phase, since each file block is accessed twice: once for reading into a main memory buffer and once for writing the sorted records back to disk into one of the sorted subfiles.
  • The second term represents the number of block accesses for the merging phase.
  • During each merge pass, a number of disk blocks approximately equal to the original file blocks b is read and written.
  • Since the number of merge passes is (logdM nR), we get the total merge cost of (2 * b * (logdM nR))
  • The minimum number of main memory buffers needed is nB = 3, which gives a dM of 2 and an nR of ⎡(b/3)⎤. The minimum dM of 2 gives the worst-case performance of the algorithm, which is: (2 * b) + (2 * (b * (log2 nR)))

18.3 Algorithms for SELECT Operation

18.3.1 Implemention Options for the SELECT Operation

1.Search Methods for Simple Selection.

  • These are also known as file scans , because they scan the records of a file to search for and retrieve records that satisfy a selection condition.
  • If the search algorithm involves the use of an index, the index search is called an index scan. The following search methods (S1 through S6) are examples of some of the search algorithms that can be used to implement a select operation
  1. S1—Linear search (brute force algorithm). Retrieve every record in the file, and test whether its attribute values satisfy the selection condition.
  2. S2—Binary search. If the selection condition involves an equality compari son on a key attribute on which the file is ordered, binary search—which is more efficient than linear search—can be used
  3. S3a—Using a primary index. If the selection condition involves an equality comparison on a key attribute with a primary index— for example, Ssn = ‘123456789’ in OP1—use the primary index to retrieve the record. Note that this condition retrieves a single record (at most).
  4. S3b—Using a hash key. If the selection condition involves an equality comparison on a key attribute with a hash key— for example , Ssn = ‘123456789’ in OP1—use the hash key to retrieve the record. Note that this condition retrieves a single record (at most).
  5. S4—Using a primary index to retrieve multiple records. If the comparison condition is >, >=, <= on a key field with a primary index—for example, Dnumber > 5 in OP2— use the index to find the record satisfying the corresponding equality condition (Dnumber = 5); then retrieve all subsequent records in the (ordered) file. For the condition Dnumber < 5, retrieve all the preceding records.
  6. S5—Using a clustering index to retrieve multiple records. If the selection condition involves an equality comparison on a nonkey attribute with a clustering index—for example, Dno = 5 in OP3—use the index to retrieve all the records satisfying the condition.
  1. S6—Using a secondary (B+-tree) index on an equality comparison. This search method can be used to retrieve a single record if the indexing field is a key (has unique values) or to retrieve multiple records if the indexing field is not a key.
  2. S7a—Using a bitmap index. (See Section 17.5.2.) If the selection condition involves a set of values for an attribute (e.g., Dnumber in (3,27,49) in OP6), the corresponding bitmaps for each value can be OR-ed to give the set of record ids that qualify.
  3. S7b—Using a functional index. (See Section 17.5.3.) In OP7, the selection con dition involves the expression ((SalaryCommission_pct) + Salary ). If there is a functional index defined as (as shown in Section 17.5.3): CREATE INDEX income_ix ON EMPLOYEE (Salary + (SalaryCommission_pct)); then this index can be used to retrieve employee records that qualify.

18.3.2 Search Methods for Conjunctive Selection

  1. S8—Conjunctive selection using an individual index. If an attribute involved in any single simple condition in the conjunctive select condition has an access path that permits the use of one of the methods S2 to S6, use that condition to retrieve the records and then check whether each retrieved record satisfies the remaining simple conditions in the conjunctive select condition.
  2. S9—Conjunctive selection using a composite index. If two or more attri butes are involved in equality conditions in the conjunctive select condition and a composite index (or hash structure) exists on the combined fields— for example, if an index has been created on the composite key (Essn, Pno) of the WORKS_ON file for OP5—we can use the index directly.
  3. S10—Conjunctive selection by intersection of record pointers. If second array indexes (or other access paths) are available on more than one of the fields involved in simple conditions in the conjunctive select condition, and if the indexes include record pointers (rather than block pointers), then each index can be used to retrieve the set of record pointers that satisfy the individual condition.

18.3.3 Search Methods for Disjunctive Selection

Compared to a conjunctive selection condition, a disjunctive condition (where simple conditions are connected by the OR logical connective rather than by AND) is much harder to process and optimize.

18.3.4 Estimating the Selectivity of a Condition

Information in the Database Catalog. A typical RDBMS catalog contains the following types of information:

➢ 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

For example , the left outer join operation shown above is equivalent to the following sequence of relational operations: 2) Implementing Semi-Join and Anti-Join. ➢ The purpose is to be able to perform some variant of join instead of evaluat ing the subquery multiple times. 3) Implementing Non-Equi-Join ➢ we referred to this operation as theta-join. This functionality is based on a condition involving any operators, such as , ≥, ≤, ≠, and so on.

18.7 Combining Operations Using Pipelining

  • Evaluating a query by creating and storing each temporary result and then passing it as an argument for the next operator is called materialized evaluation.
  • Combining several operations into one and avoiding the writing of temporary results to disk is called pipelining or stream-based processing.
  • The corresponding evaluation is considered a pipelined evaluation. It has two distinct benefits: ➢ Avoiding the additional cost and time delay incurred for writing the inter mediate results to disk. ➢ Being able to start generating results as quickly as possible

18.7.1 Iterators for implementing Physical Operations Various algorithms for algebraic operations involve reading some input in the form of one or more files, processing it, and generating an output file as a relation. If the operation is implemented in such a way that it outputs one tuple at a time, then it can be regarded as an iterator The iterator interface typically consists of the following methods:

  1. Open (): This method initializes the operator by allocating buffers for its input and output and initializing any data structures needed for the opera tor.
  2. Get_Next (): This method calls the Get_next() on each of its input arguments and calls the code specific to the operation being performed on the inputs.
  3. Close(): This method ends the iteration after all tuples that can be generated have been generated, or the required/demanded number of tuples have been returned.

18.8 Parallel Algorithms for Query Processing

  1. shared-memory architecture - multiple processors are attached to an interconnection network and can access a common main memory region.
  2. shared-disk architecture - In this architecture, every processor has its own memory, which is not accessible from other processors.
  3. shared-nothing architecture becoming the most used architecture in parallel database systems. In this architecture, each processor accesses its own main memory and disk storage. 18.8.1 Operator-Level Parallelism
  4. Sorting. If the data has been range partitioned on an attribute—say, age—into n disks on n processors, then to sort the entire relation on age, each partition can be sorted separately in parallel and the results can be concatenated.
  5. Selection. For a selection based on some condition, if the condition is an equality condition, and the same attribute A has been used for range partitioning, the selection can be performed on only that partition to which the value v belongs.
  6. In other cases, the selection would be performed in parallel on all the processors and the results merged.
  7. Projection and Duplicate Elimination. Projection without duplicate elimination can be achieved by performing the operation in parallel as data is read from each partition. Join. The basic idea of parallel join is to split the relations to be joined, say R and S, in such a way that the join is divided into multiple n smaller joins, and then perform these smaller joins in parallel on n processors and take a union of the result. we discuss the various techniques involved to achieve this.