SlideShare a Scribd company logo
DATABASE 

INDEXES
#SperasoftTalks
Who We Are?
We are a team of professionals specializing in game
development, art production, online engineering and
creation of amazing products.
Our technology competencies include solid experience
and background in delivering scalable platforms and
online solutions. That serve millions of players all over
the world and run beyond amazing games.
Read more: http://www.sperasoft.com
•  RDBMS  store  data  only  in  Trees
•  Index  is  a  tree  in  terms  of  data  structure
•  a  Table  is  an  Index
•  a  Clustered  Index  is  a  Table  itself
•  a  Non-­‐clustered  Index  is  a  copy  of  data
•  all  Non-­‐clustered  Indexes  refer  to  Clustered  one
•  all  keys  in  Tree  Nodes  are  always  unique
The Simple Truth
•  Oracle  Database
•  SQL  Server
•  IBM  DB2
•  MySQL
•  PostgreSQL
•  Sybase
•  Informix
What’s Common Between
RDBMS  is  a  type  of  Database  Management  System  
that  stores  data  in  the  form  of  related  tables

RDBMS  is  a  Database  Management  System  that  is  
based  on  the  relaJonal  model  introduced  by  E.F.  
Codd

Data  is  stored  in  tables  and  the  relaJonships  among  
the  data  are  also  stored  in  tables
Relational Database Management Systems
•  Born  on  the  Isle  of  Portland  in  England  
in  1923
•  Died  in  Florida  US  in  2003,  aged  79
•  MathemaJc
•  Worked  for  IBM

Edgar Frank “Ted” Codd
•  Introduced  “A  RelaJonal  Model  of  Data  for  Large  
Shared  Data  Banks”  and  Alpha  database  language
•  IBM  started  implemenJng  the  RelaJonal  model  and  
introduced  another  language  named  SEQUEL
Edgar Frank “Ted” Codd
•  Larry  Ellison  came  up  in  Jme  with  his  
implementaJon  of  RelaJonal  model  
and  the  language  –  Oracle  Database  and  
SQL
•  ANSI  started  making  SQL  standard
Birth of Oracle
•  It’s  all  about  Table  RelaJons
Relation Model Briefly
•  Database  contains  tables  (two  dimensional  
arrays)
•  Tables  have  relaJonships  enforced  by  Foreign  
Key  constraints  (1-­‐to-­‐Many  relaJonship)
•  NormalizaJon  of  tables  is  a  key  concept
•  That’s  why  RDBMS  are  called  RelaJonal
Relation Model
What’s Database Physically
•  Files  are  flat  in  nature
FILE
READING
CURSOR
0
 OFFSET
All Tables Are Stored in a File
•  What’s  the  value  behind  relaJons?

•  What  is  a  database  table?
•  What  is  a  table  index?
•  RelaJons  vs  How  data  is  stored
What’s Actually Matter
Id
 User  Name
 Country
 City
 Age
1
 Michael
 USA
 Boston
 30
2
 Jane
 USA
 Boston
 24
3
 Scoe
 USA
 NYC
 18
4
 Bob
 UK
 London
 41
5
 Prescoe
 UK
 London
 35
•  Such  array  seems  to  be  a  table
•  How  to  find  Users  from  Boston  faster?
ArrayList<User>  users  =  new  ArrayList<User>();
How to Handle Millions of Users
Boston
1,  Michael,  USA,  Boston,  30
2,  Jane,  USA,  Boston,  24
NYC
3,  Scoe,  USA,  NYC,  18  
London
4,  Bob,  UK,  London,  41
5,  Prescoe,  UK,  London,  35  
Index is a Tree
ID  =  2
2,  Jane,  USA,  Boston,  24
ID  =  1
1,  Michael,  USA,  Boston,  30
ID  =  3
3,  Scoe,  USA,  NYC,  18  
Can replace an initial array with Index
•  Key  values  in  a  Key  node  should  be  unique
•  Otherwise  Trees  do  not  work
What’s important to note
•  Indexes  are  Trees  in  terms  of  data  structure
•  Trees  are  suitable  to  store  any  array  of  data  to  
make  search  faster
Returning to our sheep
•  All  RDBMS  store  data  as  Balanced  Trees
•  The  concrete  implementaJon  of  B-­‐Tree  could  
differ  from  vendor  to  vendor
•  It  means  the  only  way  to  store  data  is  Tree
•  No  excepJons  here  -­‐  table  is  a  tree,  index  is  a  
tree
Balanced Trees
What’s a Clustered Index
•  The  next  record  in  Clustered  Index  is  always  
stored  aoer  the  previous  one

