Friday, July 13, 2018

Query Processing in DBMS

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. Any query written in SQL is converted into low level language using relational algebra which system can understand. DBMS verifies the code written by the user and then converts them into low level languages. It then selects the best execution path and executes the query and gets the data from internal memory. All these processes are together known as query processing.

When a query is submitted to the database, it is received by the query compiler after translating to its internal form. Parser checks the syntax of the user's query and verifies the relations in the database and so on. Query optimizer then picks them to identify the best query plan to process. It checks in the system catalog for the constraints and indexes and decides the best query plan. It generates different execution plans for the query plan. The query execution plan then decides the best and optimized execution plan for execution. once the query plan is chosen, the query is evaluated with that plan and returns the result. so the four phases in query processing are:
  • Parsing and Translation
  • Query Optimization
  • Evaluation or query code generation
  • Execution in DB’s runtime processor
Search algorithms that locate and retrieve records that fulfill a selection condition.his is the lowest level of query processing method. It has linear search (each record is read from the beginning of the file till search record is reached. It checks each record for filter condition one after the other. Records can be fetched using linear search irrespective of filter condition, sorting and index.) and binary search (applicable only when the records are sorted based on the search key value and we have equal condition. i.e.; this method is not suitable for range operation or any other kind. The filter condition should be ‘search key column = value’) methods.
 Conjunction
This is the reference to the conditions in the WHERE clause combined with AND operator. For example, SELECT * FROM STUDENT WHERE empID = 101 AND AGE>=21;
  • If we can access the record by using any one of the condition which provides better access path i.e.; if age>=21 alone can give all the records that matches both age>=21 and empID = 101, and if it has shortest cost time or vice versa.
  • If any one of the index and selection methods described above provides the better cost.
Disjunction
This is the reference to the conditions in the WHERE clause combined with OR operator. 
SELECT * FROM STUDENT WHERE empID = 101 OR AGE>=21;

SELECT * FROM STUDENT WHERE empID = 101 UNION SELECT * FROM STUDENT WHERE AGE>=21;

This is even considered as queries with UNION operator. In this case, indexes will be used when all the search key columns have indexes. It will convert the queries with OR operators to queries with UNION and will use the indexes based on the search key column. Otherwise, it uses linear search.
Negation
It has not equal condition used in the WHERE clause. In most of the cases it uses the linear search method to fetch the records. If the index is present on the search key column then index is used search the records.  
SELECT * FROM STUDENT WHERE empID <>101
Relational operations such as joins , can be implemented efficiently if the input relations are first sorted.
Sort the relation by building an index on sort key and use that index to read the relation in sorted order logically. It may lead to disk access for each record (expensive). so better to order records physically.
Sorting
Eg:
Select salary from instructor where salary < 75000;
it can be written as
it is the responsibility of the system to construct a query evaluation plan that minimizes the cost of query evaluation (query optimization).

Query cost measures

* disk accesses, CPU time to execute query in a distributed or parallel database system and cost of communication.
While calculating the disk I/O time, usually only two factors are considered – seek time and transfer time. The seek time is the time taken the processor to find a single record in the disk memory and is represented by tS. The time taken by the disk to return fetched result back to the processor / user is called transfer time and is represented by tT. 
Suppose a query need to seek S times to fetch a record and there is B blocks needs to be returned to the user. Then the disk I/O cost is calculated as below
(S* tS)+ (B* tT)
Different types of selection methods of a query which affects the cost of the query
File Scan


 Index Scan

 Uses indexes to traverse the record. The search key should be indexed and should be used in the filter condition for an Index scan to work.



Conditions in the WHERE clause combined with AND operator.
Eg: select * from employee where empID=101 AND age>=21;
Check,
conjuctive selection using one index, conjuctive selection using composite index(index on multiple attributes)- selection specifies an equality condition on two or more attributes, conjuctive selection by intersection of identifiers (use of record pointers or record identifiers)- scans each index for pointers to tuples that satisfy an individual condition and algorithm then uses pointers to retrieve the actual records.
Conditions in the WHERE clause combined with OR operator/queries with UNION operator.

