SlideShare a Scribd company logo
Understanding Indexes
for Writing Better
Queries
Janice Gerbrandt
Agenda
How Data is
stored
Types of indexes Writing queries
that use indexes
About Me
I design and implement database solutions for an
ERP software solution (supply chain
management).
Twitter: @gerbyj
Email: janice.gerbrandt@gmail.com
About Me
About Me
Why this session?
tSQL is a Declarative Language
When I write a query in SQL Server, I only tell
the database engine WHAT I want.
Eg: “I want the names of all the people with
the last name ‘Simpson’”
tSQL is a Declarative Language
“I want the names of all the people with the
last name ‘Simpson’”
select FirstName, LastName
from Person
where LastName = 'Simpson'
Why are some queries slow?
select FirstName, LastName
from Person.Person
where LastName = 'Simpson'
vs
select FirstName, LastName
from Person.Person
where FirstName = 'Kim'
Both Queries returned 10 rows
We’re spoiled in 2020
Index analogies used to be easy!
We’re spoiled in 2020
But we still eat, right?
Finding a good recipe on the internet can
take longer than cooking it.
Luckily they still make cookbooks!
A Tale of 3 Cookbooks
The Search for the Perfect Pot Roast Recipe
Book One – Aunt Gertrude’s Collection
Book Two – Basic Cookbook
Book Three – The Canadian Living Cookbook
Book Three – The Canadian Living Cookbook
How long did it take to find that Pot
Roast recipe?
 Book 1: Scanned every page in the book
 Book 2: Looked in an index for the first
page of section, turned to that page, then
scanned each page of the section
 Book 3: Located list of recipes in an index,
scanned that list, then turned to that page
We came here to talk
about databases, right?
How do we find records in tables?
Similar to how we find recipes in cookbooks:
 Scan
 Seek, then Scan
 Seek
How are tables structured?
As Pages (8 KB)
in the data
files, organized
into Extents (8
page chunks)
Page Types
 Data
 Index
 Text/Image
 Various types of Maps
Table Data Structure: Heap
 When a table doesn’t have a defined order
its data rows are stored in an unordered
structure.
 Data order cannot be predicted.
Table Data Structure: Clustered Index (CI)
 When a table does have a defined order its
data rows are stored in an ordered structure
sorted by its key values.
 Key values are the columns included in the
index definition (eg: MealType)
 There can be only one clustered index per
table, because the data rows themselves can
be stored in only one order.
Optional Index: Nonclustered Index (NCI)
 An ordered structure sorted by its key
values that is separate from the data rows.
 Entries contain the key values (eg:
Keyword) and a pointer to the data row (ie:
the row locator).
Heap
id index_id = 0 first_iam_page
Index
Allocation
Map
(IAM)
Page Header
Data Rows
Page Header
Data Rows
Page Header
Data Rows
Clustered
Index
id index_id = 1 root_page
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Data Rows
Previous Page
Next Page
Data Rows
Previous Page
Next Page
Data Rows
Root
Node
Intermediate
Node
Leaf
Node
Traversing the Tree
 Lets find ‘Simpson, Kim’ in the Employee table
 Key values are LastName, FirstName
 Root node:
Aaronson, Alan
Fairchild, Meagan
Morgan, Beverly
Thompson, Sam
Traversing the Tree
 Lets find ‘Simpson, Kim’ in the Employee table
 Key values are LastName, FirstName
 Intermediate node:
Morgan, Beverly
Potter, Henry
Ramsay, Delores
Smith, Sam
Traversing the Tree
 Lets find ‘Simpson, Kim’ in the Employee table
 Key values are LastName, FirstName
 Leaf node:
Ramsay, Delores, (1992-04-23, Sr Engineer)
Rumsfeld, Dan, (2016-03-22, Engineer)
Sanderson, Betty, (2019-08-19, Jr Engineer)
Simpson, Kim, (2014-01-04, Engineer)
Why are some queries slow?
select FirstName, LastName
from Person.Person
where LastName = 'Simpson'
vs
select FirstName, LastName
from Person.Person
where FirstName = 'Kim'
Nonclustered
Index
id index_id > 1 root_page
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Key Values
Previous Page
Next Page
Key Values
Previous Page
Next Page
Key Values
Root
Node
Intermediate
Node
Leaf
Node
Nonclustered Index
on Heap
Previous Page
Next Page
Key Values Nonclustered Index
Leaf Node
IAM
Page Header
Data Rows
Page Header
Data Rows
Page Header
Data Rows
Nonclustered Index
on Clustered Index
Previous Page
Next Page
Key Values Nonclustered Index
Leaf Node
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Index Rows
Previous Page
Next Page
Data Rows
Previous Page
Next Page
Data Rows
Previous Page
Next Page
Data Rows
Finding Data Means
Reading Pages
Indexes = Good
Indexable Queries: Finding Data
 Check that the columns tested by your
