2007-11-15
对拥有一个几十万行表的 MySQL 性能优化的简单办法
关键字: 几十万行 MySQL 优化| 对拥有一个几十万行表的 MySQL 性能优化的简单办法 | |
数据库的优化大概是在系统管理中最具有挑战性的了,因为其对人员的素质要求几乎是全方面的,好的 DBA 需要各种综合素质。在排除了操作系统,应用等引起的性能问题以外,优化数据库最核心的实际上就是配置参数的调整。本文通过一个简单的参数调整,实现了对拥有一个几十万行表的 group by 优化的例子。通过这个简单的调整,数据库性能有了突飞猛进的提升。
本例子是针对 MySQL 调整的,不像其他商业数据库,MySQL 没有视图,特别是 Oracle 可以利用固化视图来提升查询性能,没有存储过程,因此性能的调整几乎只能通过配置合适的参数来实现。
调整的具体步骤(例子针对 pLog 0.3x 的博客系统):
发现最多的 slow log 是:
SELECT category_id, COUNT(*) AS 'count' FROM plog_articles WHERE blog_id = 2 AND status = 'PUBLISHED' group by category_id;
一般在 20s 以上,甚至 30s 。
而当 blog_id=1 或者其他时,都能很快的选出结果。
于是怀疑索引有问题,重新建立索引,但无济于事。 EXPLAIN 结果如下:
mysql> EXPLAIN SELECT category_id, COUNT(*) AS 'count' FROM plog_articles WHERE blog_id = 2 AND status = 'PUBLISHED' group by category_id;
+---------------+------+------------------+------------------+---------+-------------+------+----------------------------------------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+---------------+------+------------------+------------------+---------+-------------+------+----------------------------------------------+
| plog_articles | ref | idx_article_blog | idx_article_blog | 5 | const,const | 4064 | Using where; Using temporary; Using filesort |
+---------------+------+------------------+------------------+---------+-------------+------+----------------------------------------------+
1 row in set (0.00 sec)
于是想到每次查看 blog_id = 2 的博客时,系统负载就提高,有较高的 swap 。于是查看 temporary table 有关的资料,果然有这样的说法:
If you create a lot of disk-based temporary tables, increase the size of tmp_table_size if you can do so safely. Keep in mind that setting the value too high may result in excessive swapping or MySQL running out of memory if too many threads attempt to allocate in-memory temporary tables at the same time. Otherwise, make sure that tmpdir points to a very fast disk that's not already doing lots of I/O.
Another problem that doesn't show up in the slow query log is an excessive use of disk-based temporary tables. In the output of EXPLAIN, you'll often see Using temporary. It indicates that MySQL must create a temporary table to complete the query. However, it doesn't tell you whether that temporary table will be in memory or on disk. That's controlled by the size of the table and MySQL's tmp_table_size variable.
If the space required to build the temporary table is less than or equal to tmp_table_size, MySQL keeps it in memory rather than incur the overhead and time required to write the data to disk and read it again. However, if the space required exceeds tmp_table_size, MySQL creates a disk-based table in its tmpdir directory (often /tmp on Unix systems.) The default tmp_table_size size is 32 MB.
To find out how often that happens, compare the relative sizes of the Created_tmp_tables and Created_tmp_disk_tables counters:
调整 tmp_table_size为 80M 左右后,以上语句 14s 即可解决。
这个参数是 DBA 很容易忽视的。
其实,不单单是数据库,就是操作系统,也是受 tmp 的影响巨大,例如安装软件到 d: 盘,如果 TMP 环境变量指向 c: 盘,而 c: 空间不够,照样可能导致安装失败。
因此让 TMP 有足够的空间可以说是计算机系统里一个普遍适用的原则(写程序也是一样)。
发表评论
- 浏览: 42957 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
链接
- Hibernate Tag 参考手册(中文翻译版1.0)
- 闲聊日语张宏
- 《NeoSpeech语音合成英、中、韩、日朗读引擎》
- 《新版中日交流标准日本语广播讲座初级》同步更新,2006年6月5日,更新全部ISO文件,最后更新[MP3!]
- 《番茄花园 Windows All In One DVD 特别版》[ISO]
- 中 国 DOS 联 盟
- 新DOS时代
- 《虚拟工作站 6.0.1 Build 55017》(VMware Workstation 6.0.1 Build 55017)VMware Workstation 6.0.1 Build 55017
- 《经典2D即时战略游戏集》(Classic 2D-RTS Games Collection)完美硬盘版
- 标题: 关于DOS游戏在XP系统下运行的简单总结!, 新版VDMSound和DOSBox已经出炉啦!
- DOSBox / VDMSound常見FAQ
- D - F e n d 簡 單 教 學
- Get It Done with MySQL 5
- 《沪江日语原创作品资料集(新增交响情人梦)》(jp.hjenglish.com)合辑
- SQL语句教程 - 由范例学习SQL语句
- 飞奔·慢行
- 地铁换乘/时间/价钱
最新评论
-
Hibernate3.x调用存储过程 ...
您好!hibernate提供的在*.hbm.xml中配置调用存储过程,关于< ...
-- by cheatsky -
KMP字符串模式匹配详解
我虽然曾经学过,看来一下感觉有些时候还是比较难理解!
-- by zhoujj303030 -
一个老IT人的自白:看十年 ...
我不认同三星有多牛,说白了,我认为高丽棒子做的东西真的一般般
-- by xbwolf -
JavaTiger(Java5.0) 新特 ...
感谢分享,解决了我的困惑
-- by onwulc -
磨刀不误砍柴功—难忘的Ja ...
...
-- by wcj10051891






评论排行榜