Quick sort: If a table size is small and can be accommodated into current memory, then quick sort can be used. As the name suggests it is simple and easy method of sorting.
Merge Sort: For the larger tables which cannot be accommodated in the current memory, this type of sorting is used. It has better performance compared to quick sort.
Memory cannot hold all the records of large tables; In the example above, it can hold up to 4 records only. Suppose Initial table has 12 records .  When merge sort is applied, the records are grouped into 3 with two blocks each. Each block is merged at pass 1 and sorted for the pass 2, where it again merged and sorted. At the last stage blocks at pass 2 are merged and sorted to give the final result. 
 Joins: When we use the joins in the queries, the query optimizer decides which joins to be used. Based on the better cost of the query, any one of the following join method will be used by the query optimizer.
Nested Loop Join:
r theta join s
r is outer relation and s the inner relation of  join.
Result: tr*ts
*no indices
*expensive as it examines every pair of tuples in the two relations.
Algorithm

cost: nr*bs +br block transfers. it is always better to put smaller tables in the inner loop, if it accommodated into its memory. Then the cost will reduce to 2 seeks leading to br+bs block transfers. Cost of nested loop join depends on number of records, blocks and positioning of the tables. However, the cost of nested loop join is very expensive compared to other joins.

Block Nested Loop Join (process relations on a per block basis, rather than on a per-tuple basis):
Every block of the inner relation is paired with every block of outer relation.
Here, in addition to looping each record in outer and inner tables, their blocks are also added for looping. Hence, this method selects block of records from both the tables and compares the records in them. Hence the processing is done block wise.
Here, it traverses each block of records in a loop. Each of outer block reads inner table records only once.  So cost of this method is better compared to NLJ.The number of seeks required is 2 BR and the number of block transfer is given as (Br * Bs) + Br. In the best case where records of both table fit into memory block, the cost is given as 2 seeks and Br+ Bs block transfers. If  join condition is on candidate key of the tables, then it need not traverse through all the records or blocks of inner table. At one point seek itself, it will get the record.
If an index is available on the inner loop's join attribute, replace file scans with efficient index lookups.
Indexed Nested Loop Join:
If an index is available on the inner loop's join attribute in a NLJ, index lookups can replace file scans. for each tuple tr in the outer relation r, the index is used to look up tuples in s that will satisfy the join condition with tuple tr.
* Can be used with existing indices as well as with temporary indices .
*When indexes are used, there is no sequential scan of records. For the indexes are used on the columns  in join condition, it will not scan each records of inner table. It will directly fetch matching record. * Indexes are useful when natural joins or equijoins are used. 
In the worst case, the cost of INLJ is calculated as br*(tT+tS) +nr*C, where nr is the number of records in relation r, tT and tS are each records of table T and S respectively, and C is the cost of fetching single record in the index table.
INLJ is the most efficient one among all.
Merge Join:

*Tables may be sorted or not sorted

*Uses merge-sort technique to sort the records in two tables as it is cost-effective. After sorting,  join condition is applied to get the result
*sort-merge join - used to compute natural joins and equi-joins
Eg: r(R) natural join s(S) have R intersection S common attributes. if these attributes are sorted, join can be computed like merge-sort algorithm.
Assuming all records fit in memory block, Cost of Seeks: [br/bb] + [bs/bb] , where bb buffer blocks are associated to each relation.
Cost of Block Transfer: BR + BS 


Hybrid Merge Join:



Uses If one relation is sorted, and the other has a secondary B+-tree index on the join attribute.
*Merge the sorted relation with the leaf entries of the B+-tree .
* Sort the result on the addresses of the unsorted relation’s tuples
*Scan the unsorted relation in physical address order and merge with previous result, to replace addresses by the actual tuples Sequential scan is more efficient than random lookup.

Hash Join

*used to implement natural joins and equi joins
*hash function h is used to partition tuples of both relation.
*Partition tuples into sets that have same hash value on the join attribute.


tr natural join ts - concatenation of tuples tr and ts.
performs a seperate INLJ on each of partition pairs for i=0..nh. It first builds a hash index on each si and then probes(looks up si) with tuples from ri. The relation s is build input and r is probe input.
Cost:
“Bucketize:” Read R + write 
                  Read S + write
Join: Read R, S Total cost = 3 x [ bR+bS] page3image2017102720 page3image2017102944 page3image2017103168 page3image2017103456
Note: this is an approximation since buckets will vary in size and have to round up to blocks 
Issues with Hash Join

