SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Secrets of Best MySQL
   Optimization

      Presented by – Sonali Minocha
                     OSSCube
Who Am I?
Why Tune a Database?
Who Tunes?
What is Tuned?
How much tuning is enough?
Application Development
  (Optimizing Queries)
Index Optimizations
MySQL Cluster Tutorial, OSSPAC 09
    Singapore, © OSSCube
EXPLAIN Types
system            The table has only one row
const             At the most one matching row, treated as a
                  constant
eq_ref            One row per row from previous tables
ref               Several rows with matching index value
ref_or_null       Like ref, plus NULL values
index_merge       Several index searches are merged
unique_subquery   Same as ref for some subqueries
index_subquery    As above for non-unique indexes
range             A range index scan
index             The whole index is scanned
ALL               A full table scan
EXPLAIN Extra
Using index       The result is created straight from the index
Using where       Not all rows are used in the result
Distinct          Only a single row is read per row combination
Not exists        A LEFT JOIN missing rows optimization is
                  used
Using filesort    An extra row sorting step is done
Using temporary   A temporary table is used
Range checked     The read type is optimized individually for
for each record   each combination of rows from the previous
                  tables
Optimizer Hints
STRAIGHT_JOIN          Forces the optimizer to join the tables in the
                       given order
SQL_BIG_RESULTS        Together with GROUP BY or DISTINCT
                       tells the server to use disk-based temp
                       tables
SQL_BUFFER_RESULTS Tells the server to use a temp table, thus
                   releasing locks early (for table-locks)
USE INDEX              Hints to the optimizer to use the given
                       index
FORCE INDEX            Forces the optimizer to use the index (if
                       possible)
IGNORE INDEX           Forces the optimizer not the use the index
Selecting Queries to Optimize
• The slow query log
  – Logs all queries that take longer than
    long_query_time
  – Can also log all queries that don’t use indexes
    with
    --log-queries-not-using-indexes
  – To log slow administrative commands use
    --log-slow-admin-statements
  – To analyze the contents of the slow log use
    mysqldumpslow
• The general query log can be use to analyze:
  – Reads vs. writes
  – Simple queries vs. complex queries
  – etc
Database Designing
(Optimizing Schemas)
Normalization
Table Optimizations
Choosing Best Suited Storage Engine


• Understanding benefits and drawbacks of
  each storage engine is very important while
  designing application.
• Different storage engine has different index
  capability ,application need should be kept
  in mind while choosing storage engine
MyISAM-Specific Optimizations
InnoDB-Specific Optimizations

• InnoDB uses clustered indexes
  – The length of the PRIMARY KEY is extremely
    important
• The rows are always dynamic
  – Using VARCHAR instead of CHAR is almost always
    better
• Maintenance operations needed after
  – Many UPDATE/DELETE operations
     • The pages can become underfilled
Monitoring Threads in MySQL
MEMORY-Specific Optimizations
Optimizing the Server
Performance Monitoring
Tuning MySQL Parameters
• Some MySQL options can be changed online
• The dynamic options are either
  – SESSION specific
     • Changing the value will only affect the current
       connection
  – GLOBAL
     • Changing the value will affect the whole server
  – Both
     • When changing the value SESSION/GLOBAL
       should be specified
• Online changes are not persistant over a
  server restart
  – The configuration files have to be changed as well
• The current values of all options can be found
  with
  SHOW SESSION/GLOBAL VARIABLES
Status Variables
SQL/Parser Model

Client1     Client2   ClientN



                                    MySQL Server
   Connection Thread Pool


   Query Cache                                     InnoDB
                                Storage Engines    MyISAM
                                                   MERGE
   Parser   Query     101101                       MEMORY
                                                   Federated
                                                   ARCHIVE
   Optimizer                                       NDBCluster
Query Cache
• Stores SELECT queries and their results
• Purpose: improve performance for
  frequently requested data
• The data in the query cache is invalidated as
  soon as a modification is done in the table
• Controlled with the query_cache_size
  variable
• The Qcache_% status variables help
  monitoring the cache
  – The utilisation ratio: Qcache_hits vs.
    Com_select
• The query cache can be emptied with
  RESET QUERY CACHE
Some Thread Specific Options

• read_buffer_size (default 128Kb) and
  read_rnd_buffer_size (default 256Kb)
   – Size of cache used for table scanning
   – Not equivalent to block size
       • The database is not divided into blocks but directly into
         records
   – Increase if you do many sequential scans
