index_2

Post on 07-Dec-2014

1.686 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

SQL Query Performance Analysis

What is an index ?

• Index is a way to organize data to make searching, sorting and grouping fasters

• we need indexing when :

1. WHERE, ON, HAVING clause (Searching)2. ORDER BY clause (Sorting)3. GROUP BY clause (Grouping) etc.

Table scan:

RollNo Name Country Age

101 Greg UK 23

102 Sachin India 21

103 Akaram Pakistan 22

107 Miyabi China 18

108 Marry Russia 27

109 Scott USA 31

110 Benazir Banglades 17

111 Miyabi Japan 24

112 Rahul India 27

113 Nicolus France 19

SELECT * FROM Student WHERE RollNo = 111

Time complexity of table scan is : O(n)

Types of Index

• Table without any index is called Heap

• There are two type of index:

1. Clustered index2. Non-Clustered index

Clustered index

• When we create a clustered index on any table physical organization of table is changed.

• Now data of table is stored as a balanced tree(B tree).

CREATE UNIQUE [CLUSTERED] INDEX <Name> ON <ObjectName>( <ColumnName> [ASC | DESC ] [ ,...n ] )

Types of scanning• Table scan: It is very slow can and it is used only if table has

not any clustered index.

• Index scan: It is also slow scan. It is used when table has clustered index and either in WHERE clause non-key columns are present or query has not been covered (will discuss later) or both.

• Index Seek: It is very fast. Our goal is to achieve this.

Clustered index

• If we create table with primary key, sql server automatically creates clustered index on that table

• A table can have only one clustered index .• Physical order of rows of table is same as

logical order of key columns of clustered index.

Terms of execution plan • Predicate: It is condition in WHERE clause which is either non-

key column or column which has not been covered.

• Object: It is name of source from where it getting the data. It can be name of table, Clustered index or non-clustered index

• Output list: It is name of the columns which is getting from object.

• Seek Predicate: It is condition in WHERE clause which is either key column or fully covered.

Non-clustered index• It is logical organization of data of table. A non-clustered index

can be of two types.

1. Heap2. Based on clustered index.

• If table has clustered index then leaf node of non-clustered index keeps the key columns of clustered index.

• If the table has not any clustered index then leaf node of non-

clustered index keeps RID which unique of each row of table.

Based on clustered Index

Based on heap

Covering of queries• We can specify maximum 16 column names.

• Sum of size of the columns cannot be more than 900 bytes.

• All columns must belong to same table.

• Data type of columns cannot be ntext, text, varchar (max), nvarchar (max), varbinary (max), xml, or image

• It cannot be non-deterministic computed column.

THANK YOU

top related