*some bad hash function or bad join condition will produce multiple same records. Then it is difficult to accommodate all the records into one partition block. This is called hash table overflow. 

*Can be avoided by carefully partitioning the tables. This can be resolved while creating the partition itself by creating sub partitions using another hash function. But both the tables should be partitioned in same way. 
* if tables have lot many duplicate values , use BNLJ method.

skewed partition : some partition will have relatively large number of records compared to other partitions. This is also not good for joining the tables.
Hybrid Hash Join
*useful when memory sizes are relatively larger; but not all of the build relation fits in the memory.
For complex joins (query with more than two tables) ,use conjunctions and disjunctions with effective joining techniques. Create index on joining conditions. it will fetch the matching record in one shot, rather than traversing each record of tables.  This method performs pair join rather than joining with one table at a time which means all matching records are fetched in one step.
Eliminating Duplicates and Aggregation
If the user wants to eliminate duplicate records, use DISTINCT keyword in the query.
SELECT DISTINCT * FROM student; -- retrieves unique records from the student table and shows only one record for multiple occurrence of the records.
Cost for deleting the duplicate will depend on the technique used to group the duplicate values and the cost of identifying and deleting those records. This is expensive.
Aggregation operation uses sorting or hashing techniques to get similar groups of records together and group using SUM, AVG etc. Cost estimate for implementing aggregation operation is same as the cost of duplicate elimination, for aggregate functions such as min, max, sum, count and avg. Implement aggregation operations sum, min, max, count and avg on the fly as the groups are being constructed. 
In hashing method, sorting can be optimized during run generation or merge passes. During run or merge passes, it can calculate the partial aggregate values in that block and can pass it to next merge pass.


For on the fly aggregation techniques, only one tuple needs to be stored for each of the groups. So sorted tree structure or hash index fits in memory and the aggregation can be processed with b block transfers ( 1 seek) instead of 3br transfers.
Set Operations
Set operations like UNION, INTERSECTION, MINUS etc are implemented using hashing or merge join methods. But it needs tables to be sorted before. 
*Partition the two relations by same hash function
*create partitions r0, r1, r2....rnh and s0, s1, s2...snh.
*system takes these steps on each partition i-0,1,2....nh.
r union s
*Build in memory hash index on ri, add tuples in si to the hash index if not present  and add tuples in the hash index to result.
r intersection s
*Build in memory hash index on ri, for each tuple in si, probe the hash index and output the tuple to the result only if it is already present in the hash index.
r minus s
*Build in memory hash index on ri, probe the hash index and delete if tuple is already present in the hash index. Add remaining tuples in the hash index to result.
Join
The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records.

SELECT column-names
FROM table-name1 FULL JOIN table-name2
ON column-name1 = column-name2
WHERE condition
Note: FULL OUTER JOIN can potentially return very large result-sets!
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.

SELECT column-names
FROM table-name1 LEFT JOIN table-name2
ON column-name1 = column-name2
WHERE condition
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.
A self JOIN is a regular join, but the table is joined with itself.
The INNER JOIN keyword selects records that have matching values in both tables.



SELECT column-names

FROM table-name1 INNER JOIN table-name2

ON column-name1 = column-name2

WHERE condition

Eg:

Table 1


ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Jis      |  32 | Bhandup   |  2000.00 |
|  2 | Jomini   |  25 | Delhi     |  1500.00 |
|  3 | komalam  |  23 | Kota      |  2000.00 |
|  4 | Chaithan |  25 | Mumbai    |  6500.00 |
|  5 | Haven    |  27 | Bhopal    |  8500.00 |
|  6 | Kimma    |  22 | MP        |  4500.00 |

Table 2:
+-----+---------------------+-------------+
|OID  | DATE        CUSTOMER_ID | AMOUNT |
+-----+-------------------------+--------+
| 102 | 2009-10-08            3 |   3000 |
| 100 | 2009-10-08            3 |   1500 |
| 101 | 2009-11-20            2 |   1560 |
| 103 | 2008-05-20            4 |   2060 | 
SELECT ID, NAME, AGE, AMOUNT
 FROM Table 1, Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;