RECORD  1
 RECORD  2
1    |  Michael  |  USA  |  Boston  |  30
 2    |  Jane  |  USA  |  Boston  |  24
The clustered index storage
Have  a  quesKon?  
Like  this  deck?  


Tweet  us  @SperasoR
Like  deck  on  SlideShare.com/sperasoR
•  Clustered  Indexes
•  Non-­‐clustered  indexes
•  Both  could  be  unique  and  non-­‐unique
•  Table  can  be  without  any  indexes
•  How  is  that  comply  with  how  data  is  actually  
stored?
What SQL allows us to do
•  Unique  and  non-­‐unique
•  CREATE  CLUSTERED  INDEX  [name]  ON  
[table_name]  ([column1],  [column2])
•  CREATE  UNIQUE  CLUSTERED  INDEX  [name]  
ON  [table_name]  ([column1],  [column2])
Clustered Indexes
•  Unique  and  non-­‐unique
•  CREATE  NONCLUSTERED  INDEX  [name]  ON  
[table_name]  ([column1],  [column2])
•  CREATE  UNIQUE  NONCLUSTERED  INDEX  
[name]  ON  [table_name]  ([column1],  
[column2])
None Clustered Indexes
ID  =  2
Jane,  USA,  Boston,  24
ID  =  1
Michael,  USA,  Boston,  30
ID  =  3
Scoe,  USA,  NYC,  18  
Unique Clustered Index
•  We  know  Key  values  should  be  unique
•  How  RDBMS  resolves  this  problem?
Non-unique Clustered Index
•  SQL  Server  adds  4-­‐byte  uniquifier  to  each  
duplicated  key  value
•  Algorithms  could  differ  from  vendor  to  vendor
•  But  the  principle  is  the  same  –  add  something  
to  make  them  unique
Non-unique Clustered Index
•  Just  omitng  Unique  keyword  makes  Key  values  
bigger  (why  it’s  bad  realize  later)
•  The  simple  truth  is  that  Each  table  should  have  
Clustered  Index
•  The  Clustered  Index  should  be  always  Unique
•  The  situaJons  when  its  not  so  should  be  excepJonal
Clustered Indexes
•  Such  tables  are  called  Heap  Tables

•  How  are  they  stored  in  database  if  they  do  not  
have  a  Key  value  specified?
Tables without Clustered Index
•  Heap  Tables  are  also  stored  in  Trees
•  What’s  in  a  Key  value  for  Tables  without  
Clustered  Index?
•  The  value  called  RID
•  the  unique  idenJfier  which  refers  to  the  
physical  locaJon  of  the  record  in  a  file

