Indexes

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

What is an index?

  • Structure to improve speed of accessing data from a table
  • Used to locate data quickly without having to scan the entire table
  • Useful for improving performance of queries with filter conditions
  • Applied to table columns
  • Typically added by a database administrator
Improving Query Performance in SQL Server

Clustered and nonclustered indexes

Clustered Index

  • Analogy : dictionary
  • Table data pages are ordered by the column(s) with the index
  • Only one allowed per table
  • Speeds up search operations
Improving Query Performance in SQL Server

Clustered and nonclustered indexes

Clustered Index

  • Analogy : dictionary
  • Table data pages are ordered by the column(s) with the index
  • Only one allowed per table
  • Speeds up search operations

Non-clustered Index

  • Analogy: text book with an index at the back
  • Structure contains an ordered layer of index pointers to unordered table data pages
  • A table can have more than one
  • Improves insert and update operations
Improving Query Performance in SQL Server

Clustered index: B-tree structure

 

  • ROOT NODE

 

  • BRANCH NODES

 

  • PAGE NODES
Improving Query Performance in SQL Server

Clustered index: B-tree structure

ROOT NODE:                          
                                    A G O W
BRANCH NODES:
                A B E F             G H J K             O P S T
PAGE NODES:
          Page 1             Page 2             Page 3              Page 4
       Index Column |...  Index Column |...  Index Column | ...  Index Column | ...
       A | ...            E | ...            I | ...             M | ...
       B | ...            F | ...            J | ...             N | ...
       C | ...            G | ...            K | ...             O | ...
       D | ...            H | ...            L | ...             P | ...
       ...                ...                ...                 ...
Improving Query Performance in SQL Server

Customers table without clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
PAGE NODES:
            Page 1
         CustomerID |...
         ALFKI | ... 
         ANATR | ...
         BLONP | ... 
         BSBEV | ...
         ...                 
Improving Query Performance in SQL Server

Customers table without clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
PAGE NODES:
            Page 1              Page 2
         CustomerID |...     CustomerID|...
         ALFKI | ...         FOLIG | ...
         ANATR | ...         FRANK | ...
         BLONP | ...         GALED | ...
         BSBEV | ...         GREAL | ...
         ...                 ...
Improving Query Performance in SQL Server

Customers table without clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
PAGE NODES:
            Page 1              Page 2              Page 3
         CustomerID |...     CustomerID|...      CustomerID | ... 
         ALFKI | ...         FOLIG | ...         LILAS | ...  
         ANATR | ...         FRANK | ...         LINOD | ...  
         BLONP | ...         GALED | ...         MEREP | ...  
         BSBEV | ...         GREAL | ...         MORGK | ...   
         ...                 ...                 ...          
Improving Query Performance in SQL Server

Customers table without clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
PAGE NODES:
            Page 1              Page 2              Page 3               Page 4
         CustomerID |...     CustomerID|...      CustomerID | ...     CustomerID | ...
         ALFKI | ...         FOLIG | ...         LILAS | ...          OCEAN | ...
         ANATR | ...         FRANK | ...         LINOD | ...          PARIS | ...
         BLONP | ...         GALED | ...         MEREP | ...           
         BSBEV | ...         GREAL | ...         MORGK | ...          
         ...                 ...                 ...                  ...
Improving Query Performance in SQL Server

Customers table with clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
ROOT NODE:                          
                                         ALFKI FOLIG OLDWO WOLZA
BRANCH NODES:
              ALFKI BONAP DRACD FISSA    FOLIG GALED LILAS NORTS    OCEAN OLDWO QUICK WOLZA
PAGE NODES:
                    Page 1              Page 2              Page 3               Page 4
                 CustomerID |...     CustomerID|...      CustomerID | ...     CustomerID | ...
                 ALFKI | ...         FOLIG | ...         LILAS | ...          OCEAN | ...
                 ANATR | ...         FRANK | ...         LINOD | ...          PARIS | ...
                 BLONP | ...         GALED | ...         MEREP | ...          PICCO | ...
                 BSBEV | ...         GREAL | ...         MORGK | ...          QUICK | ...
                 ...                 ...                 ...                  ...
Improving Query Performance in SQL Server

Customers table with clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
ROOT NODE:                          
                                                     OLDWO WOLZA
BRANCH NODES:
              ALFKI BONAP DRACD FISSA    FOLIG GALED LILAS NORTS    OCEAN OLDWO QUICK WOLZA
PAGE NODES:
                    Page 1              Page 2              Page 3               Page 4
                 CustomerID |...     CustomerID|...      CustomerID | ...     CustomerID | ...
                 ALFKI | ...         FOLIG | ...         LILAS | ...          OCEAN | ...
                 ANATR | ...         FRANK | ...         LINOD | ...          PARIS | ...
                 BLONP | ...         GALED | ...         MEREP | ...          PICCO | ...
                 BSBEV | ...         GREAL | ...         MORGK | ...          QUICK | ...
                 ...                 ...                 ...                  ...
Improving Query Performance in SQL Server

Customers table with clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
ROOT NODE:                          
                                                     OLDWO WOLZA
BRANCH NODES:
                                                                          OLDWO QUICK 
PAGE NODES:
                    Page 1              Page 2              Page 3               Page 4
                 CustomerID |...     CustomerID|...      CustomerID | ...     CustomerID | ...
                 ALFKI | ...         FOLIG | ...         LILAS | ...          OCEAN | ...
                 ANATR | ...         FRANK | ...         LINOD | ...          PARIS | ...
                 BLONP | ...         GALED | ...         MEREP | ...          PICCO | ...
                 BSBEV | ...         GREAL | ...         MORGK | ...          QUICK | ...
                 ...                 ...                 ...                  ...
Improving Query Performance in SQL Server

Customers table with clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
ROOT NODE:                          
                                                     OLDWO WOLZA
BRANCH NODES:
                                                                          OLDWO QUICK 
PAGE NODES:
                                                                                 Page 4
                                                                              CustomerID | ...
                                                                              OCEAN | ...
                                                                              PARIS | ...
                                                                              PICCO | ...
                                                                              QUICK | ...
                                                                              ...
Improving Query Performance in SQL Server

Customers table with clustered index

SELECT *
FROM Customers
WHERE CustomerID = "PARIS"
ROOT NODE:                          
                                                     OLDWO WOLZA
BRANCH NODES:
                                                                          OLDWO QUICK 
PAGE NODES:
                                                                                 Page 4
                                                                              CustomerID | ...



                                                                             PARIS | ...


Improving Query Performance in SQL Server

Clustered index: example

SET STATISTICS IO ON
SELECT * 
FROM PlayerStats 
WHERE Team = 'OKC'

PlayerStats table with no index

Table 'PlayerStats'. ..., logical reads 12, ...

 

PlayerStats table with clustered index on Team

Table 'PlayerStats'. ..., logical reads 2, ...
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...