• sort_buffer_size (default 2Mb)
   – Size of the GROUP BY / ORDER BY cache
   – If more memory is needed it will be taken from the disk
• tmp_table_size (default 32Mb)
   – Limit after which temporary tables will not be MEMORYs
     anymore, but MyISAM tables
Some Global Options
• table_cache (default 64)
  – Cache for storing open table handlers
  – Increase this if Opened_tables is high
• thread_cache (default 0)
  – Number of threads to keep for reuse
  – Increase if threads_created is high
  – Not useful if the client uses connection pooling
• max_connections (default 100)
  – The maximum allowed number of simultaneous
    connections
  – Very important for tuning thread specific memory
    areas
  – Each connection uses at least thread_stack of
    memory
MyISAM Global Options
• key_buffer_size (default 8Mb)
  – Cache for storing indices
  – Increase this to get better index handling
  – Miss ratio
    (key_reads/key_read_requests) should
    be very low, at least < 0.03 (often < 0.01 is
    desirable)
• Row caching is handled by the OS
MyISAM Thread-Specific Options

• myisam_sort_buffer_size (default
  8Mb)
  – Used when sorting indexes during
    REPAIR/ALTER TABLE
• myisam_repair_threads (default 1)
  – Used for bulk import and repairing
  – Allows for repairing indexes in multiple threads
• myisam_max_sort_file_size
  – The max size of the file used while re-creating
    indexes
InnoDB-Specific Optimization
• innodb_buffer_pool_size (default
  8Mb)
  – The memory buffer InnoDB uses to cache both
    data and indexes
  – The bigger you set this the less disk i/o is
    needed
  – Can be set very high (up to 80% on a dedicated
    system)
• innodb_flush_log_at_trx_commit
  (default 1)
 – 0 writes and sync’s once per second (not ACID)
 – 1 forces sync to disk after every commit
 – 2 write to disk every commit but only sync’s about
   once per second
InnoDB-Specific
                                      Optimization
• innodb_log_buffer_size (default
  1Mb)
  – Larger values allows for larger transactions to be
    logged in memory
  – Sensible values range from 1M to 8M
• innodb_log_file_size (default 5Mb)
  – Size of each InnoDB redo log file
  – Can be set up to buffer_pool_size
Thank you for your time and attention

           www.osscube.com



For more information, please feel free to drop in a line to
 sonali@osscube.com or visit http://www.osscube.com
QnA

Weitere ähnliche Inhalte

Mehr von OSSCube

High Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationHigh Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationOSSCube
 
Accelerate Your Digital Transformation Journey with Pimcore
Accelerate Your Digital Transformation Journey with PimcoreAccelerate Your Digital Transformation Journey with Pimcore
Accelerate Your Digital Transformation Journey with PimcoreOSSCube
 
Migrating Legacy Applications to AWS Cloud: Strategies and Challenges
Migrating Legacy Applications to AWS Cloud: Strategies and ChallengesMigrating Legacy Applications to AWS Cloud: Strategies and Challenges
Migrating Legacy Applications to AWS Cloud: Strategies and ChallengesOSSCube
 
Why Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your CustomersWhy Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your CustomersOSSCube
 
Using MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling OutUsing MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling OutOSSCube
 
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...OSSCube
 
Cutting Through the Disruption
Cutting Through the DisruptionCutting Through the Disruption
Cutting Through the DisruptionOSSCube
 
Legacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case studyLegacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case studyOSSCube
 
Marketing and Sales together at last
Marketing and Sales together at lastMarketing and Sales together at last
Marketing and Sales together at lastOSSCube
 
Using pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfactionUsing pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfactionOSSCube
 
Talend for the Enterprise
Talend for the EnterpriseTalend for the Enterprise
Talend for the EnterpriseOSSCube
 
Ahead of the Curve
Ahead of the CurveAhead of the Curve
Ahead of the CurveOSSCube
 
Non functional requirements. do we really care…?
Non functional requirements. do we really care…?Non functional requirements. do we really care…?
Non functional requirements. do we really care…?OSSCube
 
Learning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMILearning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMIOSSCube
 
Exploiting JXL using Selenium
Exploiting JXL using SeleniumExploiting JXL using Selenium
Exploiting JXL using SeleniumOSSCube
 
Introduction to AWS
Introduction to AWSIntroduction to AWS
Introduction to AWSOSSCube
 