WHERE clause are in an index
 Ditto for: JOIN conditions, HAVING clause
 Don’t hide the column values to make them
unusable by the index
Implicit Indexes
 Primary Key Constraints (PK)
 Unique Key Constraints (AK)
Both require uniqueness
Your only option on table variables
SARGable = Search ARGument ABLE
 A SARGable predicate is one where SQL
server can isolate the single value or range
of index key values to process
 Functions and operators can act like a black
box around the column value, so the
database engine can’t use it in an index
and must scan -> Non-SARGable
Are any functions SARGable?
Not many:
 LIKE (without a leading %)
 =, >, >=, <, <=, IN, BETWEEN
 IS NULL
 Some Date functions
 User Defined Functions that are basically
parameterized views
Indexable Queries: Clustering Data
 Check that all the columns you’re asking
for are covered by the index
 Pay attention to the order of key values in
the index. Even non-SARGable conditions
aren’t that bad if they’re grouped together
 Key values of the Clustered Index will be on
the leaf pages of your Nonclustered Index
Indexable Queries: Sorting Data
 Check that your sort order is supported by
the index.
 Ditto for: GROUP BY
 It works in reverse, too
Thank you!

More Related Content

PDF
Database Indexes
PPT
Access data connection
PPT
SQL Server 2000 Research Series - Performance Tuning
PPT
Access 4 U
PDF
Database.pdf
PPTX
Dbms keys
PDF
Database Essentials for Healthcare Finance Professionals
Database Indexes
Access data connection
SQL Server 2000 Research Series - Performance Tuning
Access 4 U
Database.pdf
Dbms keys
Database Essentials for Healthcare Finance Professionals

What's hot (15)

PPTX
SQL_Part1
DOCX
Index in sql server
PPT
Standard Grade Administration - Software Applications
PPTX
Sql server ___________session_17(indexes)
PPTX
Ui path interview questions
PPTX
Tableau Interview Questions
PPTX
Types Of Keys in DBMS
PDF
Spssbaseusersguide160
PDF
IBM SPSS Statistics: Download and Upload
PPTX
McNair Library Overview
PPTX
HTML Tables
PPTX
DBMS: Types of keys
PPTX
Slide 5 keys
PDF
Quick sheet advanced searching tips
SQL_Part1
Index in sql server
Standard Grade Administration - Software Applications
Sql server ___________session_17(indexes)
Ui path interview questions
Tableau Interview Questions
Types Of Keys in DBMS
Spssbaseusersguide160
IBM SPSS Statistics: Download and Upload
McNair Library Overview
HTML Tables
DBMS: Types of keys
Slide 5 keys
Quick sheet advanced searching tips
Ad

Similar to Geek Sync | Understand Indexes to Write Better Queries (20)

