MySQL 调优和使用必读
MySQL 应该是最流行了 WEB 后端数据库。WEB 开发语言最近发展很快,PHP, Ruby, Python, Java 各有特点,虽然 NOSQL 最近越來越多的被提到,但是相信大部分架构师还是会选择 MySQL 来做数据存储。
MySQL 如此方便和稳定,以至于我们在开发 WEB 程序的时候很少想到它。即使想到优化也是程序级别的,比如,不要写过于消耗资源的 SQL 语句。但是除此之外,在整个系统上仍然有很多可以优化的地方。
1. 选择合适的存储引擎: InnoDB
除非你的数据表使用来做只读或者全文检索 (相信现在提到全文检索,没人会用 MYSQL 了),你应该默认选择 InnoDB 。
你自己在测试的时候可能会发现 MyISAM 比 InnoDB 速度快,这是因为: MyISAM 只缓存索引,而 InnoDB 缓存数据和索引,MyISAM 不支持事务。但是 如果你使用 innodb_flush_log_at_trx_commit = 2<strong> </strong>
可以获得接近的读取性能 (相差百倍) 。
1.1 如何将现有的 MyISAM 数据库转换为 InnoDB:
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">u </span><span class="pun">[</span><span class="pln">USER_NAME</span><span class="pun">]</span><span class="pun">-</span><span class="pln">p </span><span class="pun">-</span><span class="pln">e </span><span class="str">"SHOW TABLES IN [DATABASE_NAME];"</span><span class="pun">|</span><span class="pln"> tail </span><span class="pun">-</span><span class="pln">n </span><span class="pun">+</span><span class="lit">2</span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="pln">I </span><span class="str">'{}'</span><span class="pln"> echo </span><span class="str">"ALTER TABLE {} ENGINE=InnoDB;"</span><span class="pun">></span><span class="pln"> alter_table</span><span class="pun">.</span><span class="pln">sql</span>
<span class="pln">perl </span><span class="pun">-</span><span class="pln">p </span><span class="pun">-</span><span class="pln">i </span><span class="pun">-</span><span class="pln">e </span><span class="str">'s/(search_[a-z_]+ ENGINE=)InnoDB/\1MyISAM/g'</span><span class="pln"> alter_table</span><span class="pun">.</span><span class="pln">sql</span>
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">u </span><span class="pun">[</span><span class="pln">USER_NAME</span><span class="pun">]</span><span class="pun">-</span><span class="pln">p </span><span class="pun">[</span><span class="pln">DATABASE_NAME</span><span class="pun">]</span><span class="pun"><</span><span class="pln"> alter_table</span><span class="pun">.</span><span class="pln">sql</span>
1.2 为每个表分别创建 InnoDB FILE:
<span class="pln">innodb_file_per_table</span><span class="pun">=</span><span class="lit">1</span>
这样可以保证 ibdata1 文件不会过大,失去控制。尤其是在执行 mysqlcheck -o –all-databases
的时候。
2. 保证从内存中读取数据,讲数据保存在内存中
2.1 足够大的 innodb_buffer_pool_size
推荐将数据完全保存在 innodb_buffer_pool_size
,即按存储量规划 innodb_buffer_pool_size
的容量。这样你可以完全从内存中读取数据,最大限度减少磁盘操作。
2.1.1 如何确定 innodb_buffer_pool_size 足够大,数据是从内存读取而不是硬盘?
方法 1
<span class="pln">mysql</span><span class="pun">></span><span class="pln"> SHOW GLOBAL STATUS LIKE </span><span class="str">'innodb_buffer_pool_pages_%'</span><span class="pun">;</span>
<span class="pun">+----------------------------------+--------+</span>
<span class="pun">|</span><span class="typ">Variable_name</span><span class="pun">|</span><span class="typ">Value</span><span class="pun">|</span>
<span class="pun">+----------------------------------+--------+</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_data</span><span class="pun">|</span><span class="lit">129037</span><span class="pun">|</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_dirty</span><span class="pun">|</span><span class="lit">362</span><span class="pun">|</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_flushed</span><span class="pun">|</span><span class="lit">9998</span><span class="pun">|</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_free</span><span class="pun">|</span><span class="lit">0</span><span class="pun">|</span><span class="pun">!!!!!!!!</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_misc</span><span class="pun">|</span><span class="lit">2035</span><span class="pun">|</span>
<span class="pun">|</span><span class="typ">Innodb_buffer_pool_pages_total</span><span class="pun">|</span><span class="lit">131072</span><span class="pun">|</span>
<span class="pun">+----------------------------------+--------+</span>
<span class="lit">6</span><span class="pln"> rows </span><span class="kwd">in</span><span class="kwd">set</span><span class="pun">(</span><span class="lit">0.00</span><span class="pln"> sec</span><span class="pun">)</span>
发现 Innodb_buffer_pool_pages_free
为 0,则说明 buffer pool 已经被用光,需要增大 innodb_buffer_pool_size
InnoDB 的其他几个参数:
<span class="pln">innodb_additional_mem_pool_size </span><span class="pun">=</span><span class="lit">1</span><span class="pun">/</span><span class="lit">200</span><span class="pln"> of buffer_pool</span>
<span class="pln">innodb_max_dirty_pages_pct </span><span class="lit">80</span><span class="pun">%</span>
方法 2
或者用iostat -d -x -k 1
命令,查看硬盘的操作。
2.1.2 服务器上是否有足够内存用来规划
执行 echo 1 > /proc/sys/vm/drop_caches
清除操作系统的文件缓存,可以看到真正的内存使用量。
2.2 数据预热
默认情况,只有某条数据被读取一次,才会缓存在 innodb_buffer_pool
。所以,数据库刚刚启动,需要进行数据预热,将磁盘上的所有数据缓存到内存中。数据预热可以提高读取速度。
对于 InnoDB 数据库,可以用以下方法,进行数据预热:
1. 将以下脚本保存为 MakeSelectQueriesToLoad.sql
<span class="pln">SELECT DISTINCT</span>
<span class="pln">CONCAT</span><span class="pun">(</span><span class="str">'SELECT '</span><span class="pun">,</span><span class="pln">ndxcollist</span><span class="pun">,</span><span class="str">' FROM '</span><span class="pun">,</span><span class="pln">db</span><span class="pun">,</span><span class="str">'.'</span><span class="pun">,</span><span class="pln">tb</span><span class="pun">,</span>
<span class="str">' ORDER BY '</span><span class="pun">,</span><span class="pln">ndxcollist</span><span class="pun">,</span><span class="str">';'</span><span class="pun">)</span><span class="typ">SelectQueryToLoadCache</span>
<span class="pln">FROM</span>
<span class="pun">(</span>
<span class="pln">SELECT</span>
<span class="pln">engine</span><span class="pun">,</span><span class="pln">table_schema db</span><span class="pun">,</span><span class="pln">table_name tb</span><span class="pun">,</span>
<span class="pln">index_name</span><span class="pun">,</span><span class="pln">GROUP_CONCAT</span><span class="pun">(</span><span class="pln">column_name ORDER BY seq_in_index</span><span class="pun">)</span><span class="pln"> ndxcollist</span>
<span class="pln">FROM</span>
<span class="pun">(</span>
<span class="pln">SELECT</span>
<span class="pln">B</span><span class="pun">.</span><span class="pln">engine</span><span class="pun">,</span><span class="pln">A</span><span class="pun">.</span><span class="pln">table_schema</span><span class="pun">,</span><span class="pln">A</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">,</span>
<span class="pln">A</span><span class="pun">.</span><span class="pln">index_name</span><span class="pun">,</span><span class="pln">A</span><span class="pun">.</span><span class="pln">column_name</span><span class="pun">,</span><span class="pln">A</span><span class="pun">.</span><span class="pln">seq_in_index</span>
<span class="pln">FROM</span>
<span class="pln">information_schema</span><span class="pun">.</span><span class="pln">statistics A INNER JOIN</span>
<span class="pun">(</span>
<span class="pln">SELECT engine</span><span class="pun">,</span><span class="pln">table_schema</span><span class="pun">,</span><span class="pln">table_name</span>
<span class="pln">FROM information_schema</span><span class="pun">.</span><span class="pln">tables WHERE</span>
<span class="pln">engine</span><span class="pun">=</span><span class="str">'InnoDB'</span>
<span class="pun">)</span><span class="pln"> B USING </span><span class="pun">(</span><span class="pln">table_schema</span><span class="pun">,</span><span class="pln">table_name</span><span class="pun">)</span>
<span class="pln">WHERE B</span><span class="pun">.</span><span class="pln">table_schema NOT IN </span><span class="pun">(</span><span class="str">'information_schema'</span><span class="pun">,</span><span class="str">'mysql'</span><span class="pun">)</span>
<span class="pln">ORDER BY table_schema</span><span class="pun">,</span><span class="pln">table_name</span><span class="pun">,</span><span class="pln">index_name</span><span class="pun">,</span><span class="pln">seq_in_index</span>
<span class="pun">)</span><span class="pln"> A</span>
<span class="pln">GROUP BY table_schema</span><span class="pun">,</span><span class="pln">table_name</span><span class="pun">,</span><span class="pln">index_name</span>
<span class="pun">)</span><span class="pln"> AA</span>
<span class="pln">ORDER BY db</span><span class="pun">,</span><span class="pln">tb</span>
<span class="pun">;</span>
2. 执行
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">uroot </span><span class="pun">-</span><span class="pln">AN </span><span class="pun"><</span><span class="str">/root/</span><span class="typ">MakeSelectQueriesToLoad</span><span class="pun">.</span><span class="pln">sql </span><span class="pun">></span><span class="str">/root/</span><span class="typ">SelectQueriesToLoad</span><span class="pun">.</span><span class="pln">sql</span>
3. 每次重启数据库,或者整库备份前需要预热的时候执行:
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">uroot </span><span class="pun"><</span><span class="str">/root/</span><span class="typ">SelectQueriesToLoad</span><span class="pun">.</span><span class="pln">sql </span><span class="pun">></span><span class="str">/dev/</span><span class="kwd">null</span><span class="lit">2</span><span class="pun">>&</span><span class="lit">1</span>
2.3 不要让数据存到 SWAP 中
如果是专用 MYSQL 服务器,可以禁用 SWAP,如果是共享服务器,确定 innodb_buffer_pool_size
足够大。或者使用固定的内存空间做缓存,使用 memlock
指令。
3. 定期优化重建数据库
mysqlcheck -o –all-databases
会让 ibdata1 不断增大,真正的优化只有重建数据表结构:
<span class="pln">CREATE TABLE mydb</span><span class="pun">.</span><span class="pln">mytablenew LIKE mydb</span><span class="pun">.</span><span class="pln">mytable</span><span class="pun">;</span>
<span class="pln">INSERT INTO mydb</span><span class="pun">.</span><span class="pln">mytablenew SELECT </span><span class="pun">*</span><span class="pln"> FROM mydb</span><span class="pun">.</span><span class="pln">mytable</span><span class="pun">;</span>
<span class="pln">ALTER TABLE mydb</span><span class="pun">.</span><span class="pln">mytable RENAME mydb</span><span class="pun">.</span><span class="pln">mytablezap</span><span class="pun">;</span>
<span class="pln">ALTER TABLE mydb</span><span class="pun">.</span><span class="pln">mytablenew RENAME mydb</span><span class="pun">.</span><span class="pln">mytable</span><span class="pun">;</span>
<span class="pln">DROP TABLE mydb</span><span class="pun">.</span><span class="pln">mytablezap</span><span class="pun">;</span>
4. 减少磁盘写入操作
4.1 使用足够大的写入缓存 innodb_log_file_size
但是需要注意如果用 1G 的 innodb_log_file_size
,假如服务器当机,需要 10 分钟来恢复。
推荐 innodb_log_file_size
设置为 0.25 * innodb_buffer_pool_size
4.2 innodb_flush_log_at_trx_commit
这个选项和写磁盘操作密切相关:
innodb_flush_log_at_trx_commit = 1
则每次修改写入磁盘innodb_flush_log_at_trx_commit = 0/2
每秒写入磁盘
如果你的应用不涉及很高的安全性 (金融系统),或者基础架构足够安全,或者 事务都很小,都可以用 0 或者 2 来降低磁盘操作。
4.3 避免双写入缓冲
<span class="pln">innodb_flush_method</span><span class="pun">=</span><span class="pln">O_DIRECT</span>
5. 提高磁盘读写速度
RAID0 尤其是在使用 EC2 这种虚拟磁盘 (EBS) 的时候,使用软 RAID0 非常重要。
6. 充分使用索引
6.1 查看现有表结构和索引
<span class="pln">SHOW CREATE TABLE db1</span><span class="pun">.</span><span class="pln">tb1\G</span>
6.2 添加必要的索引
索引是提高查询速度的唯一方法,比如搜索引擎用的倒排索引是一样的原理。
索引的添加需要根据查询来确定,比如通过慢查询日志或者查询日志,或者通过 EXPLAIN 命令分析查询。
<span class="pln">ADD UNIQUE INDEX</span>
<span class="pln">ADD INDEX</span>
6.2.1 比如,优化用户验证表:
添加索引
<span class="pln">ALTER TABLE users ADD UNIQUE INDEX username_ndx </span><span class="pun">(</span><span class="pln">username</span><span class="pun">);</span>
<span class="pln">ALTER TABLE users ADD UNIQUE INDEX username_password_ndx </span><span class="pun">(</span><span class="pln">username</span><span class="pun">,</span><span class="pln">password</span><span class="pun">);</span>
每次重启服务器进行数据预热
<span class="pln">echo </span><span class="pun">“</span><span class="kwd">select</span><span class="pln"> username</span><span class="pun">,</span><span class="pln">password </span><span class="kwd">from</span><span class="pln"> users</span><span class="pun">;”</span><span class="pun">></span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span><span class="pln">upcache</span><span class="pun">.</span><span class="pln">sql</span>
添加启动脚本到 my.cnf
<span class="pun">[</span><span class="pln">mysqld</span><span class="pun">]</span>
<span class="pln">init</span><span class="pun">-</span><span class="pln">file</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span><span class="pln">upcache</span><span class="pun">.</span><span class="pln">sql</span>
6.2.2 使用自动加索引的框架或者自动拆分表结构的框架
比如,Rails 这样的框架,会自动添加索引,Drupal 这样的框架会自动拆分表结构。会在你开发的初期指明正确的方向。所以,经验不太丰富的人一开始就追求从 0 开始构建,实际是不好的做法。
7. 分析查询日志和慢查询日志
记录所有查询,这在用 ORM 系统或者生成查询语句的系统很有用。
<span class="pln">log</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">.</span><span class="pln">log</span>
注意不要在生产环境用,否则会占满你的磁盘空间。
记录执行时间超过 1 秒的查询:
<span class="pln">long_query_time</span><span class="pun">=</span><span class="lit">1</span>
<span class="pln">log</span><span class="pun">-</span><span class="pln">slow</span><span class="pun">-</span><span class="pln">queries</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span><span class="pln">log</span><span class="pun">-</span><span class="pln">slow</span><span class="pun">-</span><span class="pln">queries</span><span class="pun">.</span><span class="pln">log</span>
8. 激进的方法,使用内存磁盘
现在基础设施的可靠性已经非常高了,比如 EC2 几乎不用担心服务器硬件当机。而且内存实在是便宜,很容易买到几十G内存的服务器,可以用内存磁盘,定期备份到磁盘。
将 MYSQL 目录迁移到 4G 的内存磁盘
<span class="pln">mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">ramdisk</span>
<span class="pln">sudo mount </span><span class="pun">-</span><span class="pln">t tmpfs </span><span class="pun">-</span><span class="pln">o size</span><span class="pun">=</span><span class="lit">4000M</span><span class="pln"> tmpfs </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">ramdisk</span><span class="pun">/</span>
<span class="pln">mv </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">ramdisk</span><span class="pun">/</span><span class="pln">mysql</span>
<span class="pln">ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">ramdisk</span><span class="pun">/</span><span class="pln">mysql </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql</span>
<span class="pln">chown mysql</span><span class="pun">:</span><span class="pln">mysql mysql</span>
9. 用 NOSQL 的方式使用 MYSQL
B-TREE 仍然是最高效的索引之一,所有 MYSQL 仍然不会过时。
用 HandlerSocket 跳过 MYSQL 的 SQL 解析层,MYSQL 就真正变成了 NOSQL。
10. 其他
- 单条查询最后增加 LIMIT 1,停止全表扫描。
- 将非”索引”数据分离,比如将大篇文章分离存储,不影响其他自动查询。
- 不用 MYSQL 内置的函数,因为内置函数不会建立查询缓存。
- PHP 的建立连接速度非常快,所有可以不用连接池,否则可能会造成超过连接数。当然不用连接池 PHP 程序也可能将
- 连接数占满比如用了
@ignore_user_abort(TRUE);
- 使用 IP 而不是域名做数据库路径,避免 DNS 解析问题
11. 结束
你会发现优化后,数据库的性能提高几倍到几百倍。所以 MYSQL 基本还是可以适用大部分场景的应用的。优化现有系统的成本比系统重构或者迁移到 NOSQL 低很多。