Maria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High AvailabilityMaria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High AvailabilityOSSCube
 
Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014OSSCube
 
Performance Testing Session - OSSCamp 2014
Performance Testing Session -  OSSCamp 2014Performance Testing Session -  OSSCamp 2014
Performance Testing Session - OSSCamp 2014OSSCube
 
Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014OSSCube
 

Mehr von OSSCube (20)

High Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationHigh Availability Using MySQL Group Replication
High Availability Using MySQL Group Replication
 
Accelerate Your Digital Transformation Journey with Pimcore
Accelerate Your Digital Transformation Journey with PimcoreAccelerate Your Digital Transformation Journey with Pimcore
Accelerate Your Digital Transformation Journey with Pimcore
 
Migrating Legacy Applications to AWS Cloud: Strategies and Challenges
Migrating Legacy Applications to AWS Cloud: Strategies and ChallengesMigrating Legacy Applications to AWS Cloud: Strategies and Challenges
Migrating Legacy Applications to AWS Cloud: Strategies and Challenges
 
Why Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your CustomersWhy Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your Customers
 
Using MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling OutUsing MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling Out
 
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
 
Cutting Through the Disruption
Cutting Through the DisruptionCutting Through the Disruption
Cutting Through the Disruption
 
Legacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case studyLegacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case study
 
Marketing and Sales together at last
Marketing and Sales together at lastMarketing and Sales together at last
Marketing and Sales together at last
 
Using pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfactionUsing pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfaction
 
Talend for the Enterprise
Talend for the EnterpriseTalend for the Enterprise
Talend for the Enterprise
 
Ahead of the Curve
Ahead of the CurveAhead of the Curve
Ahead of the Curve
 
Non functional requirements. do we really care…?
Non functional requirements. do we really care…?Non functional requirements. do we really care…?
Non functional requirements. do we really care…?
 
Learning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMILearning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMI
 
Exploiting JXL using Selenium
Exploiting JXL using SeleniumExploiting JXL using Selenium
Exploiting JXL using Selenium
 
Introduction to AWS
Introduction to AWSIntroduction to AWS
Introduction to AWS
 
Maria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High AvailabilityMaria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High Availability
 
Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014
 
Performance Testing Session - OSSCamp 2014
Performance Testing Session -  OSSCamp 2014Performance Testing Session -  OSSCamp 2014
Performance Testing Session - OSSCamp 2014
 
Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014
 

Kürzlich hochgeladen

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 