No magic over here
•  There  is  no  meaningful  data  in  Keys
•  Table  records  are  not  stored  physically  in  
Keys’  order
Why Heap Tables are so bad
•  Clustered  Index  has  the  actual  data  columns  in  Leaf-­‐
nodes
•  What’s  in  Leaf-­‐node  of  Non-­‐clustered  index?
•  Remember  that  Non-­‐clustered  Indexes  are  
duplicated  data
Non-clustered Indexes
Jane
Lookup  value:  ID=2
Michael
Lookup  value:  ID=1
Scoe
Lookup  value:  ID=3
•  Leaf-­‐nodes  contain  the  lookup  values
•  Lookup  value  is  Clustered  Index’s  Key
Non-clustered Index
•  We  know  Key  values  should  be  unique
•  How  non-­‐clustered  index’s  key  becomes  
unique?
Non-unique Non-clustered Index
•  SQL  Server  adds  Clustered  Index  Key  value  to  
Non-­‐clustered  Index  Key  value  to  make  it  
unique
Jane,  2
Lookup  value:  ID=2
Michael,  1
Lookup  value:  ID=1
Scoe,  3
Lookup  value:  ID=3
Non-unique Non-clustered Index
•  from  SELECT  statement  the  WHERE  condiJon  
is  taken
•  based  on  the  Columns  in  WHERE  we  know  
what  columns  we  search  by
•  look  through  available  indexes  trying  to  find  
the  appropriate  one,  starJng  from  Clustered
•  found  out  non-­‐clustered  index  which  fits  best
How indexes are used (1)
•  get  the  needed  Node  in  Non-­‐clustered  index
•  get  the  Lookup  value  from  that  Node
•  use  that  lookup  value  to  find  a  record  in  
Clustered  index
•  get  selected  columns  from  Clustered  index  
(table  itself)
How indexes are used (2)
•  Unique  Clustered  Index  on  Id  column
•  Non-­‐unique  Non-­‐clustered  Index  on  City  column
•  Select  UserName  from  tbl  where  City  =  ‘Boston’
Sample 1
•  Unique  Clustered  Index  on  Id  column
•  Non-­‐unique  Non-­‐clustered  Index  on  City  column
•  Select  Id  from  tbl  where  City  =  ‘Boston’
Sample 2
•  Unique  Clustered  Index  on  Id  column
•  Non-­‐unique  Non-­‐clustered  Index  on  City  column
•  Select  UserName  from  tbl  where  City  =  ‘Boston’  
select  should  not  go  to  Clustered  Index
Sample 3
•  Unique  Clustered  Index  on  Id,  UserName  column
•  Select  Id  from  tbl  where  City  =  ‘Boston’  and  
UserName  =  ‘Michael’
•  What  columns  Non-­‐unique  Non-­‐clustered  Index  
would  include?
Sample 1
WE  ARE  SPERASOFT  
DELIVERING  AMAZING  
PRODUCTS

Follow  us:    
@SperasoA  
  
hCp://www.sperasoA.com

More Related Content

What's hot (20)

PPTX
Relational databases
Fiddy Prasetiya
 
PPTX
Database index
Riteshkiit
 
PPT
Information Retrieval Models
Nisha Arankandath
 
ZIP
NoSQL databases
Harri Kauhanen
 
PPTX
An Introduction To NoSQL & MongoDB
Lee Theobald
 
PDF
MS-SQL SERVER ARCHITECTURE
Douglas Bernardini
 
PPT
9. Document Oriented Databases
Fabio Fumarola
 
PDF
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
PPTX
Data Warehouse
MadhuriNigam1
 
PPT
Data Warehousing and Data Mining
idnats
 
PPTX
Relational databases vs Non-relational databases
James Serra
 
PPTX
Mongodb basics and architecture
Bishal Khanal
 
PPTX
Database : Relational Data Model
Smriti Jain
 
PPT
Relational Database Fundamentals
KHALID C
 
PPT
Database structure
Forrester High School
 
PPTX
Sql server ___________session_17(indexes)
Ehtisham Ali
 
PDF
Data Warehouse Concepts | Data Warehouse Tutorial | Data Warehousing | Edureka
Edureka!
 
PPTX
HBase in Practice
DataWorks Summit/Hadoop Summit
 
PDF
SQL Joins With Examples | Edureka
Edureka!
 
PPTX
Data warehouse
Yogendra Uikey
 
Relational databases
Fiddy Prasetiya
 
Database index
Riteshkiit
 
Information Retrieval Models
Nisha Arankandath
 
NoSQL databases
Harri Kauhanen
 
An Introduction To NoSQL & MongoDB
Lee Theobald
 
MS-SQL SERVER ARCHITECTURE
Douglas Bernardini
 
9. Document Oriented Databases
Fabio Fumarola
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
Data Warehouse
MadhuriNigam1
 