+----+----------+-----+--------+
| ID | NAME     | AGE | AMOUNT |
+----+----------+-----+--------+
|  3 | komalam  |  23 |   3000 |
|  3 | komalam  |  23 |   1500 |
|  2 | Jomini   |  25 |   1560 |
|  4 | Chaithan |  25 |   2060 |
+----+----------+-----+--------+
SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 LEFT JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;


+----+----------+--------+---------------------+
| ID | NAME     | AMOUNT | DATE                |
+----+----------+--------+---------------------+
|  1 | Jis      |   NULL | NULL                |
|  2 | Jomini   |   1560 | 2009-11-20 
|  3 | komalam  |   3000 | 2009-10-08 
|  3 | komalam  |   1500 | 2009-10-08 
|  4 | Chaithan |   2060 | 2008-05-20 
|  5 | Haven    |   NULL | NULL                |
|  6 | Kimma    |   NULL | NULL  
SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 RIGHT JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;


+----+----------+--------+---------------------+
| ID | NAME     | AMOUNT | DATE                |
+----+----------+--------+---------------------+
|  3 | Komalam  |   3000 | 2009-10-08 
|  3 | Komalam  |   1500 | 2009-10-08 
|  2 | Jomini   |   1560 | 2009-11-20 
|  4 | Chaithan |  2060 | 2009-10-08 
SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 FULL JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;


+------+----------+--------+---------------------+
| ID   | NAME     | AMOUNT | DATE                |
+------+----------+--------+---------------------+
|    1 | Jis      |   NULL | NULL                
|    2 | Jomini   |   1560 | 2009-11-20 
|    3 | Komalam  |   3000 | 2009-10-08  
|    3 | Komalam  |   1500 | 2009-10-08 
|    4 | Chaithan |   2060 | 2008-05-20 
|    5 | Haven    |   NULL | NULL                
|    6 | Kimma    |   NULL | NULL                
|    3 | komalam  |   3000 | 2009-10-08 
|    3 | komalam  |   1500 | 2009-10-08 
|    2 | Jomini   |   1560 | 2009-11-20 
|    4 | Chaithan |   2060 | 2008-05-20 
+------+----------+--------+---------------------+


mySQL does not support full join. Instead we can use UNION ALL. it will produce same result shown above.

SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 LEFT JOIN Table 2

WHERE  Table 1.ID = Table 2.CUSTOMER_ID
UNION ALL
SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 RIGHT JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;

SELECT ID, NAME,  AMOUNT, DATE

FROM Table 1 LEFT JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID
UNION 
SELECT ID, NAME,  AMOUNT, DATE
FROM Table 1 RIGHT JOIN Table 2
WHERE  Table 1.ID = Table 2.CUSTOMER_ID;
+------+----------+--------+---------------------+
| ID   | NAME     | AMOUNT | DATE                |
+------+----------+--------+---------------------+
|    1 | Jis      |   NULL | NULL                
|    2 | Jomini   |   1560 | 2009-11-20 
|    3 | Komalam  |   3000 | 2009-10-08  
|    3 | Komalam  |   1500 | 2009-10-08 
|    4 | Chaithan |   2060 | 2008-05-20 
|    5 | Haven    |   NULL | NULL                
|    6 | Kimma    |   NULL | NULL   
Use hash join or merge join to perform equijoin in first step. In other words, natural outer joins and outer joins with an equi-join condition can be computed by extensions of the merge-join and hash-join algorithms.
Let us have query like below:
SELECT * FROM takes r LEFT OUTER JOIN student s ON r.ID = s.ID;
The processor picks all matching records of takes and student by using the best join algorithm , then it picks the records of takes which are not selected by hash join or merge join and adds them to the result set by appending NULLs to the columns of takes.

Read this for understanding more about joins
http://www.sql-join.com/sql-join-types/
Methods of evaluating a query written in SQL
1) Materialization -generate results of an expression whose inputs are relations or are already computed, materialize(store) it on disk. Repeat.
To be specific, queries are broken into individual queries and then the results of which are used to get the final result.
Eg: SELECT c.name FROM department s, instructor c 
where s.building='Watson';
DBMS breaks the query into two and evaluates the first query and stores it in the temporary table in the memory. This temporary table data will be then used to evaluate the second query. This is the example of two level queries in materialization method. We can have any number of levels and so many numbers of temporary tables. This is called materialized evaluation as the results of each intermediate operation are created (materialized) and are used for evaluation of next-level operations.
Cost of materialized evaluation is always more. It takes the time to evaluate and write into temporary table, then retrieve from this temporary table and query to get the next level of result and so on. Hence cost of evaluation in this method is: 
    Cost = cost of individual SELECT + cost of write into temporary table

