手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2009年06月的文章

discuz数据表优化

这是来自imysql.cn的文章,作者是叶金荣,第一部分内容是3年前的了。可略作参考,估计7.0的数据库应该已经部分解决这个问题,第二部分是最新的。或许也能帮助你解决一些问题。

对于我来说是不用的啦。。我的论坛才几十个人。根本不需要用到这些功能。哇哈哈哈。
不过,我记得,如果是自己的服务器架设的论坛,DZ可以通过打开APC来进行缓存加速(好象是6.X版本中的功能。7.X没有研究过是不是还存在)

不说废话,看叶金荣先生的文章:


第一部分:

一. 前言
近日由于需要,对discuz论坛(简称dz)进行优化,当然了,只是涉及到数据库的优化.
先说一下服务器及dz的数据量,2 * Intel(R) Xeon(TM) CPU 2.40GHz, 4GB mem, SCISC硬盘.
MySQL 版本为 4.0.23. 数据表情况:
cdb_attachments 2万
cdb_members 10万
cdb_posts 68万
cdb_threads 7万
二. 缓存优化
在 my.cnf 中添加/修改以下选项:

#取消文件系统的外部锁
skip-locking
#不进行域名反解析,注意由此带来的权限/授权问题
skip-name-resolve
#索引缓存,根据内存大小而定,如果是独立的db服务器,可以设置高达80%的内存总量
key_buffer = 512M
#连接排队列表总数
back_log = 200
max_allowed_packet = 2M
#打开表缓存总数,可以避免频繁的打开数据表产生的开销
table_cache = 512
#每个线程排序所需的缓冲
sort_buffer_size = 4M
#每个线程读取索引所需的缓冲
read_buffer_size = 4M
#MyISAM表发生变化时重新排序所需的缓冲
myisam_sort_buffer_size = 64M
#缓存可重用的线程数
thread_cache = 128
#查询结果缓存
query_cache_size = 128M
#设置超时时间,能避免长连接
set-variable = wait_timeout=60
#最大并发线程数,cpu数量*2
thread_concurrency = 4
#记录慢查询,然后对慢查询一一优化
log-slow-queries = slow.log
long_query_time = 1
#关闭不需要的表类型,如果你需要,就不要加上这个
skip-bdb

以上参数根据各自服务器的配置差异进行调整,仅作为参考.
三. 索引优化
上面提到了,已经开启了慢查询,那么接下来就要对慢查询进行逐个优化了.
1. 搜索优化
搜索的查询SQL大致如下:

SELECT t.* FROM cdb_posts p, cdb_threads t WHERE
t.fid IN ('37', '45', '4', '6', '17', '41', '28', '32', '31', '1', '42')
AND p.tid=t.tid AND p.author LIKE 'JoansWin'
GROUP BY t.tid ORDER BY lastpost DESC LIMIT 0, 80;

用 EXPLAIN 分析的结果如下:

mysql>EXPLAIN  SELECT t.* FROM cdb_posts p, cdb_threads t WHERE
t.fid IN ('37', '45', '4', '6', '17', '41', '28', '32', '31', '1', '42')
AND p.tid=t.tid AND p.author LIKE 'JoansWin'
GROUP BY t.tid ORDER BY lastpost DESC LIMIT 0, 80;
+-----------+------------+----------+--------------+-------------+-----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
+-----------+------------+----------+--------------+-------------+-----------+-------------+
| 1 | SIMPLE | t | range | PRIMARY,fid | fid | 2 | NULL | 66160 | Using where;
Using temporary; Using filesort |
| 1 | SIMPLE | p | ref | tid | tid | 3 | Forum.t.tid | 10 | Using where
| +----+-------------+-------+-------+---------------+------+---------+-------------+-------+
---------

