index_2

14
SQL Query Performance Analysis

Upload: riteshkiit

Post on 07-Dec-2014

1.674 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Index_2

SQL Query Performance Analysis

Page 2: Index_2

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.

Page 3: Index_2

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)

Page 4: Index_2

Types of Index

• Table without any index is called Heap

• There are two type of index:

1. Clustered index2. Non-Clustered index

Page 5: Index_2

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 ] )

Page 6: Index_2
Page 7: Index_2

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.

Page 8: Index_2

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.

Page 9: Index_2

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.

Page 10: Index_2

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.

Page 11: Index_2

Based on clustered Index

Page 12: Index_2

Based on heap

Page 13: Index_2

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.

Page 14: Index_2

THANK YOU