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.

!(How to Kill MySQL Performance)

4,299 views

Published on

Published in: Technology
  • Be the first to comment

!(How to Kill MySQL Performance)

  1. 1. !(How to Kill MySQL Performance) Vishnu Kumar Agarwal [email_address]
  2. 2. Before we start … <ul><li>What we are going to do? </li></ul><ul><ul><li>Going to look into basic issues first. </li></ul></ul><ul><ul><li>Ask small questions in between, save longer ones for later. </li></ul></ul><ul><li>Slide Format </li></ul><ul><ul><li>“ Things not to do” followed by “suggestions” </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  3. 3. #1 : Thinking Small <ul><li>Don't concentrated over one thing, think about ecosystem in which MySQL runs </li></ul><ul><ul><li>Different servers like web & application servers </li></ul></ul><ul><ul><li>Proxies and caching at different level </li></ul></ul><ul><ul><li>Plan for replication / partitioning earlier </li></ul></ul><ul><li>Don't think too big. </li></ul><ul><ul><li>Remember “ Performance != Scalability ” </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  4. 4. #2 : Not using EXPLAIN for queries <ul><li>Most of the performance issues can be solved by optimizing the query and database schema </li></ul><ul><li>Use EXPLAIN to check query performance </li></ul><ul><ul><li>It returns execution plan chosen by optimizer </li></ul></ul><ul><ul><li>E.g. &quot;index&quot; in type column is bad, but in extra column is good. </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  5. 5. #3 : Choosing arbitrary data types <ul><li>Concept : Smaller the data / index, faster the query execution </li></ul><ul><li>E.g. Storing IP Addresses as INT </li></ul><ul><ul><li>Functions INET_ATON() & INET_NTOA() </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  6. 6. #4 : Using Persistence Connections <ul><li>Connections to MySQL are way faster than Oracle & PostgreSQL </li></ul><ul><li>This doesn't comply with resource sharing architecture </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  7. 7. #5 : Using a heavy DB Extraction Layer <ul><li>If portability is not the issue, don't use heavy abstraction layer </li></ul><ul><ul><li>E.g. ADOdb, MDB2 etc </li></ul></ul><ul><li>Use a lightweight layer </li></ul><ul><ul><li>E.g. PDO </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  8. 8. #6 : Not getting right Storage Engine <ul><li>Don't use default Storage engine without understanding the requirements </li></ul><ul><li>Single-engine architectures are not optimal generally </li></ul><ul><li>E.g. ARCHIVE for faster Data dumping </li></ul><ul><ul><li>Compressed data size 6-8x smaller than MyISAM </li></ul></ul><ul><li>E.g. MEMORY for faster read from the transient Data </li></ul><ul><ul><li>Good for summary dashboards, or sessions </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  9. 9. #7 : Choosing wrong Index Layouts <ul><li>Clustered Vs Non-Clustered Layout </li></ul><ul><ul><li>Engine implement how they &quot;lay out&quot; both data and index records in memory and on Disk </li></ul></ul><ul><li>Clustered Layout stores data on disk in the order of primary key </li></ul><ul><li>Non-clustered Layout organization has no implicit order to Data. </li></ul><ul><li>Clustered Layout append primary key to each index record </li></ul><ul><ul><li>Choose small primary key. </li></ul></ul><ul><ul><li>Choose primary key by yourself </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  10. 10. #8 : Creating many Stored Procedures <ul><li>Compiled stored procedure execution plans kept on the connection thread. </li></ul><ul><li>In case stored procedures are executed rarely, it is a overhead on the MySQL connection </li></ul><ul><li>Rather, use prepare statements or UDFs. </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  11. 11. #9 : Not using AUTO_INCREMENT <ul><li>It enables high performance concurrent inserts </li></ul><ul><ul><li>Lockless reading and appending </li></ul></ul><ul><li>Reduces disk and page fragmentation by keeping new records together </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  12. 12. #10 : Not using ON DUPLICATE KEY UPDATE <ul><li>Cleans up code </li></ul><ul><ul><li>e.g. if(recort_exists()) … </li></ul></ul><ul><li>Avoids a roundtrip from connection to server </li></ul><ul><li>Faster than 2 statements (select & update together) </li></ul><ul><li>Need to be used when select & update statements are expected. </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  13. 13. #11 : Storing huge data into single table <ul><li>Do horizontal partitioning </li></ul><ul><li>Zippy uses it - Dumping all the data into beacon tables @2000 inserts/s </li></ul><ul><li>Can be done on time based: </li></ul><ul><ul><li>CREATE TABLE ( ... ) PARTITION BY RANGE( ( time DIV 3600 ) MOD 24 ) ... </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  14. 14. #12 : Not using Query Cache wisely <ul><li>Must understand application read/write ratio </li></ul><ul><ul><li>mysql> SHOW VARIABLES LIKE ‘%query_cache%’ ; </li></ul></ul><ul><li>Bigger Query cache != Better Performance </li></ul><ul><li>Any change in any table referenced in SELECT statement invalidates query cache </li></ul><ul><li>Remedy with Vertical table partitioning </li></ul><ul><li>Cache Miss Storm (a.k.a. cache stampede) </li></ul><ul><ul><li>It may occur with memcache & query cache both. </li></ul></ul><ul><ul><li>Solution: Identify the items with most hits, and cache it again before the expiry </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  15. 15. #13 : Doing DNS Hostname Lookup <ul><li>If DNS Hostname lookup is enabled, the MySQL thread resolves the hostname, if already not in hostname cache </li></ul><ul><ul><li>Calls functions gethostbyaddr_r() and gethostbyname_r() </li></ul></ul><ul><li>If DNS is slow or there are too many hosts, It resolving takes time </li></ul><ul><li>Solutions: </li></ul><ul><ul><li>Take Connection by IP addresses after disabling lookup (--skip-name-resolve) </li></ul></ul><ul><ul><ul><li>Disable the hostname cache with --skip-host-cache </li></ul></ul></ul><ul><ul><ul><li>Clear the hostname cache with FLUSH HOSTS or mysqladmin flush-hosts . </li></ul></ul></ul><ul><ul><li>Increase the HOST_CACHE_SIZE define (default: 128) </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  16. 16. #14 : Not profiling or benchmarking <ul><li>Profiling is a concept of diagnosing a system for bottlenecks </li></ul><ul><ul><li>Profile on staging environment with realistic data </li></ul></ul><ul><ul><li>Logs must be checked: </li></ul></ul><ul><ul><ul><li>log_slow_queries=/path/to/log </li></ul></ul></ul><ul><ul><ul><li>log_queries_not_using_indexes </li></ul></ul></ul><ul><li>Benchmarking is a process of evaluating application performance </li></ul><ul><ul><li>Track changes in application performance after making changes </li></ul></ul><ul><ul><li>Benchmark with enables / disabled Query Cache </li></ul></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  17. 17. More … How To Kill MySQL Performance - By Vishnu Agarwal
  18. 18. <ul><li>Operating on indexed column with a function </li></ul><ul><li>Having missing or useless indexes </li></ul><ul><li>Not understanding JOINs, using sub-queries </li></ul><ul><li>SELECT COUNT(*) with no WHERE on an InnoDB table </li></ul><ul><li>More … </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  19. 19. Tools & Techniques <ul><li>Show Profile </li></ul><ul><li>Slow query log </li></ul><ul><li>MyTOP / innotop </li></ul><ul><li>MyBench – Stress testing tool </li></ul><ul><li>Mysqlslap - Load Emulation Client </li></ul><ul><li>Tcprstat - TCP analysis tool that watches network traffic and computes the delay </li></ul><ul><li>mk-query-digest - Parses logs. Analyze, transform, filter, review and report on queries. </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  20. 20. References <ul><li>www.mysqlperformanceblog.com/2010/09/10/cache-miss-storm/ </li></ul><ul><li>http://dev.mysql.com/doc/refman/5.1/en/mysqlslap.html </li></ul><ul><li>http://www.percona.com/docs/wiki/tcprstat:start </li></ul><ul><li>http://www.maatkit.org/doc/mk-query-digest.html </li></ul>How To Kill MySQL Performance - By Vishnu Agarwal
  21. 21. Thanks Q & A Contact : timir126@gmail.com

×