Monthly Archives: June 2008

mysql tuner-primer

mysql下tuner-primer使用说明
mysql tuner-primer是检测mysql的主配置文件my.cnf的有力工具,当然还有其他工具,比如mysqlreport,今天先介绍这个工具的使用。
下载并改变执行权限:
wget
http://www.day32.com/MySQL/tuning-primer.sh

chmod a+x tuning-primer.sh
./tuning-primer.sh
首先会要求输入服务器的用户名和密码,并提示保存在当前用户目录下配置文件。
再次执行脚本就会运行,不用输入用户名和密码,并给出参数配置,强悍就强悍在这!!!!
看看我的相关提示,
[root@slave1 tuning-primer]# ./tuning-primer.sh
– INITIAL LOGIN ATTEMPT FAILED –
Testing Stored for passwords: None Found
– RETRY LOGIN ATTEMPT FAILED –
Could not auto detect login info!
Do you have your login handy ? [y/N] : y
User: root
Password:
Would you like me to create a ~/.my.cnf file for you? [y/N] : y
        — MYSQL PERFORMANCE TUNING PRIMER —
             – By: Matthew Montgomery –
MySQL Version 5.1.22-rc-log i686
Uptime = 1 days 2 hrs 16 min 29 sec
Avg. qps = 348
Total Questions = 32946460
Threads Connected = 23
Warning: Server has not been running for at least 48hrs.
It may not be safe to use these recommendations
To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
Visit
http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL’s Enterprise Monitoring and Advisory Service
SLOW QUERIES
The slow query log is NOT enabled.
Current long_query_time = 10.000000 sec.
You have 44 out of 32946564 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine
BINARY UPDATE LOG
The binary update log is enabled
The expire_logs_days is not set.
The mysqld will retain the entire binary log until RESET MASTER or PURGE MASTER LOGS commands are run manually
Setting expire_log_days will allow you to remove old binary logs automatically
See
http://dev.mysql.com/doc/refman/5.1/en/purge-master-logs.html
WORKER THREADS
Current thread_cache_size = 1024
Current threads_cached = 363
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine
MAX CONNECTIONS
Current max_connections = 1500
Current threads_connected = 22
Historic max_used_connections = 391
The number of used connections is 26% of the configured maximum.
Your max_connections variable seems to be fine.
MEMORY USAGE
Max Memory Ever Allocated : 61 G
Configured Max Per-thread Buffers : 225 G
Configured Max Global Buffers : 2 G
Configured Max Memory Limit : 228 G
Physical Memory : 3.95 G
Max memory limit exceeds 90% of physical memory
KEY BUFFER
Current MyISAM index space = 2 G
Current key_buffer_size = 512 M
Key cache miss rate is 1 : 121
Key buffer fill ratio = 31.00 %
Your key_buffer_size seems to be too high.
Perhaps you can use these resources elsewhere
QUERY CACHE
Query cache is enabled
Current query_cache_size = 64 M
Current query_cache_used = 14 M
Current query_cache_limit = 2 M
Current Query cache Memory fill ratio = 23.29 %
Current query_cache_min_res_unit = 4 K
Query Cache is 23 % fragmented
Run “FLUSH QUERY CACHE” periodically to defragment the query cache memory
If you have many small queries lower ‘query_cache_min_res_unit’ to reduce fragmentation.
Your query_cache_size seems to be too high.
Perhaps you can use these resources elsewhere
MySQL won’t cache query results that are larger than query_cache_limit in size
SORT OPERATIONS
Current sort_buffer_size = 4 M
Current read_rnd_buffer_size = 15 M
Sort buffer seems to be fine
JOINS
Current join_buffer_size = 4.00 M
You have had 22509 queries where a join could not use an index properly
join_buffer_size >= 4 M
This is not advised
You should enable “log-queries-not-using-indexes”
Then look for non indexed joins in the slow query log.
OPEN FILES LIMIT
Current open_files_limit = 8192 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine
TABLE CACHE
Current table_open_cache = 2048 tables
Current table_definition_cache = 128 tables
You have a total of 400 tables
You have 588 open tables.
The table_cache value seems to be fine
You should probably increase your table_definition_cache value.
TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 64 M
Of 53670 temp tables, 44% were created on disk
Effective in-memory tmp_table_size is limited to max_heap_table_size.
Perhaps you should increase your tmp_table_size and/or max_heap_table_size
to reduce the number of disk-based temporary tables
Note! BLOB and TEXT columns are not allow in memory tables.
If you are using these columns raising these values might not impact your
ratio of on disk temp tables.
TABLE SCANS
Current read_buffer_size = 127 M
Current table scan ratio = 407 : 1
read_buffer_size is over 8 MB there is probably no need for such a large read_buffer
TABLE LOCKING
Current Lock Wait ratio = 1 : 3135582
Your table locking seems to be fine

