SlideShare une entreprise Scribd logo
1  sur  32
MySQL Optimization

   Ha noi php day 2009
   vanchinh@gmail.com



19/12/2009              Ha Noi - 2009   1
Content
   Introduction
   Example
   Indexing Principles
   Considerations
   Explain
   General discussion



19/12/2009        Ha Noi - 2009   2
Introduction

   Ha noi php day 2009
   vanchinh@gmail.com



19/12/2009              Ha Noi - 2009   3
Who am i
   I working in NaisCorp (socbay.com)
   I’ve been using MySQL personally for
    2 years and professionally for over 1 years.
   Contact info:
     Personal: vanchinh@gmail.com
     work: chinhnv@socbay.com

     Web: http://ffx.socbay.com




19/12/2009         Ha Noi - 2009         4
Example

   Ha noi php day 2009
   vanchinh@gmail.com



19/12/2009              Ha Noi - 2009   5
Example
   SELECT * FROM news WHERE cate_id =1
    ORDER BY createdate DESC LIMIT 0,10;
   SELECT * FROM news WHERE cate_id =1 or
    cate_id=2 ORDER BY createdate DESC
    LIMIT 0,10;
   SELECT * FROM news WHERE id <
    12345678 ORDER BY createdate DESC LIMIT
    0, 10

19/12/2009     Ha Noi - 2009     6
Example
   Query Parsing and Optimizing
                  Query
                            Parse
    Thread   1
    Thread   2
    Thread   3             Optimize
    Thread   4
                                                                        MyISAM Key Buffer




                                             Engine API
                                                            MyISAM
     …
    Thread   5




                                              Storage
                            Execute                       (ha_myisam)
                            (fetch)                                       *.MYD, *.MYI
    Thread n
                           (update)                                     InnoDB Key Buffer
                                                            InnoDB
                                                          (ha_innodb)         ibdataN
                 Results
                           Group By


                           Having


                           Order By




Group By and Order By can be done with indexes instead.

19/12/2009                          Ha Noi - 2009                         7
Example
   Query Parsing and Optimizing
     All parsing and optimizing steps are the same for all
      storage engines
     All ORDER BY / GROUP BY steps are the same

     The only difference for a given storage engine is in
      actually fetching the rows
     Some types of indexes don’t exist on some table
      types: e.g. FULLTEXT and SPATIAL indexes don’t
      exist for InnoDB

19/12/2009          Ha Noi - 2009            8
Example
   Explain SELECT * FROM news WHERE
    cate_id =1 ORDER BY createdate DESC
    LIMIT 0,10;
   Run time: 0.9724s




19/12/2009     Ha Noi - 2009     9
Example

    WHERE cate_id =1       Order By createdate   Limit 0,10




             2.000.000         2.000.000
                                                      10




19/12/2009               Ha Noi - 2009           10
Indexing Principles


   Ha noi php day 2009
   vanchinh@gmail.com



19/12/2009              Ha Noi - 2009   11
Optimization – Indexing Principles
   Indexing principles
     Single-column index
     Multi-column index

     Table news




19/12/2009        Ha Noi - 2009   12
Optimization – Indexing Principles
                                     Single index

       Table news




                                    Multi index




19/12/2009          Ha Noi - 2009      13
Optimization – Indexing Principles
   Basic Index Principles
        Only one index can be used, per table, per query
        Index definitions must match your real-world use
        You don’t have to do anything special to maintain indexes
         after creation
   Creating/Dropping Indexes
        CREATE INDEX cate ON news (cate_id)
        DROP INDEX cate ON news;
        ALTER TABLE news ADD INDEX cate
         (cate_id);
        ALTER TABLE news DROP INDEX cate;

19/12/2009              Ha Noi - 2009                14
Optimization – Indexing Principles
   CREATE INDEX cate_date ON news (cate_id,
    createdate)
   Explain SELECT * FROM news WHERE
    cate_id =1 ORDER BY createdate DESC
    LIMIT 0,10
   Run time: 0.0007 s



19/12/2009      Ha Noi - 2009     15
Optimization – Indexing Principles

    WHERE cate_id =1       Order By createdate   Limit 0,10




             2.000.000         2.000.000
                                                      10




19/12/2009               Ha Noi - 2009           16
Optimization – Indexing Principles
   Explain SELECT * FROM news WHERE
    cate_id =1 or cate_id=2 ORDER BY createdate
    DESC LIMIT 0,10
   Run time: 1.0067S




