SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
Naresh Kumar
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
emailharmeet
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 

Was ist angesagt? (20)

Relational database
Relational databaseRelational database
Relational database
 
Database abstraction
Database abstractionDatabase abstraction
Database abstraction
 
Data Models
Data ModelsData Models
Data Models
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Linked lists
Linked listsLinked lists
Linked lists
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
 
Relational model
Relational modelRelational model
Relational model
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Partitioning
PartitioningPartitioning
Partitioning
 
Files Vs DataBase
Files Vs DataBaseFiles Vs DataBase
Files Vs DataBase
 
Hive and HiveQL - Module6
Hive and HiveQL - Module6Hive and HiveQL - Module6
Hive and HiveQL - Module6
 
Referential integrity
Referential integrityReferential integrity
Referential integrity
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Data Dictionary
Data DictionaryData Dictionary
Data Dictionary
 

Ähnlich wie Database Indexes

SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET DeveloperSQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Polish SQL Server User Group
 
Data Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptxData Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptx
TusharAgarwal49094
 
Csis110 trzos-c1 ms access
Csis110 trzos-c1 ms accessCsis110 trzos-c1 ms access
Csis110 trzos-c1 ms access
tomtrzos
 
We Don't Need Roads: A Developers Look Into SQL Server Indexes
We Don't Need Roads: A Developers Look Into SQL Server IndexesWe Don't Need Roads: A Developers Look Into SQL Server Indexes
We Don't Need Roads: A Developers Look Into SQL Server Indexes
Richie Rump
 

Ähnlich wie Database Indexes (20)

SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET DeveloperSQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
 
No sql or Not only SQL
No sql or Not only SQLNo sql or Not only SQL
No sql or Not only SQL
 
0929 databases
0929 databases0929 databases
0929 databases
 
Data Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptxData Never Lies Presentation for beginners in data field.pptx
Data Never Lies Presentation for beginners in data field.pptx
 
Geek Sync | SQL Server Indexing Basics
Geek Sync | SQL Server Indexing BasicsGeek Sync | SQL Server Indexing Basics
Geek Sync | SQL Server Indexing Basics
 
Dev traning 2016 databases
Dev traning 2016   databasesDev traning 2016   databases
Dev traning 2016 databases
 
Index
IndexIndex
Index
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
demo2.ppt
demo2.pptdemo2.ppt
demo2.ppt
 
Csis110 trzos-c1 ms access
Csis110 trzos-c1 ms accessCsis110 trzos-c1 ms access
Csis110 trzos-c1 ms access
 
SQL Joins Basic and Fundamentals
SQL Joins Basic and FundamentalsSQL Joins Basic and Fundamentals
SQL Joins Basic and Fundamentals
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
We Don't Need Roads: A Developers Look Into SQL Server Indexes
We Don't Need Roads: A Developers Look Into SQL Server IndexesWe Don't Need Roads: A Developers Look Into SQL Server Indexes
We Don't Need Roads: A Developers Look Into SQL Server Indexes
 
02222016
0222201602222016
02222016
 
Index_2
Index_2Index_2
Index_2
 
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
SqlDay 2018 - Brief introduction into SQL Server Execution PlansSqlDay 2018 - Brief introduction into SQL Server Execution Plans
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
 
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERAGeek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
 
SQL
SQLSQL
SQL
 
NOsql Presentation.pdf
NOsql Presentation.pdfNOsql Presentation.pdf
NOsql Presentation.pdf
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysis
 

Mehr von Sperasoft

Mehr von Sperasoft (20)

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

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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