2) Pipelining
In this method, DBMS do not store the records into temporary tables. Instead, it queries each query and result of which will be passed to next query to process and so on. It will process the query one after the other and each will use the result of previous query for its processing.
*No extra cost of writing into temporary tables. It has only cost of evaluation of individual queries; hence it has better performance than materialization.
*Quick query results
*Low memory requirements, as the results of an operation are not stored for long.

Pipeline Implementation
*construct a single complex operation that combines the operations that constitute the pipeline.

1) Demand -driven pipeline (Lazy evaluation)
In this method, the result of lower level queries are not passed to the higher level automatically. It will be passed to higher level only when it is requested by the higher level. In this method, it retains the result value and state with it and it will be transferred to the next level only when it is requested.
In our example above, building 'Watson' will be retrieved first from the department table, but it will not be passed to select query immediately. It will pass only when it is requested. Once it gets the request, it is passed to instructor query to retrieve name of instructor and that query will be processed.
System makes repeated requests for tuples from the operation at the top of the pipeline. Each time an operation receives a request for tuples, it computes the next tuples to be returned and then returns the tuple.
functions in iterator
open()
next()- return the next output tuple of the operation
close()-no more tuples are required
iterator maintains the state of its execution in between calls, so that successive next() requests receive successive result tuples
2) Producer-driven pipeline (Eager Pipelining/Push and Pull pipelining)
* Lower level queries eagerly pass the results to higher level queries. It does not wait for the higher level queries to request for the results. 
*Lower level query creates a buffer to store the results and the higher level queries pulls the results for its use. if the buffer is full, then the lower level query waits for the higher level query to empty it.
Operations do not wait for requests to produce tuples, but instead generate the tuples eagerly. Each operation in a producer-driven pipeline is modelled as a separate thread within the system that input tuples from its pipelined inputs and generates tuples stream for its output.
Other pipelining methods: Linear and non-linear methods of pipelining, left deep tree, right deep tree

*Producer driven pipeline- pushing data up an operation tree from below, tuples are generated eagerly
Demand driven pipeline- pulling data up an operation tree from the top, tuples are generated lazily on demand. This is used more commonly than producer driven pipelining.
Double pipelined hash join technique- If two inputs to pipeline into the join are not already sorted, use this technique. Input tuples for both input relations r and s are already pipelined and are queued for processing in a single queue.

Tuesday, July 10, 2018

Functions , Stored Procedure , cursors and Packages in Database

Procedures and Functions allow 'business logic' to be stored in the database and executed from SQL statements. Advantages are it allows multiple applications to access procedures, a single point of change in case business rules change, can be invoked from dynamic SQL

A function returns a value and a stored procedure doesn’t return a value. 

Functions:-Function will allow only Select statement, it will not allow us to use DML statements. Also, it will allow only input parameters, doesn’t support output parameters.Transactions are not allowed within functions. Transactions are not allowed within functions. Stored procedures can’t be called from function. it can be called from select statement. User defined functions can be used in join clause as a result set. Useful with specialized data types such as images and geometric objects
Eg: An image data type may have associated functions to compare two images for similarity.
Functions that can return tables as results are called table functions. Functions must contain a return statement. To call a function,  pass the required parameters along with the function name and if the function returns a value, then store the returned value.
SQL functions have the following uses −
  1. Performing calculations on data
  2. Modifying individual data items
  3. Manipulating the output
  4. Formatting dates and numbers
  5. Converting data types
CREATE [OR REPLACE] FUNCTION function_name 
[(parameter_name [IN | OUT | IN OUT] type [, ...])] 
RETURN return_datatype 
{IS | AS} 
BEGIN 
   < function_body > 

END [function_name];
Procedures and Functions allow business logic to be stored in the database and executed from SQL statements.
Functions Example
find no of instructors in a department, given department name.

