You are on page 1of 4

1 Query Processing

1.1 Introduction
In this lesson, we shall utilize some publicly available databases to illustrate the concepts of
query processing.
Database 1: Northwind Database (Download structure and data for GitHub for practical
exercises on writing SQL queries

The Northwind database contains the following detailed information:

1. Suppliers/Vendors of Northwind – who supply to the company.


2. Customers of Northwind – who buy from Northwind
3. Employee details of Northwind traders – who work for Northwind
4. The product information – the products that Northwind trades in
5. The inventory details – the details of the inventory held by Northwind traders.
6. The shippers – details of the shippers who ship the products from the traders to the end-
customers
7. PO transactions i.e Purchase Order transactions – details of the transactions taking place
between vendors & the company.
8. Sales Order transaction – details of the transactions taking place between the customers
& the company.
9. Inventory transactions – details of the transactions taking place in the inventory
10. Invoices – details of the invoice raised against the order.

Database 2: boatclub database. An .sql file will be provided via the class email which you can
import to your local mysql instance.

The purpose of the database is to facilitate members of a boat club to reserve boats for trips lasting
several hours. The two major entities (table) are:

 Sailors—members of the boat club who reserve boats; and


 Boats—boats in the club's inventory.

How are these two entities related? In this problem we need to know what boats are reserved by
what sailors. Thus, "reservation" is obviously an important relationship in this simple problem.
Thus, the following Entity-Relationship Diagram suffices.

Entity-Relationship Diagram of BoatClub database

1
Class Exercise:
Database 3: University Database (A recap on steps involved in creating a database)

 Requirements Analysis
 Normalization
 Conceptual Model
 ERD
 Database Schema

Objective: Create a database based on a brief set of requirements provided during the first lesson.

While making reference to the database schema generated after completing the above class
exercise, test your basic SQL proficiency by attempting to write SQL query commands to;

1. List the names and email of all students


2. List the names and email of all students whose names begin with ‘MA’
3. List the names and email of all students taking BIT
4. List the names and email of all students from IT department
5. List the names and email of all students from IT and Nursing departments.
6. List the names, email and program of all students from SCI
7. List the names and email of all students from SCI and SPAS

1.2 What is Query Processing


The activities involved in retrieving data from the database are called as query processing. The
aims of query processing are to transform a query written in a high-level language typically
SQL, into a correct and efficient execution strategy expressed in a low-level language
(implementing relational algebra), and to execute the strategy to retrieve the required data. An
important aspect of query processing is Query Optimization. The activity of choosing an
efficient execution strategy for processing a query is called as query optimization. As there are
many equivalent transformations of the same high-level query, the aim of query optimization is
to choose the one that minimizes the resource usage.

2
1.3 How is a query is Executed?
1. The scanner identifies the language components (tokens) in the text of the query,
while the parser checks the correctness of the query syntax.
2. The query is validated (by accessing the system catalog) to ascertain whether the
attribute names and relation names are valid.
3. An internal representation (tree or graph) of the query is created.
4. The query is parsed and then presented to a query optimizer.
5. The optimizer generates alternative plans and chooses the plan with the least
estimated cost.

1.4 Basic Query Operations


A query basically consists of following operations, which can be analyzed separately.
 Selection Operation
 Join Operation
 Projection Operation
 Set Operations
Selection Operation
A query can have condition to filter the required data. For that selection it may use several ways
to search for the data. The following are some of the ways to search:
– File Scan
– Index Scan
File Scan
A number of search algorithms are possible for selecting the records from a file. The selection
of records from a file is known as file scan, as the algorithm scans the records of a file to search
for and retrieve records that satisfy the selection condition.
Index Scan
If the search algorithm uses an index to perform the search, then it is referred as Index Scan.
– Linear Search: This is also known as Brute Force method. In this method, every record in the
file is retrieved and tested as to whether its attribute values satisfy the selection condition.
– Binary Search: If a selection condition involves an equality comparison on a key attribute on
which the file is ordered, a binary search can be used. Binary search is more efficient than linear
search.
– Primary Index Single Record Retrieval: If a selection condition involves an equality
comparison on a primary key attribute with a primary index, we can use the primary index to
retrieve that record.
– Secondary Index : This search method can be used to retrieve a single record if the indexing
field has unique values (key field) or to retrieve multiple records if the indexing field is not a
key. This can also be used for comparisons involving <, >, <= or >=

3
Primary Index Multiple Records Retrieval : A search condition uses comparison condition <,
>, etc. on a key field with primary index is known as Primary index multiple records retrieval.
– Clustering Index: If the selection condition involves an equality comparison on a non-key
attribute with a clustering index, we can use that index to retrieve all the records satisfying the
condition.
Conjunctive Condition
If the selection condition of the SELECT operation is a conjunctive condition (a condition that
is made up of several simple conditions with the AND operator), then the DBMS can use the
following additional methods to perform the selection.
Conjunctive selection: If an attribute in any single simple condition in the conjunctive condition
has an access path that permits use of binary search or an index search, use that condition to
retrieve the records and then check if that record satisfies the remaining condition.
Conjunctive selection using composite index : If two or more attributes are involved in equality
conditions in the conjunctive condition and a composite index exists on the combined fields we
can use the index.

You might also like