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:SELECT * FROM Student WHERE RollNo = 111Time complexity of table scan is : O(n)RollNo Name Country Age101 Greg UK 23102 Sachin India 21103 Akaram Pakistan 22107 Miyabi China 18108 Marry Russia 27109 Scott USA 31110 Benazir Banglades 17111 Miyabi Japan 24112 Rahul India 27113 Nicolus France 19
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.
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.