Data Warehousing and Data Mining
idnats
 
Relational databases vs Non-relational databases
James Serra
 
Mongodb basics and architecture
Bishal Khanal
 
Database : Relational Data Model
Smriti Jain
 
Relational Database Fundamentals
KHALID C
 
Database structure
Forrester High School
 
Sql server ___________session_17(indexes)
Ehtisham Ali
 
Data Warehouse Concepts | Data Warehouse Tutorial | Data Warehousing | Edureka
Edureka!
 
HBase in Practice
DataWorks Summit/Hadoop Summit
 
SQL Joins With Examples | Edureka
Edureka!
 
Data warehouse
Yogendra Uikey
 

Similar to Database Indexes (20)

PDF
Indexing techniques
Huda Alameen
 
PPTX
Sql performance tuning
Leo Mark Villar
 
PPTX
Index_2
Riteshkiit
 
PPTX
We Don't Need Roads: A Developers Look Into SQL Server Indexes
Richie Rump
 
PPTX
Geek Sync | Understand Indexes to Write Better Queries
IDERA Software
 
PPTX
Ms sql server tips 1 0
Arman Nasrollahi
 
PPTX
dotnetMALAGA - Sql query tuning guidelines
Javier García Magna
 
PPT
Intro to tsql unit 7
Syed Asrarali
 
PDF
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Polish SQL Server User Group
 
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
PPTX
9. index and index organized table
Amrit Kaur
 
PPS
07 qmds2005 session10
Niit Care
 
PPTX
Sql server lesson6
Ala Qunaibi
 
PPTX
Database indexing techniques
ahmadmughal0312
 
PPTX
File Organization in database management.pptx
ubaidullah75790
 
PPT
Intro to Data warehousing lecture 11
AnwarrChaudary
 
PPT
Intro to Data warehousing lecture 14
AnwarrChaudary
 
PPT
Intro to Data warehousing lecture 19
AnwarrChaudary
 
PDF
Sql
shenazk
 
PDF
Sql
sanjaynuru
 
Indexing techniques
Huda Alameen
 
Sql performance tuning
Leo Mark Villar
 
Index_2
Riteshkiit
 
We Don't Need Roads: A Developers Look Into SQL Server Indexes
Richie Rump
 
Geek Sync | Understand Indexes to Write Better Queries
IDERA Software
 
Ms sql server tips 1 0
Arman Nasrollahi
 
dotnetMALAGA - Sql query tuning guidelines
Javier García Magna
 
Intro to tsql unit 7
Syed Asrarali
 
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Polish SQL Server User Group
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
9. index and index organized table
Amrit Kaur
 
07 qmds2005 session10
Niit Care
 
Sql server lesson6
Ala Qunaibi
 
Database indexing techniques
ahmadmughal0312
 
File Organization in database management.pptx
ubaidullah75790
 
Intro to Data warehousing lecture 11
AnwarrChaudary
 
Intro to Data warehousing lecture 14
AnwarrChaudary
 
Intro to Data warehousing lecture 19
AnwarrChaudary
 
Sql
shenazk
 
Ad

More from Sperasoft (20)

PDF
особенности работы с Locomotion в Unreal Engine 4
Sperasoft
 
PDF
концепт и архитектура геймплея в Creach: The Depleted World
Sperasoft
 
PPTX
Опыт разработки VR игры для UE4
Sperasoft
 
PPTX
Организация работы с UE4 в команде до 20 человек
Sperasoft
 
PPTX
Gameplay Tags
Sperasoft
 
PDF
Data Driven Gameplay in UE4
Sperasoft
 
PPTX
Code and Memory Optimisation Tricks
Sperasoft
 
PPTX
The theory of relational databases
Sperasoft
 
PPTX
Automated layout testing using Galen Framework
Sperasoft
 
PDF
Sperasoft talks: Android Security Threats
Sperasoft
 
PDF
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Sperasoft
 
PDF
Sperasoft‬ talks j point 2015
Sperasoft
 
PDF
Effective Мeetings
Sperasoft
 
