Sunday, June 23, 2013

Top 83 Mysql Performance Tips


MySQL is a widely used and fast SQL database server. It is a client/server implementation that consists of a server daemon (mysqld) and many different client programs/libraries.

You can check the same tips from here.Here is very useful tips for all mysql DBA's,Developers these tips are noted from MySQL Camp 2006 suggested by mysql community experts.




  1. Don't Index Everything
  2. Use benchmarking
  3. Minimize traffic by fetching only what you need.
    1. Paging/chunked data retrieval to limit
    2. Don't use SELECT *
    3. Be wary of lots of small quick queries if a longer query can be more efficient
  4. Use EXPLAIN to profile the query execution plan
  5. Use Slow Query Log (always have it on!)
  6. Don't use DISTINCT when you have or could use GROUP BY
  7. Use proper data partitions
    1. For Cluster. Start thinking about Cluster *before* you need them
  8. Insert performance
    1. Batch INSERT and REPLACE
    2. Use LOAD DATA instead of INSERT
  9. LIMIT m,n may not be as fast as it sounds
  10. Don't use ORDER BY RAND() if you have > ~2K records
  11. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
  12. avoid wildcards at the start of LIKE queries
  13. avoid correlated subqueries and in select and where clause (try to avoid in)
  14. config params --
  15. no calculated comparisons -- isolate indexed columns
  16. innodb_flush_commit=0 can help slave lag
  17. ORDER BY and LIMIT work best with equalities and covered indexes
  18. isolate workloads don't let administrative work interfere with customer performance. (ie backups)
  19. use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
  20. use row-level instead of table-level locking for OLTP workloads
  21. Know your storage engines and what performs best for your needs, know that different ones exist.
    1. use MERGE tables ARCHIVE tables for logs
  22. Optimize for data types, use consistent data types. Use PROCEDURE ANALYSE() to help determine if you need less
  23. separate text/blobs from metadata, don't put text/blobs in results if you don't need them
  24. if you can, compress text/blobs
  25. compress static data
  26. don't back up static data as often
  27. derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs w/out sorting them. (self-join can speed up a query if 1st part finds the IDs and use it to fetch the rest)
  28. enable and increase the query and buffer caches if appropriate
  29. ALTER TABLE...ORDER BY can take chronological data and re-order it by a different field
  30. InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large, be careful of redundant columns in an index, and this can make the query faster
  31. Do not duplicate indexes
  32. Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
  33. BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
  34. Design sane query schemas. don't be afraid of table joins, often they are faster than denormalization
  35. Don't use boolean flags
  36. Use a clever key and ORDER BY instead of MAX
  37. Keep the database host as clean as possible. Do you really need a windowing system on that server?
  38. Utilize the strengths of the OS
  39. Hire a MySQL (tm) Certified DBA
  40. Know that there are many consulting companies out there that can help, as well as MySQL's Professional Services.
  41. Config variables & tips:
    1. use one of the supplied config files
    2. key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
    3. be aware of global vs. per-connection variables
    4. check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
    5. be aware of swapping esp. with Linux, "swappiness" (bypass OS filecache for innodb data files, innodb_flush_method=O_DIRECT if possible (this is also OS specific))
    6. defragment tables, rebuild indexes, do table maintenance
    7. If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
    8. more RAM is good so faster disk speed
    9. use 64-bit architectures
  42. Know when to split a complex query and join smaller ones
  43. Debugging sucks, testing rocks!
  44. Delete small amounts at a time if you can
  45. Archive old data -- don't be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
  46. use INET_ATON and INET_NTOA for IP addresses, not char or varchar
  47. make it a habit to REVERSE() email addresses, so you can easily search domains
  48. --skip-name-resolve
  49. increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
  50. look up memory tuning parameter for on-insert caching
  51. increase temp table size in a data warehousing environment (default is 32Mb) so it doesn't write to disk (also constrained by max_heap_table_size, default 16Mb)
  52. Normalize first, and denormalize where appropriate.
  53. Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn't a real database
  54. In 5.1 BOOL/BIT NOT NULL type is 1 bit, in previous versions it's 1 byte.
  55. A NULL data type can take more room to store than NOT NULL
  56. Choose appropriate character sets & collations -- UTF16 will store each character in 2 bytes, whether it needs it or not, latin1 is faster than UTF8.
  57. make similar queries consistent so cache is used
  58. Have good SQL query standards
  59. Don't use deprecated features
  60. Use Triggers wisely
  61. Run in SQL_MODE=STRICT to help identify warnings
  62. Turning OR on multiple index fields (<5.0)>
  63. /tmp dir on battery-backed write cache
  64. consider battery-backed RAM for innodb logfiles
  65. use min_rows and max_rows to specify approximate data size so space can be pre-allocated and reference points can be calculated.
  66. as your data grows, indexing may change (cardinality and selectivity change). Structuring may want to change. Make your schema as modular as your code. Make your code able to scale. Plan and embrace change, and get developers to do the same.
  67. pare down cron scripts
  68. create a test environment
  69. try out a few schemas and storage engines in your test environment before picking one.
  70. Use HASH indexing for indexing across columns with similar data prefixes
  71. Use myisam_pack_keys for int data
  72. Don't use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
  73. use --safe-updates for client
  74. Redundant data is redundant
  75. Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
  76. use groupwise maximum instead of subqueries
  77. be able to change your schema without ruining functionality of your code
  78. source control schema and config files
  79. for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
  80. use multi_query if appropriate to reduce round-trips
  81. partition appropriately
  82. partition your database when you have real data
  83. segregate tables/databases that benefit from

0 comments:

Post a Comment

Please Don't write wrong comments.
.
Please write good comments or mistakes ...