只用到了 t.fidp.tid,而 p.author 则没有索引可用,总共需要扫描
66160*10 = 661600 次索引,够夸张吧 :(
再分析 cdb_threadscdb_posts 的索引情况:

mysql>show index from cdb_posts; 
+-----------+------------+----------+--------------+-------------+-----------+----------
---+----------+--------+------+--+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment | +-----------+------------+----------+--------------+----
---------+-----------+-------------+----------+--------+------+--+
| cdb_posts | 0 | PRIMARY | 1 | pid | A | 680114 | NULL | NULL |
| BTREE | |
| cdb_posts | 1 | fid | 1 | fid | A | 10 | NULL | NULL |
| BTREE | |
| cdb_posts | 1 | tid | 1 | tid | A | 68011 | NULL | NULL |
| BTREE | |
| cdb_posts | 1 | tid | 2 | dateline | A | 680114 | NULL | NULL |
| BTREE | |
| cdb_posts | 1 | dateline | 1 | dateline | A | 680114 | NULL | NULL |
| BTREE | |
+-----------+------------+----------+--------------+-------------+-----------+---

以及

mysql>show index from cdb_threads; 
+-----------+------------+----------+--------------+-------------+-----------+-------------+
----------+--------+------+-----+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment | +-----------+------------+----------+--------------+-----
--------+-----------+-------------+----------+--------+------+-----+
| cdb_threads | 0 | PRIMARY | 1 | tid | A | 68480 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | lastpost | 1 | topped | A | 4 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | lastpost | 2 | lastpost | A | 68480 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | lastpost | 3 | fid | A | 68480 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | replies | 1 | replies | A | 233 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | dateline | 1 | dateline | A | 68480 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | fid | 1 | fid | A | 10 | NULL | NULL |
| BTREE | |
| cdb_threads | 1 | enablehot | 1 | enablehot | A | 2 | NULL | NULL |
| BTREE | | +-------------+------------+-----------+--------------+-------------+------

看到索引 fidenablehot 基数太小,看来该索引完全没必要,不过,对于fid基数较大的情况,则可能需要保留>该索引.
所做修改如下:

ALTER TABLE `cdb_threads` DROP INDEX `enablehot`, DROP INDEX `fid`, ADD INDEX (`fid`, `lastpost`);
ALTER TABLE `cdb_posts` DROP INDEX `fid`, ADD INDEX (`author`(10));
OPTIMIZE TABLE `cdb_posts`;
OPTIMIZE TABLE `cdb_threads`;

在这里, p.author 字段我设定的部分索引长度是 10, 是我经过分析后得出来的结果,不同的系统,这里的长度也不同,最好自己先取一下平均值,然后再适当调整.
现在,再来执行一次上面的慢查询,发现时间已经从 6s 变成 0.19s,提高了 30 倍.
这次先到这里,下次继续 ^_^


第二部分:

很早以前写过一个文章,是关于discuz论坛的优化:MySQL优化 之 Discuz论坛优化。 写的时候是2006年,没想到过了这么久,discuz论坛的问题还是困扰着很多网友,其实从各论坛里看到的问题总结出来,很关键的一点都是因为没有将数 据表引擎转成InnoDB导致的,discuz在并发稍微高一点的环境下就表现的非常糟糕,产生大量的锁等待,这时候如果把数据表引擎改成InnoDB的 话,我相信会好很多。这次就写个扫盲贴吧。

1. 启用innodb引擎,并配置相关参数

#skip-innodb
innodb_additional_mem_pool_size = 16M #一般16M也够了,可以适当调整下
innodb_buffer_pool_size = 6G #如果是专用db的话,一般是内存总量的80%
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 20
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 50
innodb_lock_wait_timeout = 120
innodb_file_per_table

2. 修改表引擎为innodb

mysql> alter table cdb_access engine = innodb;

其他表类似上面,把表名换一下即可...
将表存储引擎改成innodb后,不仅可以避免大量的锁等待,还可以提升查询的效率,因为innodb会把data和index都放在buffer pool中,效率更高。

 


膘叔认为:如果有自己的服务器,可以考虑做几件事情
1、如果有APC功能打开APC
2、如果没有APC,可以考虑把缓存目录指定为内存看看
3、GZIP关闭,少用rewrite等
4、在大负载的情况下,又只有一台服务器,考虑改程序,延迟插入或者其他的。。。(不太现实,哈哈)

 

Tags: discuz, mysql, innodb

关于mysql 1366的错误。

在查mysql 1366的错误原因时,发现一些有趣的回答。地址为:http://zhidao.baidu.com/question/59029575.html

不过,还是有人给出了比较好的解决方法:

  1. MySQL 1366错误大致描述如下  
  2.   
  3.    1. SQL Error: 1366: Incorrect string value: "\xE8\xAF\xA6\xE7\xBB\x86…" for column "address" at row 1  
  4.   
  5. 解决办法:检查数据库此字段的字符集与整理字符集是否与SQL语句传递数据的字符集相同;不相同则会引发MySQL1366错误。  
  6.   
  7. 修改MySQL该字段的字符集与整理规则即可。假设数据表为phplamp, SQL语句的字符集为utf8,出错的字段为address:  
  8. MySQL 1366 错误解决办法  
  9.   
  10.    1. #检查数据表所有字段的状态  
  11.    2. ->show full columns from phplamp;  
  12.    3. #发现address字段的Collation项非utf8,修改它!  
  13.    4. ->alter table phplamp change name name varchar(100) character set utf8 collate utf8_unicode_ci not null default '';  
  14.   
  15. 修改完字段的字符集后可以再使用show full columns from table_name命令检查一下,以确保万无一失。假如您的SQL字符集为GBK或是GB2312或是其它的话,只需要将数据表字段的字符集更改为其相应的编码即可。  
  16.   
  17. 再送上一个MySQL的命令:  
  18. 修改数据表的字符集与整理  
  19.   
  20.    1. ->show full columns from table_name;  

 

Tags: mysql, 1366

生日一天

一天中所拍摄的照片。。

大小: 52.52 K
尺寸: 500 x 375
浏览: 2008 次
点击打开新窗口浏览全图

大小: 56.91 K
尺寸: 500 x 375
浏览: 2077 次
点击打开新窗口浏览全图

大小: 38.2 K
尺寸: 282 x 376
浏览: 1983 次
点击打开新窗口浏览全图

大小: 56.01 K
尺寸: 500 x 375
浏览: 2034 次
点击打开新窗口浏览全图

大小: 55.53 K
尺寸: 500 x 375
浏览: 1963 次
点击打开新窗口浏览全图

更多相册请点击:http://picasaweb.google.com/xiaoyy2008/20090620#

Tags: 生日, 肖佑阳

征求意见:关于phped的虚拟空格

phped是一款PHP IDE,用下来感觉还不错
但是有一个功能是我最受不了的
就是虚拟空格。。。

鼠标点到哪里,就可以直接在哪里进行输入。
对于页面的控制不太好。不知道哪里可以关掉

顺便,再请各位看到本文推荐一款IDE
1、项目管理方便点
2、没有虚拟空格
3、在项目中可以指定单个文件编码
4、对于类或者函数的提示速度要快,要完善
5、对于单例化的class或者工厂模式的CLASS要能够进行提示

这么多,应该是每个用IDE的人都想解决的问题吧?
不知道有没有哪个IDE能够提供。。。

常用JS代码

两个常用的JS代码

Utf8
  1. /** 
  2. * 
  3. *  UTF-8 data encode / decode 
  4. *  http://www.webtoolkit.info/ 
  5. * 
  6. **/  
  7.    
  8. var Utf8 = {  
  9.    
  10.     // public method for url encoding  
  11.     encode : function (string) {  
  12.         string = string.replace(/\r\n/g,"\n");  
  13.         var utftext = "";  
  14.    
  15.         for (var n = 0; n < string.length; n++) {  
  16.    
  17.             var c = string.charCodeAt(n);  
  18.    
  19.             if (c < 128) {  
  20.                 utftext += String.fromCharCode(c);  
  21.             }  
  22.             else if((c > 127) && (c < 2048)) {  
  23.                 utftext += String.fromCharCode((c >> 6) | 192);  
  24.                 utftext += String.fromCharCode((c & 63) | 128);  
  25.             }  
  26.             else {  
  27.                 utftext += String.fromCharCode((c >> 12) | 224);  
  28.                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
  29.                 utftext += String.fromCharCode((c & 63) | 128);  
  30.             }  
  31.    
  32.         }  
  33.    
  34.         return utftext;  
  35.     },  
  36.    
  37.     // public method for url decoding  
  38.     decode : function (utftext) {  
  39.         var string = "";  
  40.         var i = 0;  
  41.         var c = c1 = c2 = 0;  
  42.    
  43.         while ( i < utftext.length ) {  
  44.    
  45.             c = utftext.charCodeAt(i);  
  46.    
  47.             if (c < 128) {  
  48.                 string += String.fromCharCode(c);  
  49.                 i++;  
  50.             }  
  51.             else if((c > 191) && (c < 224)) {  
  52.                 c2 = utftext.charCodeAt(i+1);  
  53.                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
  54.                 i += 2;  
  55.             }  
  56.             else {  
  57.                 c2 = utftext.charCodeAt(i+1);  
  58.                 c3 = utftext.charCodeAt(i+2);  
  59.                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
  60.                 i += 3;  
  61.             }  
  62.    
  63.         }  
  64.    
  65.         return string;  
  66.     }  
  67.    
  68. }  
Base64
  1. /** 
  2. * 
  3. *  Base64 encode / decode 
  4. *  http://www.webtoolkit.info/ 
  5. * 
  6. **/  
  7.    
  8. var Base64 = {  
  9.    
  10.     // private property  
  11.     _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",  
  12.    
  13.     // public method for encoding  
  14.     encode : function (input) {  
  15.         var output = "";  
  16.         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;  
  17.         var i = 0;  
  18.    
  19.         input = Base64._utf8_encode(input);  
  20.    
  21.         while (i < input.length) {  
  22.    
  23.             chr1 = input.charCodeAt(i++);  
  24.             chr2 = input.charCodeAt(i++);  
  25.             chr3 = input.charCodeAt(i++);  
  26.    
  27.             enc1 = chr1 >> 2;  
  28.             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
  29.             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
  30.             enc4 = chr3 & 63;  
  31.    
  32.             if (isNaN(chr2)) {  
  33.                 enc3 = enc4 = 64;  
  34.             } else if (isNaN(chr3)) {  
  35.                 enc4 = 64;  
  36.             }  
  37.    
  38.             output = output +  
  39.             this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +  
  40.             this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);  
  41.    
  42.         }  
  43.    
  44.         return output;  
  45.     },  
  46.    
  47.     // public method for decoding  
  48.     decode : function (input) {  
  49.         var output = "";  
  50.         var chr1, chr2, chr3;  
  51.         var enc1, enc2, enc3, enc4;  
  52.         var i = 0;  
  53.    
  54.         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");  
  55.    
  56.         while (i < input.length) {  
  57.    
  58.             enc1 = this._keyStr.indexOf(input.charAt(i++));  
  59.             enc2 = this._keyStr.indexOf(input.charAt(i++));  
  60.             enc3 = this._keyStr.indexOf(input.charAt(i++));  
  61.             enc4 = this._keyStr.indexOf(input.charAt(i++));  
  62.    
  63.             chr1 = (enc1 << 2) | (enc2 >> 4);  
  64.             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  
  65.             chr3 = ((enc3 & 3) << 6) | enc4;  
  66.    
  67.             output = output + String.fromCharCode(chr1);  
  68.    
  69.             if (enc3 != 64) {  
  70.                 output = output + String.fromCharCode(chr2);  
  71.             }  
  72.             if (enc4 != 64) {  
  73.                 output = output + String.fromCharCode(chr3);  
  74.             }  
  75.    
  76.         }  
  77.    
  78.         output = Base64._utf8_decode(output);  
  79.    
  80.         return output;  
  81.    
  82.     },  
  83.    
  84.     // private method for UTF-8 encoding  
  85.     _utf8_encode : function (string) {  
  86.         string = string.replace(/\r\n/g,"\n");  
  87.         var utftext = "";  
  88.    
  89.         for (var n = 0; n < string.length; n++) {  
  90.    
  91.             var c = string.charCodeAt(n);  
  92.    
  93.             if (c < 128) {  
  94.                 utftext += String.fromCharCode(c);  
  95.             }  
  96.             else if((c > 127) && (c < 2048)) {  
  97.                 utftext += String.fromCharCode((c >> 6) | 192);  
  98.                 utftext += String.fromCharCode((c & 63) | 128);  
  99.             }  
  100.             else {  
  101.                 utftext += String.fromCharCode((c >> 12) | 224);  
  102.                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
  103.                 utftext += String.fromCharCode((c & 63) | 128);  
  104.             }  
  105.    
  106.         }  
  107.    
  108.         return utftext;  
  109.     },  
  110.    
  111.     // private method for UTF-8 decoding  
  112.     _utf8_decode : function (utftext) {  
  113.         var string = "";  
  114.         var i = 0;  
  115.         var c = c1 = c2 = 0;  
  116.    
  117.         while ( i < utftext.length ) {  
  118.    
  119.             c = utftext.charCodeAt(i);  
  120.    
  121.             if (c < 128) {  
  122.                 string += String.fromCharCode(c);  
  123.                 i++;  
  124.             }  
  125.             else if((c > 191) && (c < 224)) {  
  126.                 c2 = utftext.charCodeAt(i+1);  
  127.                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
  128.                 i += 2;  
  129.             }  
  130.             else {  
  131.                 c2 = utftext.charCodeAt(i+1);  
  132.                 c3 = utftext.charCodeAt(i+2);  
  133.                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
  134.                 i += 3;  
  135.             }  
  136.    
  137.         }  
  138.    
  139.         return string;  
  140.     }  
  141.    
  142. }  

这两个应该是比较常用的了。不管是在AJAX中还是在其他中,都应该是很有用的

Tags: utf8, base64