myeclipse 6 hot key content assist

为什么MyEclipse6.0 不出现快速提示,我要把整个单词快打完了才出现,。
如:public class TestMain extends TestCase 如果是以前3.2的extends  直接按“ALT+/”就可以出现这个单词提示,
我现在完全要自己打出来了。这样会浪费很多时间的。
  这是不是那么没有设置好呀。

 

 

eclipse3.3中把内容助手(content assist)的快捷键由 alt + /  改成了ctrl + space,这又刚好跟我们操作系统的切换输入法的快捷键冲突,所以造成内容助手不能使用了,给写代码带来了极大的不便。 而已前eclipse 3.2版本中的 alt + / 快捷键被作为新的单词补全(word competion)功能的快捷键。所以现在想像以前一样使用alt + / 来实现内容助手的功能,就需要更改eclipse3.3快捷键的设置了。具体操作如下:

1. 点击Windows->Preferences->General->Keys .

2. 在列出的快捷键列表中查找到:word competion,把它的快捷键alt + / 改成其它的快捷键(怎么改,先把此快捷键删除,点右边的按钮”remove binding”, 再选中binding文本框,输入你想要的快捷键)。

3. 在列出的快捷键列表中查找到:content assist,把它的快捷键 ctrl + space 改成我们想的的 alt + / 即可了。

从编译到工具:几种Mysql的优化方法

一、在编译时优化mysql


如果你从源代码分发安装mysql,要注意,编译过程对以后的目标程序性能有重要的影响,不同的编译方式可能得到类似的目标文件,但性能可能相差很大,因此,在编译安装mysql适应仔细根据你的应用类型选择最可能好的编译选项。这种定制的mysql可以为你的应用提供最佳性能。 


技巧:选用较好的编译器和较好的编译器选项,这样应用可提高性能10-30%。(mysql文档如是说) 


1.1、使用pgcc(pentium gcc)编译器