19/12/2009       Ha Noi - 2009      17
Optimization – Indexing Principles



        Cate_id=1     Order by       Limit 0,10

         1.000.000

        Cate_id=2    3.000.000          10

        2.000.000




19/12/2009           Ha Noi - 2009       18
Optimization – Indexing Principles
  SELECT news.* FROM (
   (SELECT id FROM news WHERE cate_id=1
   ORDER BY createdate LIMIT 0,10)
   UNION
   (SELECT id FROM news WHERE cate_id=2
   ORDER BY createdate LIMIT 0,10)
) as temp, news where news.id=temp.id ORDER
   BY createdate DESC LIMIT 0,10;
19/12/2009    Ha Noi - 2009      19
Optimization – Indexing Principles
   Run time: 0.00928s




19/12/2009       Ha Noi - 2009   20
Optimization – Indexing Principles



      Cate_id=1             Order by   Limit

             10

                                  20    10
      Cate_id=2

             10




19/12/2009        Ha Noi - 2009        21
Optimization – Indexing Principles
   MyISAM indexes




19/12/2009     Ha Noi - 2009   22
Optimization – Indexing Principles
   MyISAM indexes




19/12/2009     Ha Noi - 2009   23
Optimization – Indexing Principles
   MyISAM indexes




19/12/2009     Ha Noi - 2009   24
Optimization – Indexing Principles
   InnoDB indexes




19/12/2009      Ha Noi - 2009   25
Optimization – Indexing Principles
   MyISAM vs InnoDB




19/12/2009     Ha Noi - 2009   26
Optimization – Considerations
   When can indexes be used?
       Simple PRIMARY KEY lookups
            PRIMARY KEY(i) :: WHERE i = 5
       Secondary KEY (index) lookups
            INDEX(i) :: WHERE i = 7
       Prefix lookups
          INDEX(s) :: WHERE s LIKE “foo%”
          INDEX(i, j) :: WHERE i = 5

       Joins – index either table

19/12/2009            Ha Noi - 2009     27
Optimization – Considerations
   When can’t indexes be used?
       LIKE that starts with a wildcard
            INDEX(s) :: WHERE s LIKE “%foo”
       Bitwise operations
            INDEX(i) :: WHERE i & 4
       Non-prefix lookups
            INDEX(i, j) :: WHERE j = 5
       Using a function containing column ref
            INDEX(s) :: WHERE foo(s) = “FOO”
            INDEX(dt) :: WHERE unix_timestamp(dt) = 1


19/12/2009              Ha Noi - 2009            28
Optimization – Explain
   Explain SELECT * FROM news WHERE
    cate_id = 218103808 ORDER BY id DESC
    LIMIT 0,10;




19/12/2009        Ha Noi - 2009   29
Optimization – Explain
   Fields in EXPLAIN
       id – (4.1+) The ID number of this SELECT
       select_type – (4.1+) The type of this SELECT
       table – The name of the table in question
       type – The type of reading done on this table (system,
        const, eq_ref, ref, ref_or_null, index_merge,
        unique_subquery, index_subquery, range,
        index, ALL)
       possible_keys – The keys which were examined for
        possible use
       key – The key that was finally chosen for use


19/12/2009            Ha Noi - 2009            30
Optimization – Explain
   Fields in EXPLAIN
     key_len – The length of the part of the key that
      was finally chosen for use
     ref – Row reference; What is used to look up
      row values from the index
     rows – The approximate number of rows which
      must be examined from this table using the chosen
      optimizations
     Extra – Any extra comments the server wanted
      to add about this optimization path (free form)
19/12/2009        Ha Noi - 2009          31
General Discussion
                   Got any questions for me?




   Ha noi php day 2009
   vanchinh@gmail.com



19/12/2009                 Ha Noi - 2009       32

Contenu connexe

Similaire à mysql optimization

How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...
How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...
How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...Gustav Lundström
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 
Track 2 session 4 db2 for z os optimizer- what’s new in db2 11 and exploiti...
Track 2 session 4   db2 for z os optimizer- what’s new in db2 11 and exploiti...Track 2 session 4   db2 for z os optimizer- what’s new in db2 11 and exploiti...
Track 2 session 4 db2 for z os optimizer- what’s new in db2 11 and exploiti...IBMSystemzEvents
 
