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

[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 clickhouseVianney FOUCAULT
 
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-19Altinity Ltd
 
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.pdfAltinity Ltd
 
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
 
Adventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree EngineAdventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree EngineAltinity Ltd
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQLDag H. Wanvik
 
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 Traceoysteing
 
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 joinsDeepthi Rachumallu
 
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...Altinity Ltd
 
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
 
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
 
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 CEOAltinity Ltd
 
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
 
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 ClickHouseAltinity Ltd
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain ExplainedJeremy Coates
 
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 MilovidovAltinity Ltd
 
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 HodgesAltinity Ltd
 

Mais procurados (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
 

Último

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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?Igalia
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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.pdfUK Journal
 
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 RobisonAnna Loughnan Colquhoun
 
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 productivityPrincipled Technologies
 
[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.pdfhans926745
 
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 Scriptwesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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.pptxEarley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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...Drew Madelung
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
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 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Último (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
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
 
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
 
[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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

MySQL Indexing Strategies

  • 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