SQL Index and SQL Plan


Index :

  • Indexes are used to improve the performance of searching.
  • An index makes a search faster by using a Balanced Tree (B-Tree) Structure. 
  • In B-Tree Structure data divided into root node, non- leaf nodes and leaf nodes.
  • An index is a pointer to data in a table.
  • Indexes are special lookup tables that the database search engine can use to speed up data retrieval.
  • Example :
  • Index has two types which helps to search fast :

1. Clustered Index :

  • A Clustered Index stores the actual data at the leaf node.
  • In SQL server clustered index is created automatically, when a primary key is created.
  • Only one clustered index is created per table.

Creating Clustered Index in SQL server : 

1.  Go to Design of a table, then right click on column and select Indexes/ Keys :

2.  After that in Table Designer set 'Create As Clustered' as Yes, See Clustered index is created :


2. Non-Clustered Index:

  • The leaf nodes of non-clustered index doesn't know where the data is? Leaf node of non-clustered index points to the clustered index's leaf node for searching the data.
  • We can create multiple non-clustered index per table.
  • The non-clustered index does not contain actual data. It uses a clustered index for fetching the data.
  • Non-Clustered index used to increase the speed of queries.


Creating Non-Clustered Index in SQL server:

1.  Go to Design of a table, then right click on column and select Indexes/ Keys :

2.  Then click Add and select the column name , also select type to index :


SQL Plan :

  • The SQL plan is nothing but the plan by SQL server created while executing.
  • It plans how to execute the query.
  • We can directly click on “Display Estimated Execution Plan” toolbar button to check plan.
  • Simple table scan :
Index scan :
  • An index scan is used when SQL Server need to scan the data or index pages to find the appropriate records. 
  • Index Scan retrieves all the rows from the table. 
  • Example : 


Index seek :
  • Index seeks are generally used for the highly selective queries.
  • Index Seek retrieves selective rows from the table.
  • Example :



Thank You!!!

For more understanding watch below video :

Comments

Popular posts from this blog

Day 3 : JavaScript

SQL Concepts-1

ASP .NET Session Management