AD303 - Extreme Makeover: IBM Lotus Domino Application Edition
AD303 - Extreme Makeover: IBM Lotus Domino Application EditionAD303 - Extreme Makeover: IBM Lotus Domino Application Edition
AD303 - Extreme Makeover: IBM Lotus Domino Application EditionRay Bilyk
 
New Features in .Net Framework 4.0 By Nyros Developer
New Features in .Net Framework 4.0 By Nyros DeveloperNew Features in .Net Framework 4.0 By Nyros Developer
New Features in .Net Framework 4.0 By Nyros DeveloperNyros Technologies
 
dominocamp2022.t1s1.dde.pptx
dominocamp2022.t1s1.dde.pptxdominocamp2022.t1s1.dde.pptx
dominocamp2022.t1s1.dde.pptxUlrich Krause
 
Hadoop World - Oct 2009
Hadoop World - Oct 2009Hadoop World - Oct 2009
Hadoop World - Oct 2009Derek Gottfrid
 
Hw09 Counting And Clustering And Other Data Tricks
Hw09   Counting And Clustering And Other Data TricksHw09   Counting And Clustering And Other Data Tricks
Hw09 Counting And Clustering And Other Data TricksCloudera, Inc.
 
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-Features
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-FeaturesAdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-Features
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-FeaturesChristoph Adler
 
IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL Torsten Steinbach
 
MySQL for business developer - Titouan BENOIT
MySQL for business developer - Titouan BENOITMySQL for business developer - Titouan BENOIT
MySQL for business developer - Titouan BENOITTitouan BENOIT
 
Bi answer to bi publisher
Bi answer to bi publisherBi answer to bi publisher
Bi answer to bi publisherAmit Sharma
 
Er ptips sap-training-manual-sample-chapter-from-materials-management
Er ptips sap-training-manual-sample-chapter-from-materials-managementEr ptips sap-training-manual-sample-chapter-from-materials-management
Er ptips sap-training-manual-sample-chapter-from-materials-managementIT
 
An open-source TIMES/MIRO app
An open-source TIMES/MIRO appAn open-source TIMES/MIRO app
An open-source TIMES/MIRO appIEA-ETSAP
 
Ingesting Data at Blazing Speed Using Apache Orc
Ingesting Data at Blazing Speed Using Apache OrcIngesting Data at Blazing Speed Using Apache Orc
Ingesting Data at Blazing Speed Using Apache OrcDataWorks Summit
 
Whats New in Postgres 12
Whats New in Postgres 12Whats New in Postgres 12
Whats New in Postgres 12EDB
 
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...Christoph Adler
 

Similaire à mysql optimization (20)

How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...
How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...
How to combine Db2 on Z, IBM Db2 Analytics Accelerator and IBM Machine Learni...
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
Track 2 session 4 db2 for z os optimizer- what’s new in db2 11 and exploiti...
Track 2 session 4   db2 for z os optimizer- what’s new in db2 11 and exploiti...Track 2 session 4   db2 for z os optimizer- what’s new in db2 11 and exploiti...
Track 2 session 4 db2 for z os optimizer- what’s new in db2 11 and exploiti...
 
AD303 - Extreme Makeover: IBM Lotus Domino Application Edition
AD303 - Extreme Makeover: IBM Lotus Domino Application EditionAD303 - Extreme Makeover: IBM Lotus Domino Application Edition
AD303 - Extreme Makeover: IBM Lotus Domino Application Edition
 
New Features in .Net Framework 4.0 By Nyros Developer
New Features in .Net Framework 4.0 By Nyros DeveloperNew Features in .Net Framework 4.0 By Nyros Developer
New Features in .Net Framework 4.0 By Nyros Developer
 
dominocamp2022.t1s1.dde.pptx
dominocamp2022.t1s1.dde.pptxdominocamp2022.t1s1.dde.pptx
dominocamp2022.t1s1.dde.pptx
 
Hadoop World - Oct 2009
Hadoop World - Oct 2009Hadoop World - Oct 2009
Hadoop World - Oct 2009
 
Hw09 Counting And Clustering And Other Data Tricks
Hw09   Counting And Clustering And Other Data TricksHw09   Counting And Clustering And Other Data Tricks
Hw09 Counting And Clustering And Other Data Tricks
 
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-Features
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-FeaturesAdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-Features
AdminCamp2019 - We love Domino V10 - 15 neue Domino-Admin-Features
 
Open micictdi
Open micictdiOpen micictdi
Open micictdi
 
IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL
 
MySQL for business developer - Titouan BENOIT
MySQL for business developer - Titouan BENOITMySQL for business developer - Titouan BENOIT
MySQL for business developer - Titouan BENOIT
 
ExtJS Overview
ExtJS OverviewExtJS Overview
ExtJS Overview
 
Bi answer to bi publisher
Bi answer to bi publisherBi answer to bi publisher
Bi answer to bi publisher
 
Er ptips sap-training-manual-sample-chapter-from-materials-management
Er ptips sap-training-manual-sample-chapter-from-materials-managementEr ptips sap-training-manual-sample-chapter-from-materials-management
Er ptips sap-training-manual-sample-chapter-from-materials-management
 
An open-source TIMES/MIRO app
An open-source TIMES/MIRO appAn open-source TIMES/MIRO app
An open-source TIMES/MIRO app
 
Ingesting Data at Blazing Speed Using Apache Orc
Ingesting Data at Blazing Speed Using Apache OrcIngesting Data at Blazing Speed Using Apache Orc
Ingesting Data at Blazing Speed Using Apache Orc
 
Whats New in Postgres 12
Whats New in Postgres 12Whats New in Postgres 12
Whats New in Postgres 12
 
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...
15 New Domino Admin Features Sure to Spark a Lasting Love Affair with Domino ...
 

Plus de hazzaz

Coffee1
Coffee1Coffee1
Coffee1hazzaz
 
Suy ngam
Suy ngamSuy ngam
Suy ngamhazzaz
 
Tu dong dat hang tu he thong ban le lon nhat trung quoc
Tu dong dat hang tu he thong ban le lon nhat trung quocTu dong dat hang tu he thong ban le lon nhat trung quoc
Tu dong dat hang tu he thong ban le lon nhat trung quochazzaz
 
how startups can benefit from launch community
how startups can benefit from launch communityhow startups can benefit from launch community
how startups can benefit from launch communityhazzaz
 
social network game
social network gamesocial network game
social network gamehazzaz
 
su dung drupal xay dung mang xa hoi
su dung drupal xay dung mang xa hoisu dung drupal xay dung mang xa hoi
su dung drupal xay dung mang xa hoihazzaz
 
html5 css3 the future of web technology
html5 css3 the future of web technologyhtml5 css3 the future of web technology
html5 css3 the future of web technologyhazzaz
 
java script unit testing framework
java script unit testing frameworkjava script unit testing framework
java script unit testing frameworkhazzaz
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extensionhazzaz
 
web optimization
web optimizationweb optimization
web optimizationhazzaz
 
speed up ntvv2 by php ext module
speed up ntvv2 by php ext modulespeed up ntvv2 by php ext module
speed up ntvv2 by php ext modulehazzaz
 
zingmepracticeforbuildingscalablewebsitewithphp
zingmepracticeforbuildingscalablewebsitewithphpzingmepracticeforbuildingscalablewebsitewithphp
zingmepracticeforbuildingscalablewebsitewithphphazzaz
 
EAV in Magento
EAV in MagentoEAV in Magento
EAV in Magentohazzaz
 
css_trends
css_trendscss_trends
css_trendshazzaz
 
Phan mem tu do nguon mo
Phan mem tu do nguon moPhan mem tu do nguon mo
Phan mem tu do nguon mohazzaz
 
Howtobuildyourownframework
HowtobuildyourownframeworkHowtobuildyourownframework
Howtobuildyourownframeworkhazzaz
 

Plus de hazzaz (20)

Coffee1
Coffee1Coffee1
Coffee1
 
Suy ngam
Suy ngamSuy ngam
Suy ngam
 
Tu dong dat hang tu he thong ban le lon nhat trung quoc
Tu dong dat hang tu he thong ban le lon nhat trung quocTu dong dat hang tu he thong ban le lon nhat trung quoc
Tu dong dat hang tu he thong ban le lon nhat trung quoc
 
how startups can benefit from launch community
how startups can benefit from launch communityhow startups can benefit from launch community
how startups can benefit from launch community
 
social network game
social network gamesocial network game
social network game
 
su dung drupal xay dung mang xa hoi
su dung drupal xay dung mang xa hoisu dung drupal xay dung mang xa hoi
su dung drupal xay dung mang xa hoi
 
html5 css3 the future of web technology
html5 css3 the future of web technologyhtml5 css3 the future of web technology
html5 css3 the future of web technology
 