create function deptcount(dept_name varchar(20))
returns integer
begin
declare dcount integer'
select count(*) into dcount
from instructor
where instructor.dept_name=dept_name
return dcount;
end;


Function usage

Return names and budget of dept with more than 10 instructors

select dept_name, budget
from instructor
where deptcount(dept_name)>10;

Stored Procedure: 
Stored Procedure can have select statements as well as DML statements such as insert, update, deletes etc.It can have both input and output parameters. Transactions can be used in SP's. Table variables and temporary variables can be used. SP's can call functions. Procedures can’t be called from Select/Where/Having etc statements. Execute/Exec statement can be used to call/execute stored procedure. it can't be used in join clause. Prepared SQL code that you save so you can reuse the code repeatedly. To running the same SQL code repeatedly, we can pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed.
Eg: creating simple sp from T-SQL
Select * from ProductFactExtracttable

Use SRCSchema
GO
CREATE PROCEDURE dbo.usp_ProductFactExtracttable
AS
Select * from ProductFactExtracttable
GO
To call procedure, EXEC dbo. usp_ProductFactExtracttable
Stored procedures can accept one or more parameter value. To Limit records based on startdate and enddate, we can use parameters @StartDate and @EndDate.
To execute sp, EXEC dbo. usp_ProductFactExtracttable @StartDate = ’ 2016-01-08 21:59:54.137' , @EndDate = ’ 2016-01-08 21:59:54.137'
Use SRCSchema
GO
CREATE PROCEDURE dbo.usp_ProductFactExtracttable @StartDate DATETIME, @EndDate DATETIME
AS
SET NOCOUNT ON;
BEGIN
……………………………

SET NOCOUNT ON Improves SQL Server Stored Procedure Performance. This one line of code put at the top of a stored procedure turns off the messages that SQL Server sends back to the client after each T-SQL statement is executed. This is performed for all SELECT, INSERT, UPDATE, and DELETE statements. To get the number of rows affected by the T-SQL statement that is executing you can still use the @@ROWCOUNT option.
-- not using SET NOCOUNT ON
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SET NOCOUNT ON
SELECT *
FROM Person.Address
WHERE City = @City
PRINT @@ROWCOUNT
GO
To drop a single stored procedure you use the DROP PROCEDURE or DROP PROC command as follows.
DROP PROCEDURE dbo.uspGetAddress
GO
-- or
DROP PROC dbo.uspGetAddress
GO
To drop multiple stored procedures with one command you specify each procedure separated by a comma as shown below.
DROP PROCEDURE dbo.uspGetAddress, dbo.uspInsertAddress, dbo.uspDeleteAddress
GO
-- or
DROP PROC dbo.uspGetAddress, dbo.uspInsertAddress, dbo.uspDeleteAddress
GO
Once a stored procedure has been created it is stored within one of the system tables in the database that is was created in.
Modifying an Existing Stored Procedure
Let's say we have the following existing stored procedure:  This allows us to do an exact match on the City.
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT *
FROM Person.Address
WHERE City = @City
GO
Let's say we want to change this to do a LIKE instead of an equals.
To change the stored procedure and save the updated code you would use the ALTER PROCEDURE command as follows.
ALTER PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT *
FROM Person.Address
WHERE City LIKE @City + '%'
GO
Cursors

Oracle uses work spaces to execute the SQL commands. when Oracle processes a SQL command, it opens an area in the memory called Private SQL Area. A cursor is an identifier for this private SQL area. It allows programmers to name this area and access it’s information.

SQL%ROW COUNT returns the number of rows that are processed by a SQL statement.  SQL%FOUND returns the Boolean value TRUE if at least one row was processed. SQL%NOT FOUND returns the Boolean value TRUE if no rows were processed. The %NOTFOUND attribute returns true when the last row of the cursor is processed, and no other row is available.
Oracle implicitly declares a cursor to all the DDL and DML commands that return only one row. For queries returning multiple rows, an explicit cursor is created.
PAckages
Packages are schema objects that groups logically related PL/SQL types, variables, and subprograms.
A package will have two mandatory parts −

  • Package specification
  • Package body or definition


Friday, June 29, 2018

Indexing in Database

To retrieve instructor details given department name, db 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 appropriate instructor details.

Two kinds of indices

