Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
MySQL Performance                    SecretsBy Rakesh KumarOSSCube, Delhirakesh@osscube.com
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 Typessystem            The table has only one rowconst             At the most one matching row, treated as a     ...
EXPLAIN ExtraUsing index       The result is created straight from the indexUsing where       Not all rows are used in the...
Optimizer HintsSTRAIGHT_JOIN            Forces the optimizer to join the tables in the                         given order...
Selecting Queries to Optimize• The slow query log  – Logs all queries that take longer than    long_query_time  – Can also...
• 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 ...
MyISAM-Specific Optimizations
InnoDB-Specific Optimizations• InnoDB uses clustered indexes  – The length of the PRIMARY KEY is extremely    important• T...
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     ...
• Online changes are not persistant over a  server restart  – The configuration files have to be changed as well• The curr...
Status Variables
SQL/Parser ModelClient1      Client2   ClientN                                       MySQL Server     Connection Thread Po...
Query Cache• Stores SELECT queries and their results• Purpose: improve performance for  frequently requested data• The dat...
• The Qcache_% status variables help  monitoring the cache  – The utilisation ratio: Qcache_hits vs.    Com_select• The qu...
Some Thread Specific Options• read_buffer_size (default 128Kb) and  read_rnd_buffer_size (default 256Kb)   – Size of cache...
Some Global Options• table_cache (default 64)  – Cache for storing open table handlers  – Increase this if Opened_tables i...
• max_connections (default 100)  – The maximum allowed number of simultaneous    connections  – Very important for tuning ...
MyISAM Global Options• key_buffer_size (default 8Mb)  – Cache for storing indices  – Increase this to get better index han...
MyISAM Thread-Specific Options• myisam_sort_buffer_size (default  8Mb)  – Used when sorting indexes during    REPAIR/ALTER...
InnoDB-Specific Optimization• innodb_buffer_pool_size (default  8Mb)  – The memory buffer InnoDB uses to cache both    dat...
InnoDB-Specific                                      Optimization• innodb_log_buffer_size (default  1Mb)  – Larger values ...
QnA
Thank you for your time and attention        www.osscube.com For more information, please feel free to drop in a line to  ...
MySQL Performance Secrets
MySQL Performance Secrets
MySQL Performance Secrets
MySQL Performance Secrets
MySQL Performance Secrets
Upcoming SlideShare
Loading in …5
×

MySQL Performance Secrets

5,936 views

Published on

How to make an efficient MySQL implementation? Performing performance tuning, query optimisation, and server configuration with MySQL

Published in: Technology
  • Be the first to comment

MySQL Performance Secrets

  1. 1. MySQL Performance SecretsBy Rakesh KumarOSSCube, Delhirakesh@osscube.com
  2. 2. Why Tune a Database?
  3. 3. Who Tunes?
  4. 4. What is Tuned?
  5. 5. How much tuning is enough?
  6. 6. Application Development (Optimizing Queries)
  7. 7. Index Optimizations
  8. 8. MySQL Cluster Tutorial, OSSPAC 09 Singapore, © OSSCube
  9. 9. EXPLAIN Typessystem The table has only one rowconst At the most one matching row, treated as a constanteq_ref One row per row from previous tablesref Several rows with matching index valueref_or_null Like ref, plus NULL valuesindex_merge Several index searches are mergedunique_subquery Same as ref for some subqueriesindex_subquery As above for non-unique indexesrange A range index scanindex The whole index is scannedALL A full table scan
  10. 10. EXPLAIN ExtraUsing index The result is created straight from the indexUsing where Not all rows are used in the resultDistinct Only a single row is read per row combinationNot exists A LEFT JOIN missing rows optimization is usedUsing filesort An extra row sorting step is doneUsing temporary A temporary table is usedRange checked The read type is optimized individually for eachfor each record combination of rows from the previous tables
  11. 11. Optimizer HintsSTRAIGHT_JOIN Forces the optimizer to join the tables in the given orderSQL_BIG_RESULTS Together with GROUP BY or DISTINCT tells the server to use disk-based temp tablesSQL_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 indexFORCE INDEX Forces the optimizer to use the index (if possible)IGNORE INDEX Forces the optimizer not the use the index
  12. 12. 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
  13. 13. • The general query log can be use to analyze: – Reads vs. writes – Simple queries vs. complex queries – etc
  14. 14. Database Designing(Optimizing Schemas)
  15. 15. Normalization
  16. 16. Table Optimizations
  17. 17. 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
  18. 18. MyISAM-Specific Optimizations
  19. 19. 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
  20. 20. Monitoring Threads in MySQL
  21. 21. MEMORY-Specific Optimizations
  22. 22. Optimizing the Server
  23. 23. Performance Monitoring
  24. 24. 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
  25. 25. • 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
  26. 26. Status Variables
  27. 27. SQL/Parser ModelClient1 Client2 ClientN MySQL Server Connection Thread Pool Query Cache  InnoDB Storage Engines  MyISAM  MERGE Parser Query 101101  MEMORY  Federated  ARCHIVE Optimizer  NDBCluster
  28. 28. 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
  29. 29. • 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
  30. 30. 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
  31. 31. 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
  32. 32. • 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
  33. 33. 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
  34. 34. 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
  35. 35. 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)
  36. 36. 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
  37. 37. QnA
  38. 38. 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

×