SlideShare a Scribd company logo
MySQL Index
                             Andy Yao




Friday, December 30, 11
MySQL Basic




Friday, December 30, 11
MySQL Architecture




Friday, December 30, 11
Storage Engine: MyISAM

                          Table lock

                          No automated data recovery

                          No transactions

                          Only indexes are cached in memory

                          Compact storage



Friday, December 30, 11
Storage Engine: InnoDB
                          Transactional

                          Foreign keys

                          Multiversioning

                          Clustered by primary key

                          No cached count(*)

                          Blocking AUTO_INCREMENT

                          Optimized cache


Friday, December 30, 11
InnoDB Storage Architecture
                          Tablespaces                              Segment
                 leaf node segment
                                                               extent        extent

             non-leaf node segment
                                                               extent        extent
              rollback node segment

                             ...

                                                                        Extent
                           Row
                                                  Page
                          trx id
                                            row          row
                          row id
                                            row          row
                     roll pointer
                                                   ...

             col 1         ...      col n                                  64 pages




Friday, December 30, 11
InnoDB and File system

                          	
  File	
  system	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  -­‐>	
  InnoDB
                          	
  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐
                          	
  disk	
  partition	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  -­‐>	
  tablespace
                          	
  file	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  -­‐>	
  segment
                          	
  inode	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  -­‐>	
  fsp0fsp.c	
  'inode'
                          	
  fs	
  space	
  allocation	
  unit	
  -­‐>	
  extent
                          	
  disk	
  block	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  -­‐>	
  page	
  (16	
  kB)




Friday, December 30, 11
Types of Indexes




Friday, December 30, 11
Types of Indexes


                          B+Tree indexes

                          Hash indexes

                          R-Tree indexes

                          Full-text indexes




Friday, December 30, 11
B+Tree Indexes




Friday, December 30, 11
Binary search tree

                     Binary search tree               AVL tree

                          2
                                                         6

                              3

                                                  3              7
                                  5

                                      7
                                              2          5           8

                                  6       8




Friday, December 30, 11
B Tree

                                          25    50   75    ...




              5       10   15   20   25   30          50   55    60   65   75   80   85   90




Friday, December 30, 11
B+Tree

                                          25    50   75    ...




              5       10   15   20   25   30          50   55    60   65   75   80   85   90




Friday, December 30, 11
B+Tree Indexes




Friday, December 30, 11
B+tree indexes: Demo

                              CREATE TABLE People (
                                 last_name varchar(50) not null,
                                 first_name varchar(50) not null,
                                 dob date not null,
                                 gender enum('m', 'f') not null,
                                 key(last_name, first_name, dob)
                              );




Friday, December 30, 11
B+tree indexes: Demo




Friday, December 30, 11
B+Tree: Types of queries

                          Match the full value
                            where last_name=? and first_name=? and bod=?


                          Match a leftmost prefix
                            where last_name=?

                            where last_name=?




Friday, December 30, 11
B+Tree: Types of queries


                          Match a column prefix
                            where last_name like ”Liang%”


                          Match a range of values
                            where last_name > ?




Friday, December 30, 11
B+Tree: Types of queries

                          Match one part exactly and match a range
                          on another
                            where last_name=? and first_name>?


                          Index only queries
                            select first_name where last_name=?




Friday, December 30, 11
B+Tree: Limitations


                          Doesn’t start from leftmost
                            where first_name=?


                          Skip columns in the index
                            where last_name=? and bod=?




Friday, December 30, 11
B+Tree: Limitations

                          More than one range conditions
                            where last_name>? and first_name>?


                          Can’t optimize to the right of the first range
                          conditon
                            where last_name>? and first_name=?




Friday, December 30, 11
Clustered Index




Friday, December 30, 11
Clustered Index

                          Non Clustered

                          Clustered



                          Index organized table

                          Heap table



Friday, December 30, 11
Friday, December 30, 11
Useful Commands




Friday, December 30, 11
Useful commands


                          show index from TABLE;

                          explain [extended] SELECT * FROM TABLE;
                            UPDATE, DELETE convert to SELECT




Friday, December 30, 11
Explain

                          select_tpe

                            simple, primary, subquery, derived, union

                          type

                            all < index < range < ref < eq_ref < const,
                            system < null



Friday, December 30, 11
Explain

                          Extra

                            using where

                            using index

                            using filesort

                            using temporary



Friday, December 30, 11
Indexing Strategies




Friday, December 30, 11
Indexing Strategies

                          Isolate the column

                          Prefix Indexes and Index Selectivity

                          Covering Indexes

                          Use Index Scan For Sorts




Friday, December 30, 11
Isolate the column


                          where last_name=”Fred”

                          where a+1=5

                          where md5(a)=”45c48cce2e2d7fbdea1afc5”




Friday, December 30, 11
Prefix index & Index
                               selectivity
                           Prefix index

                             KEY index_on_sum(`sum`(5))

                           Index Selectivity

                             Cardinality/Count(*)

                             0..1



Friday, December 30, 11
Covering Index


                          select first_name from people where
                          last_name=”fred”

                          Extra: Using index

                          Not support “Like” in query




Friday, December 30, 11
Using Index for Sorts

              select * from people
                 where last_name=? and first_name =?
                 order by dob




Friday, December 30, 11
Redundant/Duplicate index
                          Duplicate           Redundant

                            primary key(id)     key(a, b, c)

                            key(id)             key(a, b)

                            unique(id)          key(a)

                                                key(b, a)




Friday, December 30, 11
Others
                          Index merge

                          Or

                          Sub-query/Join/Union

                          Group/Order

                          Locking

                          Query optimization


Friday, December 30, 11
References

                          http://www.mysqlperformanceblog.com/

                          http://www.percona.com/