1) Ordered Indices- based on sorted ordering of the values
2)Hash Indices- based on uniform distribution of values determined by hash function across a range of buckets

Each technique evaluated based on access types, access time, insertion time, deletion time and space overhead.
An attribute or set of attributes used to look up records in a file is called search key. If there are several indices on a file, there are several search keys.
 Ordered Indices
Index structure is used to gain fast random access to records in a file.
Primary index: in a sequentially ordered file, the index whose search key specifies the sequential order of the file.
  • Also called clustering index. A clustered index is very useful when you want to retrieve many rows of data, a range of data, or when the BETWEEN clause is used in the WHERE clause.
  • The search key of a primary index is usually but not necessarily the primary key. Files with a clustering index on the search key are called index-sequential files.
Types of Ordered Indices

Dense index: An index entry appears for every search key value in the file.  In a dense clustering index, index record contains the search key value and a pointer to the first data record with that search key value. In a dense non clustering index, index must store list of pointers to all records with the same search-key value.

Sparse Index: contains index records for only some search-key values.Applicable when records are sequentially ordered on search-key. Search file sequentially starting at the record to which the index record points. Generally slower , less space and less maintenance overhead for insertions and deletions.than dense index for locating records.



Creating an index: 

It’s a good practice to only create indices for columns rather than rows, because the indices need to be updated when the table is updated as well.


 1) The table to be indexed must be in your own schema.
 2) You must have the INDEX object privilege on the table to be indexed.
 3) You must have the CREATE ANY INDEX system privilege.

CREATE INDEX the_index_name ON the_table_name (the_column_name);
create index  on (); 
create index dept_index on instructor(dept_name);
create unique index dept_index on instructor(dept_name);
dept_name is the candidate key for instructor.

To drop index: drop index indexname;
Multilevel Indices: Indices with two or more levels.Construct a sparse outer index on the original index (inner index ). index entries are always in sorted order. To search large tuples (Eg: 100,000,000), binary search is expensive and time consuming as primary index does not fit in memory. solution is treat primary index kept on disk as a sequential file and construct a sparse index on it. It is closely related to tree structures such as binary trees used for in-memory indexing. On insertion and deletion, indexes must be updated.
  • outer index –a sparse index of primary index
  • inner index –the primary index file
Secondary index: An index whose search key specifies an order different from the sequential order of the file. Also called non-clustering index. it must be dense. A secondary index must contain pointers to all records. If a person wants to find all the records whose values in a particular field satisfy some condition. For example: Employee relation stored sequentially by EName (non search key), find all employees in CS department.
A sequential scan in clustering index order is efficient because records in the file are stored physically in the same order as the index order.

Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive.

SQL Server unique indexes vs. unique constraints:

  • A unique index ensures that the values in the index key columns are unique.
  • A unique constraint also guarantees that no duplicate values can be inserted into the column(s) on which the constraint is created. When a unique constraint is created a corresponding unique index is automatically created on the column(s).
Table without a clustered index. It’s an assorted collection of objects. It has multiple extents with different page data. Generally there is no functional difference between a unique index and a unique constraint. The latter is also listed as a constraint, however this is only a method to emphasize the purpose of the index. There is no difference for the query optimizer whether the index is created as a unique index or a unique constraint, therefore there is no performance difference. However there are some differences for creation where some index creation options are not available for unique constraints.

Clustered index- The data rows are stored in order based on clustered index key. It is implemented as a B-tree index structure. It have only one row in sys.partitions, with index_id=1. Data pages in the leaf level are linked in a doubly linked list.


Non Clustered index- It is implemented as a B-tree index structure. It do not affect the order of data rows. Each index row contains the non-clustered key value, row locator (row id) and any included or non-key columns.
Indexing tips
Tip 1
Unused Indexes: Drop unused indexes
Tip 2: Missing index
Tip 3: Duplicate indexes: Drop duplicate index

Friday, June 22, 2018

Terms used in everyone's life

Perception (what’s happening now?)
Notification (what do I need to know now?)
Suggestion (what do you recommend?)
Automation (what should I always do?)
Prediction (what can I expect to happen?)
Prevention (what can I avoid?)
Situational Awareness (what do I need to do right now?)

GEN AI

  Stay Tuned....