MySQL配置优化需要避免的误区
来源:互联网 发布:卡中堂 知乎 编辑:程序博客网 时间:2024/05/16 10:58
Caution: Don’t overwrite your entire my.cnf at once when tuning MySQL. One or two changes per restart, benchmark, then continue. Take backups first and be careful!
It took me some time to decide the title for this article. MariaDB has been fast replacing MySQL as a growing number of Linux distributions now default to MariaDB over MySQL. In addition, DBA’s often manually replace MySQL with MariaDB.
MariaDB is an enhanced drop-in replacement for MySQL. Therein lies my largely self-made conundrum, MySQL or MariaDB? Last month I posted a poll on the Linux Google+ community: Which would you suggest… MySQL or MariaDB? And why?
The MySQL tuning advice below applies to both MySQL and MariaDB (and Percona) . After tuning MySQL over the years I can safely say that the following pitfall is one of the most common…
Tuning MySQL (my.cnf) – Avoid arbitrarily increasing per connection buffers
The my.cnf config file is well-known but also often grossly misconfigured. I’m not sure where or when it all started but it has become the norm to keep increasing the size and value of almost every setting in my.cnf without much reasoning behind those increases. Lets look at some important my.cnf parameters, where doing this, will not only hurt performance but also waste large chunks your server’s memory and as a result reduce MySQL’s overall capacity and throughput.
Not all buffers in my.cnf are global settings
Buffers such as join_buffer_size, sort_buffer_size, read_buffer_size and read_rnd_buffer_size are allocated per connection. Therefore a setting of read_buffer_size=1M and max_connections=150 is asking MySQL to allocate – from startup – 1MB per connection x 150 connections. For more than a decade the default remains at 128K. Increasing the default is not only a waste of server memory, but often does not help performance. In nearly all cases its best to use the defaults by removing or commenting out these four buffer config lines. For a more gradual approach, reduce them in half to free up wasted RAM, keep reducing them towards default values over time. I’ve actually seen improved throughput by reducing these buffers. But there’s really no performance gains from increasing these buffers except in cases of very high traffic and/or other special circumstances. Avoid arbitrarily increasing these!
Tuning MySQL join_buffer_size
The join_buffer_size is allocated for each full join between two tables. From MySQL’s documentation the join_buffer_size is described as: “The minimum size of the buffer that is used for plain index scans, range index scans, and joins that do not use indexes and thus perform full table scans.” It goes on to say: “Memory allocation time can cause substantial performance drops if the global size is larger than needed by most queries that use it.” The join buffer is allocated to cache table rows when the join can’t use an index. If your database(s) suffer from many joins performed without indexes it cannot be solved by just increasing join_buffer_size. The problem is “joins performed without indexes” and thus the solution for faster joins is to add indexes.
Tuning MySQL sort_buffer_size
Unless you have data to show otherwise, you should avoid arbitrarily increasing the sort_buffer_size as well. Memory here is also assigned per connection! MySQL’s documentation warns: “On Linux, there are thresholds of 256KB and 2MB where larger values may significantly slow down memory allocation, so you should consider staying below one of those values.” Don’t increase above 2M since there is a performance penalty going higher which sometimes can eliminate any benefits.
Rule of thumb when tuning MySQL, if you can’t provide a valid reason for increasing any of these buffers, keep them set to defaults (comment-out of config). These four often attract large increases from admins when tuning MySQL. If we think about the meaning of the word “buffer” we can easily see why these buffers are not the my.cnf settings which require such heavy attention.
Hopefully you’ve found the MySQL tuning info useful. Also see another pitfall: MySQL Query Cache Size and Performance. (Not mentioned in that article, but if you are using mostly InnoDB tables, then you may be better off just disabling query_cache completely). If using MariaDB you should also use Aria table types over MyISAM and Innodb in some cases. Aria tables perform really well!
- MySQL配置优化需要避免的误区
- PHP语言需要避免的10大误区
- PHP语言需要避免的10大误区
- 需要避免的MYSQL客户机程序设计错误
- 需要避免的MYSQL客户机程序设计错误
- 设计师分析需求的时候需要避免的4个误区详解
- 如何避免思维的误区
- 对PHP语言认识上需要避免的10大误区
- 提升网站排名需要避免网站优化的禁区
- 做企业网站seo需要避免哪些误区
- hashCode equal避免的几个误区
- 设备通信协议制定的一些误区避免
- 浅析MySQL JDBC连接配置上的两个误区
- Mysql性能优化需要考虑的因素
- mysql优化需要注意的点
- 搜索引擎优化的几个误区
- 细谈MySQL配置的优化
- mysql的一些配置优化
- [LeetCode] Poor Pigs
- MASKRCNN(之二)编译和错误解决
- 中文域名在线转码
- log4j中Pattern布局ConversionPattern详解
- bufferevent 流程
- MySQL配置优化需要避免的误区
- 生产产品绑定检测电流较高的问题
- android emulator虚拟设备分析第一篇之battery
- Pie Rules
- 软链接 硬链接
- Unity3d背包系统(四)—— 设计物品管理类——InventoryManager
- 函数中的this的四种绑定形式
- iOS开发之 Xcode 9 和iOS11 适配问题
- 微信浏览器无法使用window.location.reload()刷新页面