PDF
Unreal Engine 4 Introduction
Sperasoft
 
PDF
JIRA Development
Sperasoft
 
PDF
Introduction to Elasticsearch
Sperasoft
 
PDF
MOBILE DEVELOPMENT with HTML, CSS and JS
Sperasoft
 
PDF
Quick Intro Into Kanban
Sperasoft
 
PDF
ECMAScript 6 Review
Sperasoft
 
PDF
Console Development in 15 minutes
Sperasoft
 
особенности работы с Locomotion в Unreal Engine 4
Sperasoft
 
концепт и архитектура геймплея в Creach: The Depleted World
Sperasoft
 
Опыт разработки VR игры для UE4
Sperasoft
 
Организация работы с UE4 в команде до 20 человек
Sperasoft
 
Gameplay Tags
Sperasoft
 
Data Driven Gameplay in UE4
Sperasoft
 
Code and Memory Optimisation Tricks
Sperasoft
 
The theory of relational databases
Sperasoft
 
Automated layout testing using Galen Framework
Sperasoft
 
Sperasoft talks: Android Security Threats
Sperasoft
 
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Sperasoft
 
Sperasoft‬ talks j point 2015
Sperasoft
 
Effective Мeetings
Sperasoft
 
Unreal Engine 4 Introduction
Sperasoft
 
JIRA Development
Sperasoft
 
Introduction to Elasticsearch
Sperasoft
 
MOBILE DEVELOPMENT with HTML, CSS and JS
Sperasoft
 
Quick Intro Into Kanban
Sperasoft
 
ECMAScript 6 Review
Sperasoft
 
Console Development in 15 minutes
Sperasoft
 
Ad

Recently uploaded (20)

PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Learn Computer Forensics, Second Edition
AnuraShantha7
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Q2 Leading a Tableau User Group - Onboarding
lward7
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Learn Computer Forensics, Second Edition
AnuraShantha7
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Q2 Leading a Tableau User Group - Onboarding
lward7
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 