Secrets Of MySQL Optimization & Performance Tuning At OSSPAC 2009

  • 1. Secrets of Best MySQL Optimization Presented by – Sonali Minocha OSSCube
  • 3. Why Tune a Database?
  • 6. How much tuning is enough?
  • 7.
  • 8.
  • 9. Application Development (Optimizing Queries)
  • 10.
  • 12.
  • 13.
  • 14. MySQL Cluster Tutorial, OSSPAC 09 Singapore, © OSSCube
  • 15. EXPLAIN Types system The table has only one row const At the most one matching row, treated as a constant eq_ref One row per row from previous tables ref Several rows with matching index value ref_or_null Like ref, plus NULL values index_merge Several index searches are merged unique_subquery Same as ref for some subqueries index_subquery As above for non-unique indexes range A range index scan index The whole index is scanned ALL A full table scan
  • 16. EXPLAIN Extra Using index The result is created straight from the index Using where Not all rows are used in the result Distinct Only a single row is read per row combination Not exists A LEFT JOIN missing rows optimization is used Using filesort An extra row sorting step is done Using temporary A temporary table is used Range checked The read type is optimized individually for for each record each combination of rows from the previous tables
  • 17. Optimizer Hints STRAIGHT_JOIN Forces the optimizer to join the tables in the given order SQL_BIG_RESULTS Together with GROUP BY or DISTINCT tells the server to use disk-based temp tables SQL_BUFFER_RESULTS Tells the server to use a temp table, thus releasing locks early (for table-locks) USE INDEX Hints to the optimizer to use the given index FORCE INDEX Forces the optimizer to use the index (if possible) IGNORE INDEX Forces the optimizer not the use the index
  • 18. Selecting Queries to Optimize • The slow query log – Logs all queries that take longer than long_query_time – Can also log all queries that don’t use indexes with --log-queries-not-using-indexes – To log slow administrative commands use --log-slow-admin-statements – To analyze the contents of the slow log use mysqldumpslow
  • 19. • The general query log can be use to analyze: – Reads vs. writes – Simple queries vs. complex queries – etc
  • 23. Choosing Best Suited Storage Engine • Understanding benefits and drawbacks of each storage engine is very important while designing application. • Different storage engine has different index capability ,application need should be kept in mind while choosing storage engine
  • 25. InnoDB-Specific Optimizations • InnoDB uses clustered indexes – The length of the PRIMARY KEY is extremely important • The rows are always dynamic – Using VARCHAR instead of CHAR is almost always better • Maintenance operations needed after – Many UPDATE/DELETE operations • The pages can become underfilled
  • 30. Tuning MySQL Parameters • Some MySQL options can be changed online • The dynamic options are either – SESSION specific • Changing the value will only affect the current connection – GLOBAL • Changing the value will affect the whole server – Both • When changing the value SESSION/GLOBAL should be specified
  • 31. • Online changes are not persistant over a server restart – The configuration files have to be changed as well • The current values of all options can be found with SHOW SESSION/GLOBAL VARIABLES
  • 33. SQL/Parser Model Client1 Client2 ClientN MySQL Server Connection Thread Pool Query Cache  InnoDB Storage Engines  MyISAM  MERGE Parser Query 101101  MEMORY  Federated  ARCHIVE Optimizer  NDBCluster
  • 34. Query Cache • Stores SELECT queries and their results • Purpose: improve performance for frequently requested data • The data in the query cache is invalidated as soon as a modification is done in the table • Controlled with the query_cache_size variable
  • 35. • The Qcache_% status variables help monitoring the cache – The utilisation ratio: Qcache_hits vs. Com_select • The query cache can be emptied with RESET QUERY CACHE
  • 36. Some Thread Specific Options • read_buffer_size (default 128Kb) and read_rnd_buffer_size (default 256Kb) – Size of cache used for table scanning – Not equivalent to block size • The database is not divided into blocks but directly into records – Increase if you do many sequential scans • sort_buffer_size (default 2Mb) – Size of the GROUP BY / ORDER BY cache – If more memory is needed it will be taken from the disk • tmp_table_size (default 32Mb) – Limit after which temporary tables will not be MEMORYs anymore, but MyISAM tables
  • 37. Some Global Options • table_cache (default 64) – Cache for storing open table handlers – Increase this if Opened_tables is high • thread_cache (default 0) – Number of threads to keep for reuse – Increase if threads_created is high – Not useful if the client uses connection pooling
  • 38. • max_connections (default 100) – The maximum allowed number of simultaneous connections – Very important for tuning thread specific memory areas – Each connection uses at least thread_stack of memory
  • 39. MyISAM Global Options • key_buffer_size (default 8Mb) – Cache for storing indices – Increase this to get better index handling – Miss ratio (key_reads/key_read_requests) should be very low, at least < 0.03 (often < 0.01 is desirable) • Row caching is handled by the OS
  • 40. MyISAM Thread-Specific Options • myisam_sort_buffer_size (default 8Mb) – Used when sorting indexes during REPAIR/ALTER TABLE • myisam_repair_threads (default 1) – Used for bulk import and repairing – Allows for repairing indexes in multiple threads • myisam_max_sort_file_size – The max size of the file used while re-creating indexes
  • 41. InnoDB-Specific Optimization • innodb_buffer_pool_size (default 8Mb) – The memory buffer InnoDB uses to cache both data and indexes – The bigger you set this the less disk i/o is needed – Can be set very high (up to 80% on a dedicated system)
  • 42. • innodb_flush_log_at_trx_commit (default 1) – 0 writes and sync’s once per second (not ACID) – 1 forces sync to disk after every commit – 2 write to disk every commit but only sync’s about once per second
  • 43. InnoDB-Specific Optimization • innodb_log_buffer_size (default 1Mb) – Larger values allows for larger transactions to be logged in memory – Sensible values range from 1M to 8M • innodb_log_file_size (default 5Mb) – Size of each InnoDB redo log file – Can be set up to buffer_pool_size
  • 44. Thank you for your time and attention www.osscube.com For more information, please feel free to drop in a line to sonali@osscube.com or visit http://www.osscube.com
  • 45. QnA

Hinweis der Redaktion

  1. NEW/UPDATED
  2. UPDATED changed sort_buffer to sort_buffer_size (typo? or change to variable?) added defaults
  3. UPDATED moved defaults to title line
  4. UPDATED moved defaults to title line