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

Weitere ähnliche Inhalte

Was ist angesagt?

Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)
Aleksandr Kuzminsky
 

Was ist angesagt? (20)

[Meetup] a successful migration from elastic search to clickhouse
[Meetup] a successful migration from elastic search to clickhouse[Meetup] a successful migration from elastic search to clickhouse
[Meetup] a successful migration from elastic search to clickhouse
 
Fun with click house window functions webinar slides 2021-08-19
Fun with click house window functions webinar slides  2021-08-19Fun with click house window functions webinar slides  2021-08-19
Fun with click house window functions webinar slides 2021-08-19
 
Altinity Quickstart for ClickHouse-2202-09-15.pdf
Altinity Quickstart for ClickHouse-2202-09-15.pdfAltinity Quickstart for ClickHouse-2202-09-15.pdf
Altinity Quickstart for ClickHouse-2202-09-15.pdf
 
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
 
Adventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree EngineAdventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree Engine
 
SQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftjSQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftj
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
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
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
 
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
 
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
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
 
Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)Recovery of lost or corrupted inno db tables(mysql uc 2010)
Recovery of lost or corrupted inno db tables(mysql uc 2010)
 
Creating Beautiful Dashboards with Grafana and ClickHouse
Creating Beautiful Dashboards with Grafana and ClickHouseCreating Beautiful Dashboards with Grafana and ClickHouse
Creating Beautiful Dashboards with Grafana and ClickHouse
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain Explained
 
ClickHouse Features for Advanced Users, by Aleksei Milovidov
ClickHouse Features for Advanced Users, by Aleksei MilovidovClickHouse Features for Advanced Users, by Aleksei Milovidov
ClickHouse Features for Advanced Users, by Aleksei Milovidov
 
A Fast Intro to Fast Query with ClickHouse, by Robert Hodges
A Fast Intro to Fast Query with ClickHouse, by Robert HodgesA Fast Intro to Fast Query with ClickHouse, by Robert Hodges
A Fast Intro to Fast Query with ClickHouse, by Robert Hodges
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

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