Database Indexes

  • 2. Who We Are? We are a team of professionals specializing in game development, art production, online engineering and creation of amazing products. Our technology competencies include solid experience and background in delivering scalable platforms and online solutions. That serve millions of players all over the world and run beyond amazing games. Read more: http://www.sperasoft.com
  • 3. •  RDBMS  store  data  only  in  Trees •  Index  is  a  tree  in  terms  of  data  structure •  a  Table  is  an  Index •  a  Clustered  Index  is  a  Table  itself •  a  Non-­‐clustered  Index  is  a  copy  of  data •  all  Non-­‐clustered  Indexes  refer  to  Clustered  one •  all  keys  in  Tree  Nodes  are  always  unique The Simple Truth
  • 4. •  Oracle  Database •  SQL  Server •  IBM  DB2 •  MySQL •  PostgreSQL •  Sybase •  Informix What’s Common Between
  • 5. RDBMS  is  a  type  of  Database  Management  System   that  stores  data  in  the  form  of  related  tables RDBMS  is  a  Database  Management  System  that  is   based  on  the  relaJonal  model  introduced  by  E.F.   Codd Data  is  stored  in  tables  and  the  relaJonships  among   the  data  are  also  stored  in  tables Relational Database Management Systems
  • 6. •  Born  on  the  Isle  of  Portland  in  England   in  1923 •  Died  in  Florida  US  in  2003,  aged  79 •  MathemaJc •  Worked  for  IBM Edgar Frank “Ted” Codd
  • 7. •  Introduced  “A  RelaJonal  Model  of  Data  for  Large   Shared  Data  Banks”  and  Alpha  database  language •  IBM  started  implemenJng  the  RelaJonal  model  and   introduced  another  language  named  SEQUEL Edgar Frank “Ted” Codd
  • 8. •  Larry  Ellison  came  up  in  Jme  with  his   implementaJon  of  RelaJonal  model   and  the  language  –  Oracle  Database  and   SQL •  ANSI  started  making  SQL  standard Birth of Oracle
  • 9. •  It’s  all  about  Table  RelaJons Relation Model Briefly
  • 10. •  Database  contains  tables  (two  dimensional   arrays) •  Tables  have  relaJonships  enforced  by  Foreign   Key  constraints  (1-­‐to-­‐Many  relaJonship) •  NormalizaJon  of  tables  is  a  key  concept •  That’s  why  RDBMS  are  called  RelaJonal Relation Model
  • 12. •  Files  are  flat  in  nature FILE READING CURSOR 0 OFFSET All Tables Are Stored in a File
  • 13. •  What’s  the  value  behind  relaJons? •  What  is  a  database  table? •  What  is  a  table  index? •  RelaJons  vs  How  data  is  stored What’s Actually Matter
  • 14. Id User  Name Country City Age 1 Michael USA Boston 30 2 Jane USA Boston 24 3 Scoe USA NYC 18 4 Bob UK London 41 5 Prescoe UK London 35 •  Such  array  seems  to  be  a  table •  How  to  find  Users  from  Boston  faster? ArrayList<User>  users  =  new  ArrayList<User>(); How to Handle Millions of Users
  • 15. Boston 1,  Michael,  USA,  Boston,  30 2,  Jane,  USA,  Boston,  24 NYC 3,  Scoe,  USA,  NYC,  18   London 4,  Bob,  UK,  London,  41 5,  Prescoe,  UK,  London,  35   Index is a Tree
  • 16. ID  =  2 2,  Jane,  USA,  Boston,  24 ID  =  1 1,  Michael,  USA,  Boston,  30 ID  =  3 3,  Scoe,  USA,  NYC,  18   Can replace an initial array with Index
  • 17. •  Key  values  in  a  Key  node  should  be  unique •  Otherwise  Trees  do  not  work What’s important to note
  • 18. •  Indexes  are  Trees  in  terms  of  data  structure •  Trees  are  suitable  to  store  any  array  of  data  to   make  search  faster Returning to our sheep
  • 19. •  All  RDBMS  store  data  as  Balanced  Trees •  The  concrete  implementaJon  of  B-­‐Tree  could   differ  from  vendor  to  vendor •  It  means  the  only  way  to  store  data  is  Tree •  No  excepJons  here  -­‐  table  is  a  tree,  index  is  a   tree Balanced Trees
  • 21. •  The  next  record  in  Clustered  Index  is  always   stored  aoer  the  previous  one RECORD  1 RECORD  2 1    |  Michael  |  USA  |  Boston  |  30 2    |  Jane  |  USA  |  Boston  |  24 The clustered index storage
  • 22. Have  a  quesKon?   Like  this  deck?   Tweet  us  @SperasoR Like  deck  on  SlideShare.com/sperasoR
  • 23. •  Clustered  Indexes •  Non-­‐clustered  indexes •  Both  could  be  unique  and  non-­‐unique •  Table  can  be  without  any  indexes •  How  is  that  comply  with  how  data  is  actually   stored? What SQL allows us to do
  • 24. •  Unique  and  non-­‐unique •  CREATE  CLUSTERED  INDEX  [name]  ON   [table_name]  ([column1],  [column2]) •  CREATE  UNIQUE  CLUSTERED  INDEX  [name]   ON  [table_name]  ([column1],  [column2]) Clustered Indexes
  • 25. •  Unique  and  non-­‐unique •  CREATE  NONCLUSTERED  INDEX  [name]  ON   [table_name]  ([column1],  [column2]) •  CREATE  UNIQUE  NONCLUSTERED  INDEX   [name]  ON  [table_name]  ([column1],   [column2]) None Clustered Indexes
  • 26. ID  =  2 Jane,  USA,  Boston,  24 ID  =  1 Michael,  USA,  Boston,  30 ID  =  3 Scoe,  USA,  NYC,  18   Unique Clustered Index
  • 27. •  We  know  Key  values  should  be  unique •  How  RDBMS  resolves  this  problem? Non-unique Clustered Index
  • 28. •  SQL  Server  adds  4-­‐byte  uniquifier  to  each   duplicated  key  value •  Algorithms  could  differ  from  vendor  to  vendor •  But  the  principle  is  the  same  –  add  something   to  make  them  unique Non-unique Clustered Index
  • 29. •  Just  omitng  Unique  keyword  makes  Key  values   bigger  (why  it’s  bad  realize  later) •  The  simple  truth  is  that  Each  table  should  have   Clustered  Index •  The  Clustered  Index  should  be  always  Unique •  The  situaJons  when  its  not  so  should  be  excepJonal Clustered Indexes
  • 30. •  Such  tables  are  called  Heap  Tables •  How  are  they  stored  in  database  if  they  do  not   have  a  Key  value  specified? Tables without Clustered Index
  • 31. •  Heap  Tables  are  also  stored  in  Trees •  What’s  in  a  Key  value  for  Tables  without   Clustered  Index? •  The  value  called  RID •  the  unique  idenJfier  which  refers  to  the   physical  locaJon  of  the  record  in  a  file No magic over here
  • 32. •  There  is  no  meaningful  data  in  Keys •  Table  records  are  not  stored  physically  in   Keys’  order Why Heap Tables are so bad
  • 33. •  Clustered  Index  has  the  actual  data  columns  in  Leaf-­‐ nodes •  What’s  in  Leaf-­‐node  of  Non-­‐clustered  index? •  Remember  that  Non-­‐clustered  Indexes  are   duplicated  data Non-clustered Indexes
  • 34. Jane Lookup  value:  ID=2 Michael Lookup  value:  ID=1 Scoe Lookup  value:  ID=3 •  Leaf-­‐nodes  contain  the  lookup  values •  Lookup  value  is  Clustered  Index’s  Key Non-clustered Index
  • 35. •  We  know  Key  values  should  be  unique •  How  non-­‐clustered  index’s  key  becomes   unique? Non-unique Non-clustered Index
  • 36. •  SQL  Server  adds  Clustered  Index  Key  value  to   Non-­‐clustered  Index  Key  value  to  make  it   unique Jane,  2 Lookup  value:  ID=2 Michael,  1 Lookup  value:  ID=1 Scoe,  3 Lookup  value:  ID=3 Non-unique Non-clustered Index
  • 37. •  from  SELECT  statement  the  WHERE  condiJon   is  taken •  based  on  the  Columns  in  WHERE  we  know   what  columns  we  search  by •  look  through  available  indexes  trying  to  find   the  appropriate  one,  starJng  from  Clustered •  found  out  non-­‐clustered  index  which  fits  best How indexes are used (1)
  • 38. •  get  the  needed  Node  in  Non-­‐clustered  index •  get  the  Lookup  value  from  that  Node •  use  that  lookup  value  to  find  a  record  in   Clustered  index •  get  selected  columns  from  Clustered  index   (table  itself) How indexes are used (2)
  • 39. •  Unique  Clustered  Index  on  Id  column •  Non-­‐unique  Non-­‐clustered  Index  on  City  column •  Select  UserName  from  tbl  where  City  =  ‘Boston’ Sample 1
  • 40. •  Unique  Clustered  Index  on  Id  column •  Non-­‐unique  Non-­‐clustered  Index  on  City  column •  Select  Id  from  tbl  where  City  =  ‘Boston’ Sample 2
  • 41. •  Unique  Clustered  Index  on  Id  column •  Non-­‐unique  Non-­‐clustered  Index  on  City  column •  Select  UserName  from  tbl  where  City  =  ‘Boston’   select  should  not  go  to  Clustered  Index Sample 3
  • 42. •  Unique  Clustered  Index  on  Id,  UserName  column •  Select  Id  from  tbl  where  City  =  ‘Boston’  and   UserName  =  ‘Michael’ •  What  columns  Non-­‐unique  Non-­‐clustered  Index   would  include? Sample 1
  • 43. WE  ARE  SPERASOFT   DELIVERING  AMAZING   PRODUCTS Follow  us:     @SperasoA     hCp://www.sperasoA.com