SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 
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 Performanceoysteing
 
InnoDB Flushing and Checkpoints
InnoDB Flushing and CheckpointsInnoDB Flushing and Checkpoints
InnoDB Flushing and CheckpointsMIJIN AN
 
MySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxNeoClova
 
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsQuery Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsJaime Crespo
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain ExplainedJeremy Coates
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in ActionSveta Smirnova
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performanceoysteing
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexingMahabubur Rahaman
 
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
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Jaime Crespo
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLMorgan Tocker
 
How to Take Advantage of Optimizer Improvements in MySQL 8.0
How to Take Advantage of Optimizer Improvements in MySQL 8.0How to Take Advantage of Optimizer Improvements in MySQL 8.0
How to Take Advantage of Optimizer Improvements in MySQL 8.0Norvald Ryeng
 
Boost Performance With My S Q L 51 Partitions
Boost Performance With  My S Q L 51 PartitionsBoost Performance With  My S Q L 51 Partitions
Boost Performance With My S Q L 51 PartitionsPerconaPerformance
 
How to upgrade like a boss to MySQL 8.0 - PLE19
How to upgrade like a boss to MySQL 8.0 -  PLE19How to upgrade like a boss to MySQL 8.0 -  PLE19
How to upgrade like a boss to MySQL 8.0 - PLE19Alkin Tezuysal
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexingYoshinori Matsunobu
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition StrategyHamid J. Fard
 

Mais procurados (20)

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)
 
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
 
InnoDB Flushing and Checkpoints
InnoDB Flushing and CheckpointsInnoDB Flushing and Checkpoints
InnoDB Flushing and Checkpoints
 
MySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptx
 
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The BasicsQuery Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
Query Optimization with MySQL 8.0 and MariaDB 10.3: The Basics
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain Explained
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
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
 
Percona toolkit
Percona toolkitPercona toolkit
Percona toolkit
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
 
How to Take Advantage of Optimizer Improvements in MySQL 8.0
How to Take Advantage of Optimizer Improvements in MySQL 8.0How to Take Advantage of Optimizer Improvements in MySQL 8.0
How to Take Advantage of Optimizer Improvements in MySQL 8.0
 
Boost Performance With My S Q L 51 Partitions
Boost Performance With  My S Q L 51 PartitionsBoost Performance With  My S Q L 51 Partitions
Boost Performance With My S Q L 51 Partitions
 
Sql joins
Sql joinsSql joins
Sql joins
 
How to upgrade like a boss to MySQL 8.0 - PLE19
How to upgrade like a boss to MySQL 8.0 -  PLE19How to upgrade like a boss to MySQL 8.0 -  PLE19
How to upgrade like a boss to MySQL 8.0 - PLE19
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexing
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition Strategy
 

Último

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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, Adobeapidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
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 educationjfdjdjcjdnsjd
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
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...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
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.pdfsudhanshuwaghmare1
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 

Último (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

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