Friday, December 30, 11
Questions?




Friday, December 30, 11

More Related Content

What's hot

MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to Have
Sveta Smirnova
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
Mahabubur Rahaman
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
Joel Brewer
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
Georgi Sotirov
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MYXPLAIN
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLMorgan Tocker
 
Types of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal GulatiTypes of keys in database management system by Dr. Kamal Gulati
InnoDB Internal
InnoDB InternalInnoDB Internal
InnoDB Internal
mysqlops
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
MySQL JOINS
MySQL JOINSMySQL JOINS
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimizationBaohua Cai
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
Ankit Dubey
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
Sveta Smirnova
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
Salman Memon
 
MySQL Space Management
MySQL Space ManagementMySQL Space Management
MySQL Space Management
MIJIN AN
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
 

What's hot (20)

MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to Have
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
 
Types of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal GulatiTypes of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal Gulati
 
InnoDB Internal
InnoDB InternalInnoDB Internal
InnoDB Internal
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
MySQL JOINS
MySQL JOINSMySQL JOINS
MySQL JOINS
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimization
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
MySQL Space Management
MySQL Space ManagementMySQL Space Management
MySQL Space Management
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

Mysql index

  • 1. MySQL Index Andy Yao Friday, December 30, 11
  • 4. Storage Engine: MyISAM Table lock No automated data recovery No transactions Only indexes are cached in memory Compact storage Friday, December 30, 11
  • 5. Storage Engine: InnoDB Transactional Foreign keys Multiversioning Clustered by primary key No cached count(*) Blocking AUTO_INCREMENT Optimized cache Friday, December 30, 11
  • 6. InnoDB Storage Architecture Tablespaces Segment leaf node segment extent extent non-leaf node segment extent extent rollback node segment ... Extent Row Page trx id row row row id row row roll pointer ... col 1 ... col n 64 pages Friday, December 30, 11
  • 7. InnoDB and File system  File  system                            -­‐>  InnoDB  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  disk  partition                      -­‐>  tablespace  file                                          -­‐>  segment  inode                                        -­‐>  fsp0fsp.c  'inode'  fs  space  allocation  unit  -­‐>  extent  disk  block                              -­‐>  page  (16  kB) Friday, December 30, 11
  • 8. Types of Indexes Friday, December 30, 11
  • 9. Types of Indexes B+Tree indexes Hash indexes R-Tree indexes Full-text indexes Friday, December 30, 11
  • 11. Binary search tree Binary search tree AVL tree 2 6 3 3 7 5 7 2 5 8 6 8 Friday, December 30, 11
  • 12. B Tree 25 50 75 ... 5 10 15 20 25 30 50 55 60 65 75 80 85 90 Friday, December 30, 11
  • 13. B+Tree 25 50 75 ... 5 10 15 20 25 30 50 55 60 65 75 80 85 90 Friday, December 30, 11
  • 15. B+tree indexes: Demo CREATE TABLE People ( last_name varchar(50) not null, first_name varchar(50) not null, dob date not null, gender enum('m', 'f') not null, key(last_name, first_name, dob) ); Friday, December 30, 11
  • 16. B+tree indexes: Demo Friday, December 30, 11
  • 17. B+Tree: Types of queries Match the full value where last_name=? and first_name=? and bod=? Match a leftmost prefix where last_name=? where last_name=? Friday, December 30, 11
  • 18. B+Tree: Types of queries Match a column prefix where last_name like ”Liang%” Match a range of values where last_name > ? Friday, December 30, 11
  • 19. B+Tree: Types of queries Match one part exactly and match a range on another where last_name=? and first_name>? Index only queries select first_name where last_name=? Friday, December 30, 11
  • 20. B+Tree: Limitations Doesn’t start from leftmost where first_name=? Skip columns in the index where last_name=? and bod=? Friday, December 30, 11
  • 21. B+Tree: Limitations More than one range conditions where last_name>? and first_name>? Can’t optimize to the right of the first range conditon where last_name>? and first_name=? Friday, December 30, 11
  • 23. Clustered Index Non Clustered Clustered Index organized table Heap table Friday, December 30, 11
  • 26. Useful commands show index from TABLE; explain [extended] SELECT * FROM TABLE; UPDATE, DELETE convert to SELECT Friday, December 30, 11
  • 27. Explain select_tpe simple, primary, subquery, derived, union type all < index < range < ref < eq_ref < const, system < null Friday, December 30, 11
  • 28. Explain Extra using where using index using filesort using temporary Friday, December 30, 11
  • 30. Indexing Strategies Isolate the column Prefix Indexes and Index Selectivity Covering Indexes Use Index Scan For Sorts Friday, December 30, 11
  • 31. Isolate the column where last_name=”Fred” where a+1=5 where md5(a)=”45c48cce2e2d7fbdea1afc5” Friday, December 30, 11
  • 32. Prefix index & Index selectivity Prefix index KEY index_on_sum(`sum`(5)) Index Selectivity Cardinality/Count(*) 0..1 Friday, December 30, 11
  • 33. Covering Index select first_name from people where last_name=”fred” Extra: Using index Not support “Like” in query Friday, December 30, 11
  • 34. Using Index for Sorts select * from people where last_name=? and first_name =? order by dob Friday, December 30, 11
  • 35. Redundant/Duplicate index Duplicate Redundant primary key(id) key(a, b, c) key(id) key(a, b) unique(id) key(a) key(b, a) Friday, December 30, 11
  • 36. Others Index merge Or Sub-query/Join/Union Group/Order Locking Query optimization Friday, December 30, 11
  • 37. References http://www.mysqlperformanceblog.com/ http://www.percona.com/ Friday, December 30, 11