PPTX
dotnetMALAGA - Sql query tuning guidelines
PPSX
Annotating search results from web databases-IEEE Transaction Paper 2013
PPTX
DATABASE-1.pptx
PPTX
Unit 10 - Realtional Databases.pptxxxxxxxxx
PDF
SQL dabatase interveiw pdf for interveiw preparation
PDF
Practical SQL A Beginner s Guide to Storytelling with Data 2nd Edition Anthon...
PDF
Practical SQL A Beginner s Guide to Storytelling with Data 2nd Edition Anthon...
PDF
Practical SQL: A Beginner's Guide to Storytelling with Data, 2nd Edition Anth...
PPTX
DBMS-Unit-2.pptx
PPTX
Query parameterization
PPTX
SQL.pptx
DOCX
JAVA 2013 IEEE DATAMINING PROJECT Annotating search results from web databases
DOCX
Annotating search results from web databases
PPTX
SQL things ace series of the thing useful
PPT
Physical elements of data
PDF
Steps towards of sql server developer
PDF
Indexing techniques
PDF
Difference between fact tables and dimension tables
PPTX
Relational data model in Cassandra: Will it fit?
PPTX
Presentation
dotnetMALAGA - Sql query tuning guidelines
Annotating search results from web databases-IEEE Transaction Paper 2013
DATABASE-1.pptx
Unit 10 - Realtional Databases.pptxxxxxxxxx
SQL dabatase interveiw pdf for interveiw preparation
Practical SQL A Beginner s Guide to Storytelling with Data 2nd Edition Anthon...
Practical SQL A Beginner s Guide to Storytelling with Data 2nd Edition Anthon...
Practical SQL: A Beginner's Guide to Storytelling with Data, 2nd Edition Anth...
DBMS-Unit-2.pptx
Query parameterization
SQL.pptx
JAVA 2013 IEEE DATAMINING PROJECT Annotating search results from web databases
Annotating search results from web databases
SQL things ace series of the thing useful
Physical elements of data
Steps towards of sql server developer
Indexing techniques
Difference between fact tables and dimension tables
Relational data model in Cassandra: Will it fit?
Presentation
Ad

More from IDERA Software (20)

PPTX
The role of the database administrator (DBA) in 2020: Changes, challenges, an...
PPTX
Problems and solutions for migrating databases to the cloud
PPTX
Public cloud uses and limitations
PPTX
Optimize the performance, cost, and value of databases.pptx
PPTX
Monitor cloud database with SQL Diagnostic Manager for SQL Server
PPTX
Database administrators (dbas) face increasing pressure to monitor databases
PPTX
Six tips for cutting sql server licensing costs
PDF
Idera live 2021: The Power of Abstraction by Steve Hoberman
PDF
Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
PDF
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
PDF
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
PDF
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
PDF
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
PDF
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
PDF
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
PPTX
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
PPTX
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
PPTX
Benefits of Third Party Tools for MySQL | IDERA
PPTX
Achieve More with Less Resources | IDERA
PPTX
Benefits of SQL Server 2017 and 2019 | IDERA
The role of the database administrator (DBA) in 2020: Changes, challenges, an...
Problems and solutions for migrating databases to the cloud
Public cloud uses and limitations
Optimize the performance, cost, and value of databases.pptx
Monitor cloud database with SQL Diagnostic Manager for SQL Server
Database administrators (dbas) face increasing pressure to monitor databases
Six tips for cutting sql server licensing costs
Idera live 2021: The Power of Abstraction by Steve Hoberman
Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
Benefits of Third Party Tools for MySQL | IDERA
Achieve More with Less Resources | IDERA
Benefits of SQL Server 2017 and 2019 | IDERA

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx

Geek Sync | Understand Indexes to Write Better Queries

  • 1. Understanding Indexes for Writing Better Queries Janice Gerbrandt
  • 2. Agenda How Data is stored Types of indexes Writing queries that use indexes
  • 3. About Me I design and implement database solutions for an ERP software solution (supply chain management). Twitter: @gerbyj Email: janice.gerbrandt@gmail.com
  • 7. tSQL is a Declarative Language When I write a query in SQL Server, I only tell the database engine WHAT I want. Eg: “I want the names of all the people with the last name ‘Simpson’”
  • 8. tSQL is a Declarative Language “I want the names of all the people with the last name ‘Simpson’” select FirstName, LastName from Person where LastName = 'Simpson'
  • 9. Why are some queries slow? select FirstName, LastName from Person.Person where LastName = 'Simpson' vs select FirstName, LastName from Person.Person where FirstName = 'Kim'
  • 11. We’re spoiled in 2020 Index analogies used to be easy!
  • 13. But we still eat, right? Finding a good recipe on the internet can take longer than cooking it. Luckily they still make cookbooks!
  • 14. A Tale of 3 Cookbooks The Search for the Perfect Pot Roast Recipe
  • 15. Book One – Aunt Gertrude’s Collection
  • 16. Book Two – Basic Cookbook
  • 17. Book Three – The Canadian Living Cookbook
  • 18. Book Three – The Canadian Living Cookbook
  • 19. How long did it take to find that Pot Roast recipe?  Book 1: Scanned every page in the book  Book 2: Looked in an index for the first page of section, turned to that page, then scanned each page of the section  Book 3: Located list of recipes in an index, scanned that list, then turned to that page
  • 20. We came here to talk about databases, right?
  • 21. How do we find records in tables? Similar to how we find recipes in cookbooks:  Scan  Seek, then Scan  Seek
  • 22. How are tables structured? As Pages (8 KB) in the data files, organized into Extents (8 page chunks)
  • 23. Page Types  Data  Index  Text/Image  Various types of Maps
  • 24. Table Data Structure: Heap  When a table doesn’t have a defined order its data rows are stored in an unordered structure.  Data order cannot be predicted.
  • 25. Table Data Structure: Clustered Index (CI)  When a table does have a defined order its data rows are stored in an ordered structure sorted by its key values.  Key values are the columns included in the index definition (eg: MealType)  There can be only one clustered index per table, because the data rows themselves can be stored in only one order.
  • 26. Optional Index: Nonclustered Index (NCI)  An ordered structure sorted by its key values that is separate from the data rows.  Entries contain the key values (eg: Keyword) and a pointer to the data row (ie: the row locator).
  • 27. Heap id index_id = 0 first_iam_page Index Allocation Map (IAM) Page Header Data Rows Page Header Data Rows Page Header Data Rows
  • 28. Clustered Index id index_id = 1 root_page Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Data Rows Previous Page Next Page Data Rows Previous Page Next Page Data Rows Root Node Intermediate Node Leaf Node
  • 29. Traversing the Tree  Lets find ‘Simpson, Kim’ in the Employee table  Key values are LastName, FirstName  Root node: Aaronson, Alan Fairchild, Meagan Morgan, Beverly Thompson, Sam
  • 30. Traversing the Tree  Lets find ‘Simpson, Kim’ in the Employee table  Key values are LastName, FirstName  Intermediate node: Morgan, Beverly Potter, Henry Ramsay, Delores Smith, Sam
  • 31. Traversing the Tree  Lets find ‘Simpson, Kim’ in the Employee table  Key values are LastName, FirstName  Leaf node: Ramsay, Delores, (1992-04-23, Sr Engineer) Rumsfeld, Dan, (2016-03-22, Engineer) Sanderson, Betty, (2019-08-19, Jr Engineer) Simpson, Kim, (2014-01-04, Engineer)
  • 32. Why are some queries slow? select FirstName, LastName from Person.Person where LastName = 'Simpson' vs select FirstName, LastName from Person.Person where FirstName = 'Kim'
  • 33. Nonclustered Index id index_id > 1 root_page Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Key Values Previous Page Next Page Key Values Previous Page Next Page Key Values Root Node Intermediate Node Leaf Node
  • 34. Nonclustered Index on Heap Previous Page Next Page Key Values Nonclustered Index Leaf Node IAM Page Header Data Rows Page Header Data Rows Page Header Data Rows
  • 35. Nonclustered Index on Clustered Index Previous Page Next Page Key Values Nonclustered Index Leaf Node Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Index Rows Previous Page Next Page Data Rows Previous Page Next Page Data Rows Previous Page Next Page Data Rows
  • 36. Finding Data Means Reading Pages Indexes = Good
  • 37. Indexable Queries: Finding Data  Check that the columns tested by your WHERE clause are in an index  Ditto for: JOIN conditions, HAVING clause  Don’t hide the column values to make them unusable by the index
  • 38. Implicit Indexes  Primary Key Constraints (PK)  Unique Key Constraints (AK) Both require uniqueness Your only option on table variables
  • 39. SARGable = Search ARGument ABLE  A SARGable predicate is one where SQL server can isolate the single value or range of index key values to process  Functions and operators can act like a black box around the column value, so the database engine can’t use it in an index and must scan -> Non-SARGable
  • 40. Are any functions SARGable? Not many:  LIKE (without a leading %)  =, >, >=, <, <=, IN, BETWEEN  IS NULL  Some Date functions  User Defined Functions that are basically parameterized views
  • 41. Indexable Queries: Clustering Data  Check that all the columns you’re asking for are covered by the index  Pay attention to the order of key values in the index. Even non-SARGable conditions aren’t that bad if they’re grouped together  Key values of the Clustered Index will be on the leaf pages of your Nonclustered Index
  • 42. Indexable Queries: Sorting Data  Check that your sort order is supported by the index.  Ditto for: GROUP BY  It works in reverse, too