java script unit testing framework
java script unit testing frameworkjava script unit testing framework
java script unit testing framework
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extension
 
web optimization
web optimizationweb optimization
web optimization
 
speed up ntvv2 by php ext module
speed up ntvv2 by php ext modulespeed up ntvv2 by php ext module
speed up ntvv2 by php ext module
 
zingmepracticeforbuildingscalablewebsitewithphp
zingmepracticeforbuildingscalablewebsitewithphpzingmepracticeforbuildingscalablewebsitewithphp
zingmepracticeforbuildingscalablewebsitewithphp
 
EAV in Magento
EAV in MagentoEAV in Magento
EAV in Magento
 
Albus
AlbusAlbus
Albus
 
css_trends
css_trendscss_trends
css_trends
 
Cloud
CloudCloud
Cloud
 
Phan mem tu do nguon mo
Phan mem tu do nguon moPhan mem tu do nguon mo
Phan mem tu do nguon mo
 
Zing
ZingZing
Zing
 
redis
redisredis
redis
 
Howtobuildyourownframework
HowtobuildyourownframeworkHowtobuildyourownframework
Howtobuildyourownframework
 

Dernier

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Dernier (20)

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

mysql optimization

  • 1. MySQL Optimization Ha noi php day 2009 vanchinh@gmail.com 19/12/2009 Ha Noi - 2009 1
  • 2. Content  Introduction  Example  Indexing Principles  Considerations  Explain  General discussion 19/12/2009 Ha Noi - 2009 2
  • 3. Introduction Ha noi php day 2009 vanchinh@gmail.com 19/12/2009 Ha Noi - 2009 3
  • 4. Who am i  I working in NaisCorp (socbay.com)  I’ve been using MySQL personally for 2 years and professionally for over 1 years.  Contact info:  Personal: vanchinh@gmail.com  work: chinhnv@socbay.com  Web: http://ffx.socbay.com 19/12/2009 Ha Noi - 2009 4
  • 5. Example Ha noi php day 2009 vanchinh@gmail.com 19/12/2009 Ha Noi - 2009 5
  • 6. Example  SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10;  SELECT * FROM news WHERE cate_id =1 or cate_id=2 ORDER BY createdate DESC LIMIT 0,10;  SELECT * FROM news WHERE id < 12345678 ORDER BY createdate DESC LIMIT 0, 10 19/12/2009 Ha Noi - 2009 6
  • 7. Example  Query Parsing and Optimizing Query Parse Thread 1 Thread 2 Thread 3 Optimize Thread 4 MyISAM Key Buffer Engine API MyISAM … Thread 5 Storage Execute (ha_myisam) (fetch) *.MYD, *.MYI Thread n (update) InnoDB Key Buffer InnoDB (ha_innodb) ibdataN Results Group By Having Order By Group By and Order By can be done with indexes instead. 19/12/2009 Ha Noi - 2009 7
  • 8. Example  Query Parsing and Optimizing  All parsing and optimizing steps are the same for all storage engines  All ORDER BY / GROUP BY steps are the same  The only difference for a given storage engine is in actually fetching the rows  Some types of indexes don’t exist on some table types: e.g. FULLTEXT and SPATIAL indexes don’t exist for InnoDB 19/12/2009 Ha Noi - 2009 8
  • 9. Example  Explain SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10;  Run time: 0.9724s 19/12/2009 Ha Noi - 2009 9
  • 10. Example WHERE cate_id =1 Order By createdate Limit 0,10 2.000.000 2.000.000 10 19/12/2009 Ha Noi - 2009 10
  • 11. Indexing Principles Ha noi php day 2009 vanchinh@gmail.com 19/12/2009 Ha Noi - 2009 11
  • 12. Optimization – Indexing Principles  Indexing principles  Single-column index  Multi-column index  Table news 19/12/2009 Ha Noi - 2009 12
  • 13. Optimization – Indexing Principles Single index Table news Multi index 19/12/2009 Ha Noi - 2009 13
  • 14. Optimization – Indexing Principles  Basic Index Principles  Only one index can be used, per table, per query  Index definitions must match your real-world use  You don’t have to do anything special to maintain indexes after creation  Creating/Dropping Indexes  CREATE INDEX cate ON news (cate_id)  DROP INDEX cate ON news;  ALTER TABLE news ADD INDEX cate (cate_id);  ALTER TABLE news DROP INDEX cate; 19/12/2009 Ha Noi - 2009 14
  • 15. Optimization – Indexing Principles  CREATE INDEX cate_date ON news (cate_id, createdate)  Explain SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10  Run time: 0.0007 s 19/12/2009 Ha Noi - 2009 15
  • 16. Optimization – Indexing Principles WHERE cate_id =1 Order By createdate Limit 0,10 2.000.000 2.000.000 10 19/12/2009 Ha Noi - 2009 16
  • 17. Optimization – Indexing Principles  Explain SELECT * FROM news WHERE cate_id =1 or cate_id=2 ORDER BY createdate DESC LIMIT 0,10  Run time: 1.0067S 19/12/2009 Ha Noi - 2009 17
  • 18. Optimization – Indexing Principles Cate_id=1 Order by Limit 0,10 1.000.000 Cate_id=2 3.000.000 10 2.000.000 19/12/2009 Ha Noi - 2009 18
  • 19. Optimization – Indexing Principles  SELECT news.* FROM ( (SELECT id FROM news WHERE cate_id=1 ORDER BY createdate LIMIT 0,10) UNION (SELECT id FROM news WHERE cate_id=2 ORDER BY createdate LIMIT 0,10) ) as temp, news where news.id=temp.id ORDER BY createdate DESC LIMIT 0,10; 19/12/2009 Ha Noi - 2009 19
  • 20. Optimization – Indexing Principles  Run time: 0.00928s 19/12/2009 Ha Noi - 2009 20
  • 21. Optimization – Indexing Principles  Cate_id=1 Order by Limit 10 20 10 Cate_id=2 10 19/12/2009 Ha Noi - 2009 21
  • 22. Optimization – Indexing Principles  MyISAM indexes 19/12/2009 Ha Noi - 2009 22
  • 23. Optimization – Indexing Principles  MyISAM indexes 19/12/2009 Ha Noi - 2009 23
  • 24. Optimization – Indexing Principles  MyISAM indexes 19/12/2009 Ha Noi - 2009 24
  • 25. Optimization – Indexing Principles  InnoDB indexes 19/12/2009 Ha Noi - 2009 25
  • 26. Optimization – Indexing Principles  MyISAM vs InnoDB 19/12/2009 Ha Noi - 2009 26
  • 27. Optimization – Considerations  When can indexes be used?  Simple PRIMARY KEY lookups  PRIMARY KEY(i) :: WHERE i = 5  Secondary KEY (index) lookups  INDEX(i) :: WHERE i = 7  Prefix lookups  INDEX(s) :: WHERE s LIKE “foo%”  INDEX(i, j) :: WHERE i = 5  Joins – index either table 19/12/2009 Ha Noi - 2009 27
  • 28. Optimization – Considerations  When can’t indexes be used?  LIKE that starts with a wildcard  INDEX(s) :: WHERE s LIKE “%foo”  Bitwise operations  INDEX(i) :: WHERE i & 4  Non-prefix lookups  INDEX(i, j) :: WHERE j = 5  Using a function containing column ref  INDEX(s) :: WHERE foo(s) = “FOO”  INDEX(dt) :: WHERE unix_timestamp(dt) = 1 19/12/2009 Ha Noi - 2009 28
  • 29. Optimization – Explain  Explain SELECT * FROM news WHERE cate_id = 218103808 ORDER BY id DESC LIMIT 0,10; 19/12/2009 Ha Noi - 2009 29
  • 30. Optimization – Explain  Fields in EXPLAIN  id – (4.1+) The ID number of this SELECT  select_type – (4.1+) The type of this SELECT  table – The name of the table in question  type – The type of reading done on this table (system, const, eq_ref, ref, ref_or_null, index_merge, unique_subquery, index_subquery, range, index, ALL)  possible_keys – The keys which were examined for possible use  key – The key that was finally chosen for use 19/12/2009 Ha Noi - 2009 30
  • 31. Optimization – Explain  Fields in EXPLAIN  key_len – The length of the part of the key that was finally chosen for use  ref – Row reference; What is used to look up row values from the index  rows – The approximate number of rows which must be examined from this table using the chosen optimizations  Extra – Any extra comments the server wanted to add about this optimization path (free form) 19/12/2009 Ha Noi - 2009 31
  • 32. General Discussion Got any questions for me? Ha noi php day 2009 vanchinh@gmail.com 19/12/2009 Ha Noi - 2009 32