该编译器(http://www.goof.com/pcg/)针对运行在奔腾处理器系统上的程序进行优化,用pgcc编译mysql源代码,总体性能可提高10%。当然如果你的服务器不是用奔腾处理器,就不必用它了,因为它是专为奔腾系统设计的。 


1.2、仅使用你想使用的字符集编译mysql


mysql目前提供多达24种不同的字符集,为全球用户以他们自己的语言插入或查看表中的数据。却省情况下,mysql安装所有者这些字符集,热然而,最好的选择是指选择一种你需要的。如,禁止除latin1字符集以外的所有其它字符集: 


—————————————————————————— 


%>./configure -with-extra-charsets=none [–other-configuration-options] 


—————————————————————————— 


1.3、将mysqld编译成静态执行文件


将mysqld编译成静态执行文件而无需共享库也能获得更好的性能。通过在配置时指定下列选项,可静态编译mysqld。 


—————————————————————————— 


%>./configure -with-mysqld-ldflags=-all-static [–other-configuration-options] 


—————————————————————————— 


1.4、配置样本


下列配置命令常用于提高性能: 


—————————————————————————— 


%>cflags=”-o6  -mpentiumpro -fomit-frame-pointer” cxx=gcc cxxflags=”-o6 -mpentiumpro  -fomit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti”  ./configure –prefix=/usr/local –enable-assembler  –with-mysqld-ldflags=-all-static –disable-shared 


—————————————————————————— 


二、调整服务器


确保运用正确的编译固然重要,但这只是成功的第一步,配置众多的mysql变量同样对服务器的正常运行起关键作用。你可以将这些变量的赋值存在一个配置文件中,以确保它们在每次启动mysql时均起作用,这个配置文件就是my.cnf文件。


mysql已经提供了几个my.cnf文件的样本,可在/usr/local/mysqld/share/mysql/目录下找到。这些文件分别命名为my-small.cnf、 my-medium.cnf、my-large.cnf和my-huge.cnf,规模说明可在描述配置文件适用的系统类型标题中找到。如果在只有相当少内存的系统上运行mysql,而且只是偶尔的用一下,那么my-small.cnf会比较理想,因为它命令mysqld 只使用最少的资源。类似地,如果你计划构建电子商务超市,而且系统拥有2g内存,那么你可能要用到mysql-huge.cnf文件了。为了利用这些文件中的一个,你需要复制一个最适合需求的文件,改名为my.cnf。你可以选择使用配置文件三种作用范围的一种:


global:将my.cnf文件复制到服务器的/etc目录下,这使得配置文件中的变量作用于全局,即对所有服务器上的mysql数据库服务器有效。


local:将my.cnf文件复制到[mysql-install-dir]/var/目录下,使得my.cnf作用于特定的服务器。[mysql-install-dir]表示mysql安装目录。


user:你可以再限制作用于特定的用户,将my.cnf复制到用户的根目录下。


究竟如何设置my.cnf中的这些变量呢?更进一步说,你可以设置哪一个变量。虽然所用变量对mysql服务器相对通用,每一个变量与mysql的的某些组件有更特定的关系。如变量max_connects归在mysqld类别下。执行下列命令即可知道: 


——————————————————————


%>/usr/local/mysql/libexec/mysqld –help 


—————————————————————— 


它显示大量的选项及与mysqld相关的变量。你可以很容易地在该行文字之下找出变量: 


——————————————————————- 


possible variables for option –set-variable (-o) are 


——————————————————————- 


然后你可以如下设置my.cnf中的那些变量: 


——————————————————————- 


set-variable = max_connections=100 


——————————————————————- 


它设置mysql服务器的最大并发连接数为100。要确保在my.cnf文件中的[mysqld]标题下插入变量设置。 


三、表类型


很多mysql用户可能很惊讶,mysql确实为用户提供5种不同的表类型,称为dbd、heap、isam、merge和myiasm。dbd归为事务安全类,而其他为非事务安全类。 


3.1、事务安全 


dbd 


berkeley  db(dbd)表是支持事务处理的表,由sleepycat软件公司(http://www.sleepycat.com)开发。它提供mysql用户期待已久的功能-事务控制。事务控制在任何数据库系统中都是一个极有价值的功能,因为它们确保一组命令能成功地执行。 


3.2、非事务安全 


heap 


heap表是mysql中存取数据最快的表。这是因为他们使用存储在动态内存中的一个哈希索引。另一个要点是如果mysql或服务器崩溃,数据将丢失。 


isam 


isam表是早期mysql版本的缺省表类型,直到myiasm开发出来。建议不要再使用它。 


merge 


merge是一个有趣的新类型,在3.23.25之后出现。一个merge表实际上是一个相同myisam表的集合,合并成一个表,主要是为了效率原因。这样可以提高速度、搜索效率、修复效率并节省磁盘空间。 


myiasm 


这是mysql的缺省表类型。它基于iasm代码,但有很多有用的扩展。myiasm比较好的原因: 


myiasm表小于iasm表,所以使用较少资源。 


myiasm表在不同的平台上二进制层可移植。 


更大的键码尺寸,更大的键码上限。 


3.3、指定表类型


你可在创建表时指定表的类型。下例创建一个heap表: 


——————————————————————–


mysql>create table email_addresses type=heap ( 


->email char(55) not null, 


->name char(30) not null, 


->primary key(email) ); 


———————————————————————


bdb表需要一些配置工作,参见http://www.mysql.com/doc/b/d/bdb_overview.HTML。 


3.4、更多的表类型


为了使mysql管理工作更有趣,即将发布的mysql 4.0将提供两种新的表类型,称为innobase和gemeni。 


四、优化工具


mysql服务器本身提供了几条内置命令用于帮助优化。 


4.1、show 


你可能有兴趣知道mysql服务器究竟更了什么,下列命令给出一个总结: 


———————————–


mysql>show status; 


———————————–


它给出了一个相当长的状态变量及其值的列表。有些变量包含了异常终止客户的数量、异常终止连接的数量、连接尝试的次数、最大并发连接数和大量其他有用的信息。这些信息对找出系统问题和低效极具价值。 


show还能做更多的事情。它可以显示关于日志文件、特定数据库、表、索引、进程和权限表中有价值的信息。详见mysql手册。 


4.2、explain 


当你面对select语句时,explain解释select命令如何被处理。这不仅对决定是否应该增加一个索引,而且对决定一个复杂的join如何被mysql处理都是有帮助的。 


4.3、optimize 


optimize语句允许你恢复空间和合并数据文件碎片,对包含变长行的表进行了大量更新和删除后,这样做特别重要。optimize目前只工作于myiasm和bdb表。


日誌分析工具 access.log analysis


Visitors is a very fast web log analyzer for Linux, Windows, and other Unix-like operating systems. It takes as input a web server log file, and outputs statistics in form of different reports. The design principles are very different compared to other software of the same type:


  • No installation required, can process up to 150,000 lines of log entries per second in fast computers (20MB/s with my log files average length).
  • Designed to be executed by the command line, output html and text reports. The text report can be used in pipe to less to check web stats from ssh.
  • Support for real time statistics with the Visitors Stream Mode introduced with version 0.3.
  • To specify the log format is not needed at all. Works out of box with apache and most other web servers with a standard log format (see the documentation for more information on the format).
  • It’s a portable C program, can be compiled on many different systems. Binaries for Windows systems are in the Download section of this page.
  • The produced html report doesn’t contain images or external CSS, is self-contained, you can send it by email to users.
  • Visitors is free software (and of course, freeware), under the terms of the GPL license. You don’t need to pay to use it. Visitors is supported, if you want a custom version made directly by the original author for a modest price, contact me at antirez (at) invece (dot) org. ISPs may take advantage of the high processing speed.

  •  


    download:http://www.hping.org/visitors/visitors-0.7.tar.gz

    Get Working day difference for 2 dates

    CREATE DEFINER=`root`@`%` FUNCTION `getworkdaydiff`(b date, a date) RETURNS int(11)
    DETERMINISTIC
    COMMENT ‘Get Working day difference for 2 dates’
    BEGIN
    DECLARE freedays int;
    SET freedays = 0;


    SET @x = DATEDIFF(b, a);
    IF @x<0 THEN
    SET @m = a;
    SET a = b;
    SET b = @m;
    SET @m = -1;
    ELSE
    SET @m = 1;
    END IF;
    SET @x = abs(@x) + 1;
    /* days in first week */
    SET @w1 = WEEKDAY(a)+1;
    SET @wx1 = 8-@w1;
    IF @w1>5 THEN
    SET @w1 = 0;
    ELSE
    SET @w1 = 6-@w1;
    END IF;
    /* days in last week */
    SET @wx2 = WEEKDAY(b)+1;
    SET @w2 = @wx2;
    IF @w2>5 THEN
    SET @w2 = 5;
    END IF;
    /* summary */
    SET @weeks = (@x-@wx1-@wx2)/7;
    SET @noweekends = (@weeks*5)+@w1+@w2;
    SET @result = @noweekends-freedays;
    RETURN @result*@m;
    END