﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>P.Linux Laboratory &#187; 程序设计</title>
	<atom:link href="http://www.penglixun.com/category/tech/program/feed" rel="self" type="application/rss+xml" />
	<link>http://www.penglixun.com</link>
	<description>MySQL DBA &#38; Linux SA</description>
	<lastBuildDate>Sun, 22 Jan 2012 16:34:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL Multi-Master实现方式</title>
		<link>http://www.penglixun.com/tech/program/how_to_mysql_multi_master.html</link>
		<comments>http://www.penglixun.com/tech/program/how_to_mysql_multi_master.html#comments</comments>
		<pubDate>Fri, 14 Jan 2011 09:35:01 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysqlbinlog]]></category>
		<category><![CDATA[Repliction]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1188</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/how_to_mysql_multi_master.html MySQL Mutil-Master Replication喊了很久... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/how_to_mysql_multi_master.html </p>
<p></span>MySQL Mutil-Master Replication喊了很久了，但是MySQL一直没有去，虽然在MySQL源码中有注释将实现Multi-Master，mi结构体也为Multi-Master做好了准备，但是却一直不见MySQL发布。<br />
但是Multi-Master &#8211;> Slave的Repliction确实非常有用，例如一台集中备份机备份所有Master的数据。</p>
<p>实现Multi-Master有几种思路：<br />
1. 修改MySQL源码：修改sql_yacc.yy, sql_lex.cc支持多Master的CHANGE MASTER TO语法，然后修改slave相关的slave.cc，支持开启多个Slave, 将slave io/ slave thread线程扩展为一个slave_list。<br />
2. 利用mysqlbinlog之类的工具，远程注册到Master获取binlog，导入本地Slave服务器。</p>
<p>从效率看，肯定第一种方式效率高，但是风险太大了，并且MySQL版本更新，可能需要变动自己的代码以适应新的MySQL Source, MySQL官方的实现方式肯定是第一种，从源码中的注释可以看出他们的设计思路。但是他们考虑的问题可能是多个Master复制如何处理冲突等异常，因而迟迟不发布。</p>
<p>为了避免过多的入侵MySQL，我采用第二种方式，用一个脚本或者程序等等，去调用mysqlbinlog，用-R远程请求到&#8211;to-last-log，然后稍微修改一下啊mysqlbinlog的源码，在日志切换后计数一下，在输出文件末尾打上切换日志的个数，例如：</p>

<div class="wp_codebox"><table><tr id="p11885"><td class="code" id="p1188code5"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">insert</span> <span style="color: #993333; font-weight: bold;">into</span> a <span style="color: #993333; font-weight: bold;">values</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">/*!*/</span>;
# at <span style="color: #cc66cc;">1070</span>
#<span style="color: #cc66cc;">110114</span> <span style="color: #cc66cc;">16</span>:<span style="color: #cc66cc;">16</span>:<span style="color: #cc66cc;">11</span> server id <span style="color: #cc66cc;">3</span>  end_log_pos <span style="color: #cc66cc;">1097</span> 	Xid <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">36</span>
COMMIT<span style="color: #808080; font-style: italic;">/*!*/</span>;
DELIMITER ;
# End of log file
ROLLBACK <span style="color: #808080; font-style: italic;">/* added by mysqlbinlog */</span>;
<span style="color: #808080; font-style: italic;">/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/</span>;
<span style="color: #808080; font-style: italic;">-- Rorate binlog count: 1</span></pre></td></tr></table></div>

<p>&#8211; Rorate binlog count: 1就是日志切换信息，表示切换了一次日志（即传入Master的日志号没有用完）然后tail末尾的end_pos来查看本次同步到哪里了，写到*.info的文件中。</p>
<p>我的脚本需要配置一个multi_master.conf文件，配好每个Master的信息，例如：</p>

<div class="wp_codebox"><table><tr id="p11886"><td class="code" id="p1188code6"><pre class="sql" style="font-family:monospace;">#cat multi_master<span style="color: #66cc66;">.</span>conf 
<span style="color: #66cc66;">&#91;</span>master1<span style="color: #66cc66;">&#93;</span>
MASTER_HOST<span style="color: #66cc66;">=</span>1<span style="color: #66cc66;">.</span>2<span style="color: #66cc66;">.</span>3<span style="color: #66cc66;">.</span>4
MASTER_USER<span style="color: #66cc66;">=</span>plx
MASTER_PASSWORD<span style="color: #66cc66;">=</span>plx
MASTER_PORT<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3306</span>
MASTER_LOG_NAME<span style="color: #66cc66;">=</span>mysql<span style="color: #66cc66;">-</span>bin
MASTER_LOG_IDX<span style="color: #66cc66;">=</span>000002
MASTER_LOG_POS<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">521</span>
RELAY_LOG_DIR<span style="color: #66cc66;">=/</span>tmp<span style="color: #66cc66;">/</span>
RELAY_LOG_NAME<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span>relay<span style="color: #66cc66;">-</span>bin
&nbsp;
<span style="color: #66cc66;">&#91;</span>master2<span style="color: #66cc66;">&#93;</span>
MASTER_HOST<span style="color: #66cc66;">=</span>2<span style="color: #66cc66;">.</span>3<span style="color: #66cc66;">.</span>4<span style="color: #66cc66;">.</span>5
MASTER_USER<span style="color: #66cc66;">=</span>plx
MASTER_PASSWORD<span style="color: #66cc66;">=</span>plx
MASTER_PORT<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3306</span>
MASTER_LOG_NAME<span style="color: #66cc66;">=</span>mysql<span style="color: #66cc66;">-</span>bin
MASTER_LOG_IDX<span style="color: #66cc66;">=</span>000002
MASTER_LOG_POS<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">581</span>
RELAY_LOG_DIR<span style="color: #66cc66;">=/</span>tmp<span style="color: #66cc66;">/</span>
RELAY_LOG_NAME<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">-</span>relay<span style="color: #66cc66;">-</span>bin
&nbsp;
<span style="color: #66cc66;">&#91;</span>slave<span style="color: #66cc66;">&#93;</span>
SLAVE_USER<span style="color: #66cc66;">=</span>plx
SLAVE_PASSWORD<span style="color: #66cc66;">=</span>plx</pre></td></tr></table></div>

<p>SLAVE默认导入本地，所以没有提供主机选项。<br />
配置文件的含义是，定义了master1和master2两个Master，名称其实只要不是slave都行，[slave]中定义了本地导入的用户名和密码。<br />
特有的参数我解释下，没解释的跟MySQL一样，<br />
MASTER_LOG_NAME和MASTER_LOG_IDX组成MySQL中的Master_log_file，RELAY_LOG_DIR表示取回的binlog文件放哪个目录，RELAY_LOG_NAME是Relay文件的文件名，会加上标号，跟MySQL一样，这个脚本会自动处理。<br />
一旦执行过一次，就会生成master1.info之类的文件，来表示当前同步到哪里了，例如下面这个例子：</p>

<div class="wp_codebox"><table><tr id="p11887"><td class="code" id="p1188code7"><pre class="sql" style="font-family:monospace;">MASTER_LOG_POS<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1482</span>
NAME<span style="color: #66cc66;">=</span>master1
MASTER_USER<span style="color: #66cc66;">=</span>plx
RELAY_LOG_NAME<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span>relay<span style="color: #66cc66;">-</span>bin
MASTER_LOG_IDX<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span>
MASTER_HOST<span style="color: #66cc66;">=</span>1<span style="color: #66cc66;">.</span>2<span style="color: #66cc66;">.</span>3<span style="color: #66cc66;">.</span>4
MASTER_LOG_NAME<span style="color: #66cc66;">=</span>mysql<span style="color: #66cc66;">-</span>bin
MASTER_PORT<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3306</span>
RELAY_LOG_DIR<span style="color: #66cc66;">=/</span>tmp<span style="color: #66cc66;">/</span>
MASTER_PASSWORD<span style="color: #66cc66;">=</span>plx
RELAY_LOG_IDX<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3</span></pre></td></tr></table></div>

<p>只有找不到*.info的时候，才会使用multi_master.conf。</p>
<p>现在每次调度multi_master_repl.pl都只会运行一次，可以不断的调度multi_master_repl.pl，因为还没有完全搞定KILL信号在<acronym title="Practical Extraction and Report Language">Perl</acronym>脚本的处理，用C重写后会解决，不能暴力kill -9，会导致不知道复制到哪里了。</p>
<p>这是下载地址，切勿用在生产环境，这只是个验证想法的程序。</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>下一步我想用C重新实现，在mysqlbinlog源码基础上修改，获取到的日志直接写入到sock或直接导入远程mysql，避免多写一次文件，也欢迎提供新思路。</p>
<p>这是一次执行的日志：</p>

<div class="wp_codebox"><table><tr id="p11888"><td class="code" id="p1188code8"><pre class="shell" style="font-family:monospace;">#./multi_master_repl.pl 
(DEBUG) Enter: get_config()
	Info: begin
	(DEBUG) get_config --&gt; master1
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_HOST=1.2.3.4
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_USER=plx
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_PASSWORD=plx
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_PORT=3306
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_LOG_NAME=mysql-bin
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_LOG_IDX=000002
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:MASTER_LOG_POS=521
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:RELAY_LOG_DIR=/tmp/
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master1:RELAY_LOG_NAME=1-relay-bin
	(DEBUG) get_config --&gt; Found master1.info, Read it
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_LOG_POS=1097
	(DEBUG) get_config --&gt; master1.info --&gt; master1:NAME=master1
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_USER=plx
	(DEBUG) get_config --&gt; master1.info --&gt; master1:RELAY_LOG_NAME=1-relay-bin
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_LOG_IDX=2
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_HOST=1.2.3.4
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_LOG_NAME=mysql-bin
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_PORT=3306
	(DEBUG) get_config --&gt; master1.info --&gt; master1:RELAY_LOG_DIR=/tmp/
	(DEBUG) get_config --&gt; master1.info --&gt; master1:MASTER_PASSWORD=plx
	(DEBUG) get_config --&gt; master1.info --&gt; master1:RELAY_LOG_IDX=2
	(DEBUG) get_config --&gt; Push[master1] to Master_Info_List
	(DEBUG) get_config --&gt; master2
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_HOST=2.3.4.5
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_USER=plx
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_PASSWORD=plx
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_PORT=3306
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_LOG_NAME=mysql-bin
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_LOG_IDX=000002
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:MASTER_LOG_POS=581
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:RELAY_LOG_DIR=/tmp/
	(DEBUG) get_config --&gt; multi_master.conf --&gt; master2:RELAY_LOG_NAME=2-relay-bin
	(DEBUG) get_config --&gt; Found master2.info, Read it
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_LOG_POS=1541
	(DEBUG) get_config --&gt; master2.info --&gt; master2:NAME=master2
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_USER=plx
	(DEBUG) get_config --&gt; master2.info --&gt; master2:RELAY_LOG_NAME=2-relay-bin
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_LOG_IDX=2
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_HOST=2.3.4.5
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_LOG_NAME=mysql-bin
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_PORT=3306
	(DEBUG) get_config --&gt; master2.info --&gt; master2:RELAY_LOG_DIR=/tmp/
	(DEBUG) get_config --&gt; master2.info --&gt; master2:MASTER_PASSWORD=plx
	(DEBUG) get_config --&gt; master2.info --&gt; master2:RELAY_LOG_IDX=2
	(DEBUG) get_config --&gt; Push[master2] to Master_Info_List
	(DEBUG) get_config --&gt; multi_master.conf --&gt; slave:SLAVE_USER=plx
	(DEBUG) get_config --&gt; multi_master.conf --&gt; slave:SLAVE_PASSWORD=plx
(DEBUG) Enter: get_config()
	Info: exit
(DEBUG) Enter: create_slave_threads()
	Info: begin
	(DEBUG) create_slave_threads --&gt; Creating run_slave Threads...
(DEBUG) Enter: run_slave()
	Info: begin [tid: 1]
	(DEBUG) run_slave(0) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) run_slave --&gt; mysqlbinlog: ./mysqlbinlog -h1.2.3.4 -uplx -pplx -R -t --start-position=1097 mysql-bin.000002 &gt; /tmp/1-relay-bin.000002
Warning: ./mysqlbinlog: unknown variable 'loose_default-character-set=utf8'
	(DEBUG) run_slave(0) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
(DEBUG) Enter: import_to_slave()
	Info: begin [Param: p_master_idx=&gt;0]
	(DEBUG) import_to_slave(0) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) import_to_slave(0) --&gt; Importing Relay Log /tmp/1-relay-bin.000002 To Slave...
	(DEBUG) create_slave_threads --&gt; Created 2 run_slave Threads
(DEBUG) Enter: run_slave()
	Info: begin [tid: 2]
	(DEBUG) run_slave(1) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) run_slave --&gt; mysqlbinlog: ./mysqlbinlog -h2.3.4.5 -uplx -pplx -R -t --start-position=1541 mysql-bin.000002 &gt; /tmp/2-relay-bin.000002
Warning: ./mysqlbinlog: unknown variable 'loose_default-character-set=utf8'
	(DEBUG) run_slave(1) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
(DEBUG) Enter: import_to_slave()
	Info: begin [Param: p_master_idx=&gt;1]
	(DEBUG) import_to_slave(1) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) import_to_slave(1) --&gt; Importing Relay Log /tmp/2-relay-bin.000002 To Slave...
(DEBUG) Enter: update_master_info()
	Info: begin [Param: p_master_idx=&gt;0]
(DEBUG) Enter: update_master_info()
	Info: begin [Param: p_master_idx=&gt;1]
	(DEBUG) update_master_info(0) --&gt; Now Master-Log is mysql-bin.000002 Pos is 1482
(DEBUG) Enter: update_master_info_file()
	Info: begin [Param: p_master_idx=&gt;0]
	(DEBUG) update_master_info_file(0) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_LOG_POS=1482
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; NAME=master1
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_USER=plx
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; RELAY_LOG_NAME=1-relay-bin
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_LOG_IDX=2
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_HOST=1.2.3.4
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_LOG_NAME=mysql-bin
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_PORT=3306
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; RELAY_LOG_DIR=/tmp/
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; MASTER_PASSWORD=plx
	(DEBUG) update_master_info_file(0) --&gt; Writing master1.info --&gt; RELAY_LOG_IDX=3
	(DEBUG) update_master_info_file(0) --&gt; Created master1.info
(DEBUG) Enter: update_master_info_file(0)
	Info: exit
(DEBUG) Enter: update_master_info(0)
	Info: exit
(DEBUG) Enter: import_to_slave(0)
	Info: exit
(DEBUG) Enter: run_slave(0)
	Info: exit
	(DEBUG) update_master_info(1) --&gt; Now Master-Log is mysql-bin.000002 Pos is 2120
(DEBUG) Enter: update_master_info_file()
	Info: begin [Param: p_master_idx=&gt;1]
	(DEBUG) update_master_info_file(1) --&gt; NO KILL SIGNAL --&gt; g_is_killed =&gt;0
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_LOG_POS=2120
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; NAME=master2
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_USER=plx
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; RELAY_LOG_NAME=2-relay-bin
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_LOG_IDX=2
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_HOST=2.3.4.5
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_LOG_NAME=mysql-bin
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_PORT=3306
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; RELAY_LOG_DIR=/tmp/
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; MASTER_PASSWORD=plx
	(DEBUG) update_master_info_file(1) --&gt; Writing master2.info --&gt; RELAY_LOG_IDX=3
	(DEBUG) update_master_info_file(1) --&gt; Created master2.info
(DEBUG) Enter: update_master_info_file(1)
	Info: exit
(DEBUG) Enter: update_master_info(1)
	Info: exit
(DEBUG) Enter: import_to_slave(1)
	Info: exit
(DEBUG) Enter: run_slave(1)
	Info: exit
(DEBUG) Enter: create_slave_threads()
	Info: exit</pre></td></tr></table></div>
<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年09月12日 -- <a href="http://www.penglixun.com/tech/database/backup_mysql_use_mysqlbinlog.html" title="利用mysqlbinlog进行集群备份的设想">利用mysqlbinlog进行集群备份的设想</a> (3)</li><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li><li>2011年12月23日 -- <a href="http://www.penglixun.com/tech/database/server_kill_idle_transaction.html" title="在Server层实现Kill Idle Transaction">在Server层实现Kill Idle Transaction</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/how_to_mysql_multi_master.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>自动生成索引的脚本</title>
		<link>http://www.penglixun.com/tech/program/autocreateindex_script_release.html</link>
		<comments>http://www.penglixun.com/tech/program/autocreateindex_script_release.html#comments</comments>
		<pubDate>Fri, 12 Nov 2010 13:20:30 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[AutoCreateIndex]]></category>
		<category><![CDATA[索引]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1173</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/autocreateindex_script_release.html 写了个自动建索引的脚本，... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/autocreateindex_script_release.html </p>
<p></span>写了个自动建索引的脚本，基本原理是解析<acronym title="Structured Query Language">SQL</acronym>的条件，然后得出每个条件的运算符，最后通过优先级矩阵将可以建索引的列排序求最长的序列，也就是尽可能把更多的列包含进索引。</p>
<p>使用方法：<br />
./AutoCreateIndex.pl -e “你的<acronym title="Structured Query Language">SQL</acronym>”</p>
<p>对于多表连接，务必将列名前跟上表名，否则无法处理，因为脚本并不连接数据库，无法判断哪个列属于那个表。<br />
测试版本，Bug很多，欢迎提供意见。</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>下面是一些例子</p>

<div class="wp_codebox"><table><tr id="p117310"><td class="code" id="p1173code10"><pre class="sql" style="font-family:monospace;">plx@plinux<span style="color: #66cc66;">-</span>Laptop:~<span style="color: #66cc66;">/</span>Dropbox<span style="color: #66cc66;">/</span>MySQLScript$ <span style="color: #66cc66;">./</span>AutoCreateIndex<span style="color: #66cc66;">.</span>pl <span style="color: #66cc66;">-</span>e <span style="color: #ff0000;">&quot;SELECT * FROM foo1 a,foo2 b WHERE 'fdsfsa'=a.col1 AND b.col2 = 'abc' AND a.col1=b.col2 AND a.col3 &gt;1 ORDER BY b.col3,b.col1&quot;</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> foo1 <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> idx <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> foo2 <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> idx <span style="color: #66cc66;">&#40;</span>col2<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">,</span>col1<span style="color: #66cc66;">&#41;</span>;
plx@plinux<span style="color: #66cc66;">-</span>Laptop:~<span style="color: #66cc66;">/</span>Dropbox<span style="color: #66cc66;">/</span>MySQLScript$ <span style="color: #66cc66;">./</span>AutoCreateIndex<span style="color: #66cc66;">.</span>pl <span style="color: #66cc66;">-</span>e <span style="color: #ff0000;">&quot;SELECT * FROM t1 where t1 =1 and t2=2&quot;</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> idx <span style="color: #66cc66;">&#40;</span>t2<span style="color: #66cc66;">,</span>t1<span style="color: #66cc66;">&#41;</span>;
plx@plinux<span style="color: #66cc66;">-</span>Laptop:~<span style="color: #66cc66;">/</span>Dropbox<span style="color: #66cc66;">/</span>MySQLScript$ <span style="color: #66cc66;">./</span>AutoCreateIndex<span style="color: #66cc66;">.</span>pl <span style="color: #66cc66;">-</span>e <span style="color: #ff0000;">&quot;SELECT * FROM t1 where col1=1 and col2=4&quot;</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> idx <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col2<span style="color: #66cc66;">&#41;</span>;
plx@plinux<span style="color: #66cc66;">-</span>Laptop:~<span style="color: #66cc66;">/</span>Dropbox<span style="color: #66cc66;">/</span>MySQLScript$ <span style="color: #66cc66;">./</span>AutoCreateIndex<span style="color: #66cc66;">.</span>pl <span style="color: #66cc66;">-</span>e <span style="color: #ff0000;">&quot;SELECT * FROM t1 where col1&gt;1 and col2=4&quot;</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">INDEX</span> idx <span style="color: #66cc66;">&#40;</span>col2<span style="color: #66cc66;">,</span>col1<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>
<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年06月14日 -- <a href="http://www.penglixun.com/tech/program/b_plus_tree_program.html" title="B+树实现程序">B+树实现程序</a> (2)</li><li>2009年12月17日 -- <a href="http://www.penglixun.com/tech/database/index_dependent_on_business_logic.html" title="索引的建立完全依赖于业务">索引的建立完全依赖于业务</a> (0)</li><li>2009年11月10日 -- <a href="http://www.penglixun.com/tech/database/mysql_index_check_script_preview.html" title="MySQL索引检查脚本预览">MySQL索引检查脚本预览</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/autocreateindex_script_release.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MyRndTest随机测试脚本</title>
		<link>http://www.penglixun.com/tech/program/myrndtest_script.html</link>
		<comments>http://www.penglixun.com/tech/program/myrndtest_script.html#comments</comments>
		<pubDate>Thu, 15 Jul 2010 08:42:42 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MyRndTest]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1148</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/myrndtest_script.html MyRndTest脚本用于生成随机测试SQL并发送... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/myrndtest_script.html </p>
<p></span>MyRndTest脚本用于生成随机测试<acronym title="Structured Query Language">SQL</acronym>并发送到数据库执行。<br />
目前支持生成随机主键查询<acronym title="Structured Query Language">SQL</acronym>，类似：SELECT * FROM table WHERE ID = ?。<br />
支持多线程并发测试，将继续改进支持自定义格式<acronym title="Structured Query Language">SQL</acronym>随机填充条件进行测试。</p>
<p>用法：myrndtest.pl -u 用户名 -p 密码 -h 数据库主机 -d 目标数据库 -t 要测试的表 -c 每线程执行<acronym title="Structured Query Language">SQL</acronym>次数 -n 线程数 -m 测试模式，暂时只支持0，默认0</p>
<p>猛击下载：<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年01月15日 -- <a href="http://www.penglixun.com/tech/database/delete_a_type_index.html" title="从MySQL中删除指定类型索引的脚本">从MySQL中删除指定类型索引的脚本</a> (0)</li><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li><li>2011年12月23日 -- <a href="http://www.penglixun.com/tech/database/server_kill_idle_transaction.html" title="在Server层实现Kill Idle Transaction">在Server层实现Kill Idle Transaction</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/myrndtest_script.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MyInitData脚本发布</title>
		<link>http://www.penglixun.com/tech/program/my_init_data_script.html</link>
		<comments>http://www.penglixun.com/tech/program/my_init_data_script.html#comments</comments>
		<pubDate>Mon, 12 Jul 2010 14:16:25 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MyInitData]]></category>
		<category><![CDATA[MyOM-Tools]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1147</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/my_init_data_script.html MyInitData脚本是用来随机造测试数据... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/my_init_data_script.html </p>
<p></span>MyInitData脚本是用来随机造测试数据的脚本。</p>
<p>基本用法：<br />
mysqlinitdata.pl -u 用户名 -p 密码 -h 主机名 -d 数据库名 -t 表名（不写-t则全库都生成） -c 生成记录条数<br />
具体为：<br />
sub print_usage () {</p>
<p>printf &lt;&lt;<acronym title="End of file"><acronym title="End of file">EOF</acronym></acronym></p>
<p>NAME:<br />
myrndtest</p>
<p>SYNTAX:<br />
myrndtest -h host -u user -p password -d db -t table -c count</p>
<p>FUNCTION:<br />
Run MutilThread Random <acronym title="Structured Query Language">SQL</acronym> Test</p>
<p>PARAMETER:<br />
-h   Hostname 默认localhost<br />
-u   Username 默认root<br />
-p   Password 默认空<br />
-d   Database默认test<br />
-t   Table 默认空<br />
-c   Count 默认1<br />
<acronym title="End of file">EOF</acronym><br />
}</p>
<p>就可以根据表的结构随机的填充数据。<br />
基本规则为：<br />
整型（TINYINT~INT）：填充0～MAX的其中任意值，随机分布<br />
字符串型（VARCHAR/CHAR）：填充长度为0～最大长度之间的任意长度随机字符串，长度和每个字符均随机分布。<br />
DATETIME类型和浮点型暂时不支持。</p>
<p>猛击链接下载：<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年07月15日 -- <a href="http://www.penglixun.com/tech/program/myrndtest_script.html" title="MyRndTest随机测试脚本">MyRndTest随机测试脚本</a> (0)</li><li>2010年01月15日 -- <a href="http://www.penglixun.com/tech/database/delete_a_type_index.html" title="从MySQL中删除指定类型索引的脚本">从MySQL中删除指定类型索引的脚本</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/my_init_data_script.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>李开复——算法的力量</title>
		<link>http://www.penglixun.com/tech/program/algorithm_power.html</link>
		<comments>http://www.penglixun.com/tech/program/algorithm_power.html#comments</comments>
		<pubDate>Sun, 04 Jul 2010 16:15:35 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1142</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/algorithm_power.html 算法的力量（李开复） 算法是计算机科... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/algorithm_power.html </p>
<p></span><a href="http://www.sciencenet.cn/blog/Print.aspx?id=240799">算法的力量（李开复）</a></p>
<p>算法是计算机科学领域最重要的基石之一，但却受到了国内一些程序员的冷落。许多学生看到一些公司在招聘时要求的编程语言五花八门就产生了一种误解，认为学计算机就是学各种编程语言，或者认为，学习最新的语言、技术、标准就是最好的铺路方法。其实大家都被这些公司误导了。编程语言虽然该学，但是学习计算机算法和理论更重要，因为计算机算法和理论更重要，因为计算机语言和开发平台日新月异，但万变不离其宗的是那些算法和理论，例如数据结构、算法、编译原理、计算机体系结构、关系型数据库原理等等。在“开复学生网”上，有位同学生动地把这些基础课程比拟为“内功”，把新的语言、技术、标准比拟为“外功”。整天赶时髦的人最后只懂得招式，没有功力，是不可能成为高手的。</p>
<p>算法与我</p>
<p>当我在1980年转入计算机科学系时，还没有多少人的专业方向是计算机科学。有许多其他系的人嘲笑我们说：“知道为什么只有你们系要加一个‘科学’，而没有‘物理科学系’或‘化学科学系’吗？因为人家是真的科学，不需要画蛇添足，而你们自己心虚，生怕不‘科学’，才这样欲盖弥彰。”其实，这点他们彻底弄错了。真正学懂计算机的人（不只是“编程匠”）都对数学有相当的造诣，既能用科学家的严谨思维来求证，也能用工程师的务实手段来解决问题——而这种思维和手段的最佳演绎就是“算法”。</p>
<p>记得我读博时写的Othello对弈软件获得了世界冠军。当时，得第二名的人认为我是靠侥幸才打赢他，不服气地问我的程序平均每秒能搜索多少步棋，当他发现我的软件在搜索效率上比他快60多倍时，才彻底服输。为什么在同样的机器上，我可以多做60倍的工作呢？这是因为我用了一个最新的算法，能够把一个指数函数转换成四个近似的表，只要用常数时间就可得到近似的答案。在这个例子中，是否用对算法才是能否赢得世界冠军的关键。</p>
<p>还记得1988年贝尔实验室副总裁亲自来访问我的学校，目的就是为了想了解为什么他们的语音识别系统比我开发的慢几十倍，而且，在扩大至大词汇系统后，速度差异更有几百倍之多。他们虽然买了几台超级计算机，勉强让系统跑了起来，但这么贵的计算资源让他们的产品部门很反感，因为“昂贵”的技术是没有应用前景的。在与他们探讨的过程中，我惊讶地发现一个O(n*m)的动态规划(dynamic programming)居然被他们做成了O(n*n*m)。更惊讶的是，他们还为此发表了不少文章，甚至为自己的算法起了一个很特别的名字，并将算法提名到一个科学会议里，希望能得到大奖。当时，贝尔实验室的研究员当然绝顶聪明，但他们全都是学数学、物理或电机出身，从未学过计算机科学或算法，才犯了这么基本的错误。我想那些人以后再也不会嘲笑学计算机科学的人了吧！<br />
<span id="more-1142"></span></p>
<p>网络时代的算法</p>
<p>有人也许会说：“今天计算机这么快，算法还重要吗？”其实永远不会有太快的计算机，因为我们总会想出新的应用。虽然在摩尔定律的作用下，计算机的计算能力每年都在飞快增长，价格也在不断下降。可我们不要忘记，需要处理的信息量更是呈指数级的增长。现在每人每天都会创造出大量数据（照片，视频，语音，文本等等）。日益先进的纪录和存储手段使我们每个人的信息量都在爆炸式的增长。互联网的信息流量和日志容量也在飞快增长。在科学研究方面，随着研究手段的进步，数据量更是达到了前所未有的程度。无论是三维图形、海量数据处理、机器学习、语音识别，都需要极大的计算量。在网络时代，越来越多的挑战需要靠卓越的算法来解决。</p>
<p>再举另一个网络时代的例子。在互联网和手机搜索，如果要找附近的咖啡店，那么搜索引擎该怎么处理这个请求呢？最简单的办法就是把整个城市的咖啡馆都找出来，然后计算出它们的所在位置与你之间的距离，再进行排序，然后返回最近的结果。但该如何计算距离呢？图论里有不少算法可以解决这个问题。</p>
<p>这么做也许是最直观的，但绝对不是最迅速的。如果一个城市只有为数不多的咖啡馆，那么这么做应该没什么问题，反正计算量不大。但如果一个城市里有很多咖啡馆，又有很多用户都需要类似的搜索，那么服务器所承受的压力就大多了。在这种情况下，我们该怎样优化算法呢？</p>
<p>首先，我们可以把整个城市的咖啡馆做一次“预处理”。比如，把一个城市分成若干个“格子(grid)”，然后根据用户所在的位置把他放到某一个格子里，只对格子里的咖啡馆进行距离排序。</p>
<p>问题又来了，如果格子大小一样，那么绝大多数结果都可能出现在市中心的一个格子里，而郊区的格子里只有极少的结果。在这种情况下，我们应该把市中心多分出几个格子。更进一步，格子应该是一个“树结构”，最顶层是一个大格——整个城市，然后逐层下降，格子越来越小，这样有利于用户进行精确搜索——如果在最底层的格子里搜索结果不多，用户可以逐级上升，放大搜索范围。</p>
<p>上述算法对咖啡馆的例子很实用，但是它具有通用性吗？答案是否定的。把咖啡馆抽象一下，它是一个“点”，如果要搜索一个“面”该怎么办呢？比如，用户想去一个水库玩，而一个水库有好几个入口，那么哪一个离用户最近呢？这个时候，上述“树结构”就要改成“r-tree”，因为树中间的每一个节点都是一个范围，一个有边界的范围（参考：http://www.cs.umd.edu/~hjs/rtrees/index.html）。</p>
<p>通过这个小例子，我们看到，应用程序的要求千变万化，很多时候需要把一个复杂的问题分解成若干简单的小问题，然后再选用合适的算法和数据结构。</p>
<p>并行算法：Google的核心优势</p>
<p>上面的例子在Google里就要算是小case了！每天Google的网站要处理十亿个以上的搜索，GMail要储存几千万用户的2G邮箱，Google Earth要让数十万用户同时在整个地球上遨游，并将合适的图片经过互联网提交给每个用户。如果没有好的算法，这些应用都无法成为现实。</p>
<p>在这些的应用中，哪怕是最基本的问题都会给传统的计算带来很大的挑战。例如，每天都有十亿以上的用户访问Google的网站，使用Google的服务，也产生很多很多的日志（Log）。因为Log每份每秒都在飞速增加，我们必须有聪明的办法来进行处理。我曾经在面试中问过关于如何对Log进行一些分析处理的问题，有很多面试者的回答虽然在逻辑上正确，但是实际应用中是几乎不可行的。按照它们的算法，即便用上几万台机器，我们的处理速度都根不上数据产生的速度。</p>
<p>那么Google是如何解决这些问题的？</p>
<p>首先，在网络时代，就算有最好的算法，也要能在并行计算的环境下执行。在Google的数据中心，我们使用的是超大的并行计算机。但传统的并行算法运行时，效率会在增加机器数量后迅速降低，也就是说，十台机器如果有五倍的效果，增加到一千台时也许就只有几十倍的效果。这种事半功倍的代价是没有哪家公司可以负担得起的。而且，在许多并行算法中，只要一个结点犯错误，所有计算都会前功尽弃。</p>
<p>那么Google是如何开发出既有效率又能容错的并行计算的呢？</p>
<p>Google最资深的计算机科学家Jeff Dean认识到，Google所需的绝大部分数据处理都可以归结为一个简单的并行算法：Map and Reduce（http://labs.google.com/papers/mapreduce.html）。这个算法能够在很多种计算中达到相当高的效率，而且是可扩展的（也就是说，一千台机器就算不能达到一千倍的效果，至少也可以达到几百倍的效果）。Map and Reduce的另外一大特色是它可以利用大批廉价的机器组成功能强大的server farm。最后，它的容错性能异常出色，就算一个server farm宕掉一半，整个fram依然能够运行。正是因为这个天才的认识，才有了Map and Reduce算法。借助该算法，Google几乎能无限地增加计算量，与日新月异的互联网应用一同成长。</p>
<p>算法并不局限于计算机和网络</p>
<p>举一个计算机领域外的例子：在高能物理研究方面，很多实验每秒钟都能几个TB的数据量。但因为处理能力和存储能力的不足，科学家不得不把绝大部分未经处理的数据丢弃掉。可大家要知道，新元素的信息很有可能就藏在我们来不及处理的数据里面。同样的，在其他任何领域里，算法可以改变人类的生活。例如人类基因的研究，就可能因为算法而发明新的医疗方式。在国家安全领域，有效的算法可能避免下一个911的发生。在气象方面，算法可以更好地预测未来天灾的发生，以拯救生命。</p>
<p>所以，如果你把计算机的发展放到应用和数据飞速增长的大环境下，你一定会发现；算法的重要性不是在日益减小，而是在日益加强。</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年06月22日 -- <a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_cache_buffer_lock.html" title="数据库算法与数据结构——Cache&#038;Buffer&#038;Lock">数据库算法与数据结构——Cache&#038;Buffer&#038;Lock</a> (1)</li><li>2010年05月10日 -- <a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_about_sort.html" title="数据库算法与数据结构系列——排序相关">数据库算法与数据结构系列——排序相关</a> (0)</li><li>2010年04月10日 -- <a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_about_btree.html" title="数据库算法与数据结构系列——B树相关">数据库算法与数据结构系列——B树相关</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/algorithm_power.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>中文分词算法设计（统计分词算法）</title>
		<link>http://www.penglixun.com/tech/program/chinese_segmentation_algorithm_design_and_implementation.html</link>
		<comments>http://www.penglixun.com/tech/program/chinese_segmentation_algorithm_design_and_implementation.html#comments</comments>
		<pubDate>Fri, 02 Jul 2010 07:46:39 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Chinese word segmentation]]></category>
		<category><![CDATA[Statistical word segmentation]]></category>
		<category><![CDATA[中文分词]]></category>
		<category><![CDATA[统计分词]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1140</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/chinese_segmentation_algorithm_design_and_implementation.html 我的毕... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/chinese_segmentation_algorithm_design_and_implementation.html </p>
<p></span>我的毕业设计，HOHO，偶然发现某公司也是采用的我这种统计分词方法。</p>
<div style="width:477px" id="__ss_4664767"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/plinux/ss-4664767" title="中文分词算法设计">中文分词算法设计</a></strong><object id="__sse4664767" width="477" height="510"><param name="movie" value="http://static.slidesharecdn.com/swf/doc_player.swf?doc=040620520-100702023922-phpapp01&#038;stripped_title=ss-4664767" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse4664767" src="http://static.slidesharecdn.com/swf/doc_player.swf?doc=040620520-100702023922-phpapp01&#038;stripped_title=ss-4664767" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="477" height="510"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">documents</a> from <a href="http://www.slideshare.net/plinux">Lixun Peng</a>.</div>
</div><h2  class="related_post_title">随机显示文章</h2><ul class="related_post"><li>2010年06月14日 -- <a href="http://www.penglixun.com/tech/program/b_plus_tree_program.html" title="B+树实现程序">B+树实现程序</a> (2)</li><li>2009年10月10日 -- <a href="http://www.penglixun.com/study/science/how_we_know_about_world.html" title="关于世界，我们知道多少？">关于世界，我们知道多少？</a> (2)</li><li>2010年01月18日 -- <a href="http://www.penglixun.com/tech/system/linux_compile_php_error.html" title="Linux下编译PHP的几种错误">Linux下编译PHP的几种错误</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/chinese_segmentation_algorithm_design_and_implementation.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MyClusterManager 1.2 beta</title>
		<link>http://www.penglixun.com/tech/program/my_cluster_manager_12beta.html</link>
		<comments>http://www.penglixun.com/tech/program/my_cluster_manager_12beta.html#comments</comments>
		<pubDate>Mon, 21 Jun 2010 10:44:26 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Cluster]]></category>
		<category><![CDATA[Manager]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1136</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/my_cluster_manager_12beta.html 主要功能：将SQL分发到集群的各... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/my_cluster_manager_12beta.html </p>
<p></span>主要功能：将<acronym title="Structured Query Language">SQL</acronym>分发到集群的各个主机中。<br />
所需<acronym title="Practical Extraction and Report Language">Perl</acronym>的模块：strict，DBI，DBD::mysql。<br />
使用方法：perl ./MyClusterManager.pl<br />
所需表结构为db.sql中的<acronym title="Structured Query Language">SQL</acronym>。</p>
<p><a href="http://www.yupoo.com/photos/plinux/75614945/" title="主界面"><img src="http://pic.yupoo.com/plinux/39432982b5db/28owoftl.png" alt="主界面" width="664" height="473" border="0" /></a><br />
选择1进入主机管理，其他类似。</p>
<p><span id="more-1136"></span><br />
<a href="http://www.yupoo.com/photos/plinux/75614946/" title="添加主机"><img src="http://pic.yupoo.com/plinux/88444982b5dc/kimd0gq1.png" alt="添加主机" width="664" height="473" border="0" /></a><br />
根据提示输入指令，例如a是增加主机，目前m和d指令还不支持。</p>
<p><a href="http://www.yupoo.com/photos/plinux/75614947/" title="下级浏览"><img src="http://pic.yupoo.com/plinux/24952982b5dc/1s6h7nr4.png" alt="下级浏览" width="664" height="473" border="0" /></a><br />
<a href="http://www.yupoo.com/photos/plinux/75614948/" title="显示实例列表"><img src="http://pic.yupoo.com/plinux/48730982b5dc/d1djtp4k.png" alt="显示实例列表" width="664" height="473" border="0" /></a><br />
v number指令可以浏览当前层级ID为number的目标下面有多少下一级单位，例如在host使用v指令可以看某个主机下的instance。<br />
按u指令可以回到上一层。</p>
<p><a href="http://www.yupoo.com/photos/plinux/75614951/" title="添加集群"><img src="http://pic.yupoo.com/plinux/01039982b5dd/eacpnxrx.png" alt="添加集群" width="664" height="473" border="0" /></a><br />
集群管理下，按a添加集群，默认是schema模式，只能添加schema为集群的目标，Target填写Schema_ID，用逗号分割。</p>
<p><a href="http://www.yupoo.com/photos/plinux/75614949/" title="选择集群"><img src="http://pic.yupoo.com/plinux/13389982b5dc/05xfhsum.png" alt="选择集群" width="664" height="473" border="0" /></a><br />
集群管理下s指令可以选择一个集群进行操作，以cluster_name为选择条件。</p>
<p><a href="http://www.yupoo.com/photos/plinux/75614950/" title="执行模式"><img src="http://pic.yupoo.com/plinux/05805982b5dd/xx1uo4l2.png" alt="执行模式" width="664" height="473" border="0" /></a><br />
选好集群后，输入指令r进入运行模式，之后输入的<acronym title="Structured Query Language">SQL</acronym>都会被分发到集群的各个目标。</p>
<p><strong><br />
猛击链接下载：</strong><br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>Web版开发也已完成，<acronym title="Pre-Hypertext Processing">PHP</acronym>开发，即将部署测试。</p>
<p>所需的表结构</p>

<div class="wp_codebox"><table><tr id="p113612"><td class="code" id="p1136code12"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`hosts`</span> <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #993333; font-weight: bold;">auto_increment</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    host_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'主机名'</span><span style="color: #66cc66;">,</span>
    host_ip varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'主机IP'</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`hosts`</span> <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span> uk_Hosts_HostName <span style="color: #66cc66;">&#40;</span>host_name<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`instances`</span> <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #993333; font-weight: bold;">auto_increment</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    instance_type enum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mysql'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'oracle'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'pgsql'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'实例类型'</span><span style="color: #66cc66;">,</span>
    instance_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'实例登录名'</span><span style="color: #66cc66;">,</span>
    instance_password varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'实例登录密码'</span><span style="color: #66cc66;">,</span>
    instance_port smallint <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'实例连接端口'</span><span style="color: #66cc66;">,</span>
    host_id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'关联主机ID'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`schemas`</span> <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #993333; font-weight: bold;">auto_increment</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    schema_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'数据库名'</span><span style="color: #66cc66;">,</span>
    instance_id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'关联实例ID'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`clusters`</span> <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #993333; font-weight: bold;">auto_increment</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    cluster_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'集群名'</span><span style="color: #66cc66;">,</span>
    cluster_comment varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> comment <span style="color: #ff0000;">'集群注释'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`cluster_relations`</span> <span style="color: #66cc66;">&#40;</span>
    id  int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #993333; font-weight: bold;">auto_increment</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    cluster_id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span>  comment <span style="color: #ff0000;">'关联集群ID'</span><span style="color: #66cc66;">,</span>
    target_id int <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span>  comment <span style="color: #ff0000;">'关联目标ID'</span><span style="color: #66cc66;">,</span>
    target_type enum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'instance'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'schema'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">not</span> <span style="color: #993333; font-weight: bold;">null</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #ff0000;">'schema'</span> comment <span style="color: #ff0000;">'目标类型'</span><span style="color: #66cc66;">,</span>
    <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cluster_id<span style="color: #66cc66;">,</span> target_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
    id INT <span style="color: #993333; font-weight: bold;">unsigned</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> comment <span style="color: #ff0000;">'主键'</span><span style="color: #66cc66;">,</span>
    user_name VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> comment <span style="color: #ff0000;">'用户名'</span><span style="color: #66cc66;">,</span>
    user_password VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> comment <span style="color: #ff0000;">'用户密码'</span>
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>
<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li><li>2011年12月23日 -- <a href="http://www.penglixun.com/tech/database/server_kill_idle_transaction.html" title="在Server层实现Kill Idle Transaction">在Server层实现Kill Idle Transaction</a> (1)</li><li>2011年11月29日 -- <a href="http://www.penglixun.com/tech/database/how_to_kill_idle_trx.html" title="如何杀掉空闲事务">如何杀掉空闲事务</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/my_cluster_manager_12beta.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>B+树实现程序</title>
		<link>http://www.penglixun.com/tech/program/b_plus_tree_program.html</link>
		<comments>http://www.penglixun.com/tech/program/b_plus_tree_program.html#comments</comments>
		<pubDate>Mon, 14 Jun 2010 10:30:38 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[BTree]]></category>
		<category><![CDATA[B树]]></category>
		<category><![CDATA[索引]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1131</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/b_plus_tree_program.html 在我的另一篇文章数据库算法与数据... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/b_plus_tree_program.html </p>
<p></span>在我的另一篇文章<a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_about_btree.html">数据库算法与数据结构系列——B树相关</a>中我介绍了B树的结构，PPT上的东西很理论，这里补充一下演示的程序和B+树实现的代码。</p>
<p>下面是从网上找的一个B-树的图形演示程序。<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>下面是用C++实现B+树的代码，没做什么优化，全部都是操作文件，所以相对较慢。<br />
<span id="more-1131"></span></p>

<div class="wp_codebox"><table><tr id="p113114"><td class="code" id="p1131code14"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
操作说明
1.插入操作，&quot;i 关键字&quot; -- 若存在，则提示；若不存在，则插入关键字。 
2.删除操作，&quot;d 关键字&quot;。
3.输出B+树操作，&quot;p 1或2&quot; -- 两种输出模式，模式1输出整个树结构，模式2按从小到大顺序输出所有关键字。
4.测试，&quot;t 1或2&quot; -- 1测试插入，2测试查找
*/</span>
&nbsp;
<span style="color: #339900;">#include &lt;cstdlib&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;fstream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;sys/time.h&gt;</span>
<span style="color: #339900;">#include &lt;ctime&gt;</span>
<span style="color: #339900;">#include &lt;limits.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*计时器*/</span>
<span style="color: #0000ff;">class</span> Timer <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span>
    <span style="color: #666666;">//构造函数</span>
    Timer <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//析构函数</span>
    ~Timer <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//开始计时</span>
    <span style="color: #0000ff;">void</span> begin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//计时结束</span>
    <span style="color: #0000ff;">void</span> end<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//获取时间,ms</span>
    <span style="color: #0000ff;">double</span> get_time<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span> <span style="color: #008080;">:</span>
    <span style="color: #0000ff;">clock_t</span> start, finish<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> <span style="color: #0000dd;">time</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
Timer<span style="color: #008080;">::</span><span style="color: #007788;">Timer</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    start <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    finish <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Timer<span style="color: #008080;">::</span>~Timer <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    start <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    finish <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Timer<span style="color: #008080;">::</span><span style="color: #007788;">begin</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    start <span style="color: #000080;">=</span> <span style="color: #0000dd;">clock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Timer<span style="color: #008080;">::</span><span style="color: #007788;">end</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    finish <span style="color: #000080;">=</span> <span style="color: #0000dd;">clock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">double</span> Timer<span style="color: #008080;">::</span><span style="color: #007788;">get_time</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">time</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>finish<span style="color: #000040;">-</span>start<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000ff;">CLOCKS_PER_SEC</span><span style="color: #000040;">*</span><span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">time</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// 定义B+树的阶数</span>
<span style="color: #339900;">#define M 4</span>
<span style="color: #666666;">// B+树结点定义</span>
<span style="color: #0000ff;">struct</span> BPlusNode
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span>  amount<span style="color: #008080;">;</span>      <span style="color: #666666;">// 该结点中已保存的键值个数</span>
    <span style="color: #0000ff;">long</span> key<span style="color: #008000;">&#91;</span>M<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>      <span style="color: #666666;">// 键值数组</span>
    <span style="color: #0000ff;">long</span> children<span style="color: #008000;">&#91;</span>M<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 子结点位置数组</span>
    <span style="color: #0000ff;">long</span> father<span style="color: #008080;">;</span>      <span style="color: #666666;">// 父结点位置</span>
    <span style="color: #0000ff;">long</span> left<span style="color: #008080;">;</span>        <span style="color: #666666;">// 同一层中的左结点位置</span>
    <span style="color: #0000ff;">long</span> right<span style="color: #008080;">;</span>       <span style="color: #666666;">// 同一层中的右结点位置</span>
    <span style="color: #0000ff;">bool</span> isactive<span style="color: #008080;">;</span>    <span style="color: #666666;">// 有效位</span>
    <span style="color: #0000ff;">bool</span> isleave<span style="color: #008080;">;</span>     <span style="color: #666666;">// 标志该结点是否叶结点</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// 函数头定义</span>
<span style="color: #0000ff;">bool</span> findkey   <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> <span style="color: #000040;">&amp;</span>position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 查找包含给定键值的结点位置</span>
<span style="color: #0000ff;">bool</span> insertkey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// 插入一个新键值</span>
<span style="color: #0000ff;">bool</span> deletekey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// 删除给定的键值</span>
<span style="color: #0000ff;">void</span> printkey  <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> mode<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                <span style="color: #666666;">// 按照给定模式输出B+树</span>
<span style="color: #0000ff;">void</span> testBtree <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> type<span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span>    <span style="color: #666666;">//测试B树</span>
<span style="color: #666666;">// 全局变量</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span> pre<span style="color: #008080;">;</span>             <span style="color: #666666;">// 上一个被访问的结点</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span> cur<span style="color: #008080;">;</span>             <span style="color: #666666;">// 当前指向的结点</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span> root<span style="color: #008080;">;</span>            <span style="color: #666666;">// 根结点位置</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span> smallest<span style="color: #008080;">;</span>        <span style="color: #666666;">// 保存最小关键值的叶结点</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span> nodenewposition<span style="color: #008080;">;</span> <span style="color: #666666;">// 新插入的结点位置</span>
&nbsp;
<span style="color: #666666;">// 主函数</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Timer     timer<span style="color: #008080;">;</span>
    BPlusNode node<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span>    command<span style="color: #008080;">;</span>
    fstream     iofile<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> keynumber<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> position <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// 检测索引文件是否存在，若不存在则创建一个初始化的索引文件，其中包含根结点位置，最小键值位置和一个空结点</span>
    fstream infile <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">in</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>infile<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>    
        node.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><span style="color: #666666;">//如果不存在，则创建根结点，且阶数M=4</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
            node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        node.<span style="color: #007788;">father</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">left</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">isactive</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">isleave</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        root <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span><span style="color: #666666;">//root为什么＝8？</span>
        smallest <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
        fstream outfile <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">out</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #666666;">//输出文件BPlusTreeData.dat</span>
        outfile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #666666;">//指针移到begin处，文件开头</span>
        outfile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #666666;">//输出根结点</span>
        outfile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>smallest, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #666666;">//输出最小关键字</span>
        outfile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #666666;">//输出头指针？</span>
        outfile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    infile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// 循环获取命令行，执行给定的命令</span>
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> command <span style="color: #000080;">&gt;&gt;</span> keynumber<span style="color: #008080;">;</span>
        <span style="color: #666666;">// 插入一个新的键值，该值不能与已有关键值重复</span>
        <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'i'</span> <span style="color: #008080;">:</span>
            timer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>findkey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> keynumber <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is already in this B+ tree&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>insertkey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                timer.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Successful inserted in &quot;</span> <span style="color: #000080;">&lt;&lt;</span> timer.<span style="color: #007788;">get_time</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; ms&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> 
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Action falled&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'d'</span> <span style="color: #008080;">:</span>  <span style="color: #666666;">// 删除给定的键值</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>deletekey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
               <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Successful deleted&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'p'</span> <span style="color: #008080;">:</span>
            <span style="color: #666666;">// 按照指定模式输出B+数</span>
            <span style="color: #666666;">// 模式“1”输出整棵树结构</span>
            <span style="color: #666666;">// 模式“2”按照从小到大的顺序输出所有键值</span>
            timer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            printkey<span style="color: #008000;">&#40;</span>keynumber<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            timer.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Printed in &quot;</span> <span style="color: #000080;">&lt;&lt;</span> timer.<span style="color: #007788;">get_time</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; ms&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'t'</span><span style="color: #008080;">:</span>
            timer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            testBtree<span style="color: #008000;">&#40;</span>keynumber<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            timer.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Test in &quot;</span> <span style="color: #000080;">&lt;&lt;</span> timer.<span style="color: #007788;">get_time</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; ms&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'e'</span><span style="color: #008080;">:</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">default</span> <span style="color: #008080;">:</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Please make sure the command is correct&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// 查找包含给定键值的结点位置，若找到则返回“true”</span>
<span style="color: #666666;">// “position”保存最后访问的结点位置</span>
<span style="color: #0000ff;">bool</span> findkey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> <span style="color: #000040;">&amp;</span>position<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    BPlusNode node<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> point<span style="color: #008080;">;</span>
    fstream iofile <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">in</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">out</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>root, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        cur <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>node.<span style="color: #007788;">isactive</span><span style="color: #008000;">&#41;</span> 
            <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// B+树只在叶结点保存记录信息</span>
        <span style="color: #666666;">// 所以查找必须执行到叶结点</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>node.<span style="color: #007788;">isleave</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 如果该结点不是叶结点，则根据键值决定访问哪个子结点</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                point <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    point <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>point <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> 
                point <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>node.<span style="color: #007788;">amount</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>point, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            pre <span style="color: #000080;">=</span> cur<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 如果该结点是叶结点，则顺序访问结点中的键值</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    position <span style="color: #000080;">=</span> cur<span style="color: #008080;">;</span>
                    iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            position <span style="color: #000080;">=</span> cur<span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// 按照给定格式输出B+数</span>
<span style="color: #0000ff;">void</span> printkey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> mode<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    BPlusNode node<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>, k <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    fstream iofile<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">in</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">out</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// 从根结点开始广度遍历，输出整棵B+树结构</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mode <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>root, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
            cur <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;level &quot;</span> <span style="color: #000080;">&lt;&lt;</span> k <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
                iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;     node &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>j<span style="color: #008000;">&#41;</span>
                    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
                    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                    <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
                iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #000040;">++</span>i<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>cur, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #000040;">++</span>k<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mode <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 从包含最小键值的叶结点开始按照从小到大的顺序输出所有键值</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>smallest, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>smallest, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
            iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> l <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> l <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>l<span style="color: #008000;">&#41;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>l<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// 在位于“position”的结点中插入一个新键值“key”</span>
<span style="color: #666666;">// 按照B+树的规则，根据情况分裂结点 </span>
<span style="color: #0000ff;">bool</span> insertkey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> position<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    fstream iofile<span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">in</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">out</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    BPlusNode node<span style="color: #008080;">;</span>
    BPlusNode nodenew<span style="color: #008080;">;</span>
    BPlusNode nodetemp1, nodetemp2<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> keytemp<span style="color: #008000;">&#91;</span>M<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> childrentemp<span style="color: #008000;">&#91;</span>M<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> posEnd <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">//根节点分裂之后新建根节点</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>position <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>root, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> root<span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">father</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">left</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">isactive</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        nodenew.<span style="color: #007788;">isleave</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">8</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
            cur <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodetemp2, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>nodetemp2.<span style="color: #007788;">isactive</span> <span style="color: #000040;">&amp;&amp;</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> posEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>nodetemp2.<span style="color: #007788;">isactive</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            nodenewposition <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodenew, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>cur, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            nodenewposition <span style="color: #000080;">=</span> cur<span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodenew, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        root <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodetemp1, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            nodetemp1.<span style="color: #007788;">father</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodetemp1, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 没有分裂到根结点</span>
        <span style="color: #0000ff;">int</span> insertposition <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>position, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">amount</span> <span style="color: #000080;">&lt;</span> M<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 结点中还有空位保存新插入的键值，不需要分裂</span>
            <span style="color: #666666;">// 按照从小到大的顺序重新排列原有键值和新插入的键值</span>
            <span style="color: #0000ff;">bool</span> issort1 <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> node.<span style="color: #007788;">amount</span> <span style="color: #000040;">-</span> i, k <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>j, <span style="color: #000040;">++</span>k<span style="color: #008000;">&#41;</span>
                        node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>node.<span style="color: #007788;">amount</span><span style="color: #000040;">-</span>k<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>node.<span style="color: #007788;">amount</span><span style="color: #000040;">-</span>k<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
                    insertposition <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
                    issort1 <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                    <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>issort1<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>node.<span style="color: #007788;">amount</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
                insertposition <span style="color: #000080;">=</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            node.<span style="color: #007788;">amount</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>node.<span style="color: #007788;">isleave</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> p <span style="color: #000080;">=</span> node.<span style="color: #007788;">amount</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> p <span style="color: #000080;">&gt;</span> insertposition<span style="color: #008080;">;</span> <span style="color: #000040;">--</span>p<span style="color: #008000;">&#41;</span>
                    node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>p<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>p<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>insertposition<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>position, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 结点中没有空位保存新插入的键值，必须分裂成两个结点</span>
            <span style="color: #0000ff;">long</span> nextinsertkey <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
            <span style="color: #666666;">// 按照从小到大的顺序重新排列原有键值和新插入的键值</span>
            <span style="color: #666666;">// 并且按照键值的顺序重新排列保存子结点位置的数组</span>
            <span style="color: #0000ff;">bool</span> issort2 <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> m <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>, n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, o <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> m <span style="color: #000080;">&lt;</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>m, <span style="color: #000040;">++</span>o<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>m<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> key <span style="color: #000040;">||</span> issort2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    keytemp<span style="color: #008000;">&#91;</span>m<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #000040;">+</span>n<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>m<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    childrentemp<span style="color: #008000;">&#91;</span>o<span style="color: #000040;">+</span>n<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>o<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                    keytemp<span style="color: #008000;">&#91;</span>m<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
                    childrentemp<span style="color: #008000;">&#91;</span>o<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>o<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    childrentemp<span style="color: #008000;">&#91;</span>o<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
                    n <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
                    <span style="color: #000040;">--</span>m<span style="color: #008080;">;</span>
                    issort2 <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>issort2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                keytemp<span style="color: #008000;">&#91;</span>M<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
                childrentemp<span style="color: #008000;">&#91;</span>M<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>M<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                childrentemp<span style="color: #008000;">&#91;</span>M<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            node.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>node.<span style="color: #007788;">isleave</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 按照内部结点的方式创建新结点</span>
                nodenew.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, j <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> keytemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                        node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> childrentemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">==</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        nextinsertkey <span style="color: #000080;">=</span> keytemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                        node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> childrentemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> M<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        nodenew.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> keytemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                        nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>j<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> childrentemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                        node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
                        <span style="color: #000040;">++</span>j<span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">==</span> M<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
                        nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>j<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> childrentemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
                nodenew.<span style="color: #007788;">isleave</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// 按照叶结点的方式创建新结点</span>
                nodenew.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, j <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> M<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> keytemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                        nodenew.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> keytemp<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> M<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
                            node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
                        <span style="color: #000040;">++</span>j<span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
                nextinsertkey <span style="color: #000080;">=</span> nodenew.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                nodenew.<span style="color: #007788;">isleave</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> n <span style="color: #000080;">&lt;</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>n<span style="color: #008000;">&#41;</span>
                    nodenew.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            nodenew.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
            nodenew.<span style="color: #007788;">father</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">father</span><span style="color: #008080;">;</span>
            nodenew.<span style="color: #007788;">left</span> <span style="color: #000080;">=</span> position<span style="color: #008080;">;</span>
            nodenew.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">right</span><span style="color: #008080;">;</span>
            nodenew.<span style="color: #007788;">isactive</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
            <span style="color: #666666;">// 查找新结点的插入位置</span>
            <span style="color: #666666;">// 若索引文件中存在一个曾经被删除的结点，则用新结点覆盖掉这个结点</span>
            <span style="color: #666666;">// 若不存在这样的结点，则将新结点添加到索引文件尾部</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">8</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">do</span><span style="color: #008000;">&#123;</span>
                cur <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodetemp2, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>nodetemp2.<span style="color: #007788;">isactive</span> <span style="color: #000040;">&amp;&amp;</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> posEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>nodetemp2.<span style="color: #007788;">isactive</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                nodenewposition <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodenew, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>cur, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                nodenewposition <span style="color: #000080;">=</span> cur<span style="color: #008080;">;</span>
                iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>nodenew, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            node.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> nodenewposition<span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>position, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>insertkey<span style="color: #008000;">&#40;</span>nextinsertkey, nodenew.<span style="color: #007788;">father</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>  <span style="color: #666666;">// 递归调用插入算法将分裂后需要插入到父结点的键值插入到父结点中</span>
                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">else</span> 
                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// 删除给定的键值</span>
<span style="color: #666666;">// 该算法不符合B+树的删除规则</span>
<span style="color: #666666;">// 只是简单地将除被删除键值外的其它键值重新插入一遍</span>
<span style="color: #0000ff;">bool</span> deletekey <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> key, <span style="color: #0000ff;">long</span> position<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    fstream iofile<span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;BPlusTreeData.dat&quot;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">binary</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">in</span><span style="color: #000040;">|</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">out</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    BPlusNode node<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> <span style="color: #000040;">*</span>keynumbertemp <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">long</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> number <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> posEnd<span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    posEnd <span style="color: #000080;">=</span> iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>smallest, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>smallest, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> node.<span style="color: #007788;">amount</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            keynumbertemp<span style="color: #008000;">&#91;</span>number<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
            <span style="color: #000040;">++</span>number<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #000040;">--</span>number<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span>node.<span style="color: #007788;">right</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    node.<span style="color: #007788;">amount</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> M<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>j<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        node.<span style="color: #007788;">key</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">children</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    node.<span style="color: #007788;">father</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    node.<span style="color: #007788;">left</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    node.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    node.<span style="color: #007788;">isactive</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    node.<span style="color: #007788;">isleave</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    root <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
    smallest <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>root, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>smallest, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #008080;">;</span>iofile.<span style="color: #007788;">tellp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> posEnd<span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
        iofile.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        node.<span style="color: #007788;">isactive</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">seekp</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>, ios<span style="color: #008080;">::</span><span style="color: #007788;">cur</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        iofile.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>node, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BPlusNode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    iofile.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> k <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> k <span style="color: #000080;">&lt;=</span> number<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>k<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>keynumbertemp<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> key<span style="color: #008000;">&#41;</span>
            <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
        findkey<span style="color: #008000;">&#40;</span>keynumbertemp<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span>, position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        insertkey<span style="color: #008000;">&#40;</span>keynumbertemp<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span>, position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// 测试模块</span>
<span style="color: #0000ff;">void</span> testBtree <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> type<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> keynumber, position<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>type<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">5000</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">%</span> <span style="color: #0000dd;">1000</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Insert &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; Nodes&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            keynumber <span style="color: #000080;">=</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000ff;">LONG_MAX</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span> findkey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                insertkey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">10000</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">%</span> <span style="color: #0000dd;">1000</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Select &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; Nodes&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            keynumber <span style="color: #000080;">=</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000ff;">LONG_MAX</span><span style="color: #008080;">;</span>
            findkey<span style="color: #008000;">&#40;</span>keynumber, position<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>我的测试结果：<br />
plx@plinux-Laptop:~/Dropbox/Code$ ./btree<br />
t 1<br />
Insert 0 Nodes<br />
Insert 1000 Nodes<br />
Insert 2000 Nodes<br />
Insert 3000 Nodes<br />
Insert 4000 Nodes<br />
Test in 40290 ms<br />
t 2<br />
Select 1000 Nodes<br />
Select 2000 Nodes<br />
Select 3000 Nodes<br />
Select 4000 Nodes<br />
Select 5000 Nodes<br />
Select 6000 Nodes<br />
Select 7000 Nodes<br />
Select 8000 Nodes<br />
Select 9000 Nodes<br />
Select 10000 Nodes<br />
Test in 540 ms</p>
<p>查找速度还是非常快的，如果全部载入内存操作，估计1/100的时间</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年04月10日 -- <a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_about_btree.html" title="数据库算法与数据结构系列——B树相关">数据库算法与数据结构系列——B树相关</a> (6)</li><li>2010年11月12日 -- <a href="http://www.penglixun.com/tech/program/autocreateindex_script_release.html" title="自动生成索引的脚本">自动生成索引的脚本</a> (2)</li><li>2009年12月17日 -- <a href="http://www.penglixun.com/tech/database/index_dependent_on_business_logic.html" title="索引的建立完全依赖于业务">索引的建立完全依赖于业务</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/b_plus_tree_program.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>mystat 更新1.1.1</title>
		<link>http://www.penglixun.com/tech/program/mystat_update_20100514.html</link>
		<comments>http://www.penglixun.com/tech/program/mystat_update_20100514.html#comments</comments>
		<pubDate>Fri, 14 May 2010 05:45:22 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[mystat]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1109</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mystat_update_20100514.html 修复了一个重大Bug，强烈建议更新... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mystat_update_20100514.html </p>
<p></span>修复了一个重大Bug，强烈建议更新。</p>
<p>原来有的用户报告说mysql客户端可以连上，但是mystat连不上，报没权限。<br />
经过不断的定位，发现是个很扯淡和低级的错误：<br />
为了统一处理参数，我将所有输入的参数小写处理了，结果，password也被小写了！所以密码中有大写的都连不上！<br />
非常非常低级的错误。</p>
<p>目前支持的操作系统:<br />
Windows/Linux/Unix<br />
要求:<br />
<acronym title="Practical Extraction and Report Language">Perl</acronym> 5以上，DBD::mysql、Switch、strict、Getopt::Std、Term::ANSIColor模块。</p>
Note: There is a file embedded within this post, please visit this post to download the file.<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年03月6日 -- <a href="http://www.penglixun.com/tech/program/mystat_win_support.html" title="mystat脚本更新Windows支持">mystat脚本更新Windows支持</a> (0)</li><li>2010年02月5日 -- <a href="http://www.penglixun.com/tech/program/mystat_release.html" title="mystat 1.0 release 发布">mystat 1.0 release 发布</a> (0)</li><li>2010年01月28日 -- <a href="http://www.penglixun.com/tech/program/mystat_usage.html" title="mystat 0.5 beta 的使用介绍">mystat 0.5 beta 的使用介绍</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mystat_update_20100514.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[转]程序员能力矩阵</title>
		<link>http://www.penglixun.com/tech/program/programmer_competency_matrix.html</link>
		<comments>http://www.penglixun.com/tech/program/programmer_competency_matrix.html#comments</comments>
		<pubDate>Sat, 13 Mar 2010 15:58:54 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[能力矩阵]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1053</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/programmer_competency_matrix.html 看到一个不错的内容：《[译... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/programmer_competency_matrix.html </p>
<p></span>看到一个不错的内容：《<a href="http://goo.gl/hKII">[译文]程序员能力矩阵 Programmer Competency Matrix</a>》，自己对照着圈了圈自己的Level。</p>
<table>
<tbody>
<tr>
<th colspan="5">计算机科学 Computer Science</th>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>数据结构</td>
<td>不知道数组和链表的差异</td>
<td>能够解释和使用数组，链表，字典等，并且能够用于实际的编程任务。</td>
<td>了解基本数据结构时间和空间的折中，比如数组vs 链表，能够解释如何实现哈希表和处理冲突，了解优先队列及其实现。</td>
<td><span style="color: #ff0000;">高等的数据结构的知识，比如B-树、二项堆、斐波那契堆、AVL树、红黑树、伸展树、跳跃表以及前缀树等。</span></td>
<td></td>
</tr>
<tr>
<td>算法</td>
<td>不能够找出一个数组各数的平均值(这令人难以置信，但是我的确在应聘者中遇到过)</td>
<td>基本的排序，搜索和数据的遍历和检索算法。</td>
<td>树，图，简单的贪婪算法和分而治之算法，能够适度了解矩阵该层的含义。</td>
<td><span style="color: #ff0000;">能够辨识和编写动态规划方案，良好的图算法知识，良好的数值估算的知识，能够辨别NP问题等。</span></td>
<td></td>
</tr>
<tr>
<td>编程体系</td>
<td>不知道何为编译器、链接器和解释器。</td>
<td>对编译器、链接器、解释器有基本的了解。知道什么是汇编代码以及在硬件层如何工作。有一些虚拟内存和分页知识。</td>
<td><span style="color: #ff0000;">了解内核模式vs用户模式,多线程，同步原语以及它们如何实现，能够阅读汇编代码。了解网络如何工作，了解网络协议和socket级别编程。</span></td>
<td>了解整个程序堆栈、硬件(CPU+内存+中断+微码)、二进制代码、汇编、静态和动态链接、编码、解释、JIT（just-in-time）编译、内存碎片回收、堆、栈、存储器编址…</td>
<td></td>
</tr>
<tr>
<th colspan="5">软件工程 Software Engineering</th>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>源码版本控制</td>
<td>通过日期备份文件夹</td>
<td>VSS和初级的<acronym title="Concurrent Versions System">CVS</acronym>/<acronym title="Subversion">SVN</acronym>用户</td>
<td><span style="color: #ff0000;">熟练地使用<acronym title="Concurrent Versions System">CVS</acronym>和<acronym title="Subversion">SVN</acronym>特性。知道如何分支和归并，使用程序库补丁安装特性等</span></td>
<td>有分布式VCS系统的知识。尝试过Bzr/Mercurial/Darcs/Git</td>
<td></td>
</tr>
<tr>
<td>自动化编译</td>
<td>只知道在IDE下编译</td>
<td>知道如何编译在命令行下编译系统</td>
<td><span style="color: #ff0000;">能够安装一个脚本构建基本的系统</span></td>
<td>能够安装一个脚本来构建系统并且归档，安装程序，生成发布记录和给源码控制中的代码分配标签。</td>
<td></td>
</tr>
<tr>
<td>自动化测试</td>
<td>认为所有的测试都是测试员的工作。</td>
<td><span style="color: #ff0000;">能够编写自动化的单元测试，能够为正在编写的代码提出良好的测试用例。</span></td>
<td>按照TDD （Test Driven Development）方式编写代码。</td>
<td>了解并且能够有效自动化安装，载入/性能和UI测试</td>
<td></td>
</tr>
<tr>
<th colspan="5">程序设计 Programming</th>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>问题分解</td>
<td>只有直线式的代码，通过复制粘贴来复用</td>
<td>能够把问题分散到多个函数中</td>
<td>能够想出可复用的函数/对象来解决大题的问题</td>
<td><span style="color: #ff0000;">使用适宜的数据结构和算法，写出通用的/面向对象的代码来封装问题的易改变的层面。</span></td>
<td></td>
</tr>
<tr>
<td>系统分解</td>
<td>N想不出比单一的文件/类更好的层面</td>
<td>如果不在同一平台或没采用相同的技术，能够把问题空间和设计方案分解。</td>
<td><span style="color: #ff0000;">能够设计跨技术/平台的系统。</span></td>
<td>能够在多个产品线和与外部体系一体化中虚拟化和设计复制的系统。同时也能够设计支持系统监视、报告、故障恢复等。</td>
<td></td>
</tr>
<tr>
<td>交流</td>
<td>不能向同伴表达想法/主意。匮乏拼写和语法的能力。</td>
<td><span style="color: #ff0000;">同伴能了解你在说什么。有良好的拼写和语法能力。</span></td>
<td>能够和同伴进行高效的交流</td>
<td>能够使用清晰的方式了解和交流想法/设计/主意/细则，能适应每种环境的交流</td>
<td></td>
</tr>
<tr>
<td>同一文件中代码组织</td>
<td>同一文件中组织没有依据</td>
<td>按照逻辑性或者易接近的方法</td>
<td>代码分块和对于其他源文件来说是易于是释,引用其他源文件时有良好的注释</td>
<td><span style="color: #ff0000;">文档头部有许可声明，总结，良好的注释，一致的空格缩进。文档外观美观。</span></td>
<td></td>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>跨文件代码组织</td>
<td>没够想过给代码跨文件组织</td>
<td>相关文件按文件夹分组</td>
<td><span style="color: #ff0000;">每个物理文件都有独立的目的，比如一个类的定义，一个特性的实现等。</span></td>
<td>代码在物理层组织紧密，在文件名上与设计和外观相匹配，可以通过文件分布方式洞察设计理念。</td>
<td></td>
</tr>
<tr>
<td>源码树组织</td>
<td>一切都放在一个文件夹内</td>
<td>初步地将代码分散进对应逻辑的文件夹。</td>
<td><span style="color: #ff0000;">没有循环依赖，二进制文件，库，文档，构建，第三方的代码都组织进合适的文件夹内。</span></td>
<td>源码树的物理布局与逻辑层次、组织方式相匹配。可以通过目录名称和组织方式洞察设计理念。</td>
<td></td>
</tr>
<tr>
<td>代码可读性</td>
<td>单音节的名称 （在国内应该是那些类似用汉语拼音命名的习惯）</td>
<td>对文件、变量、类、方法等，有良好的命名。</td>
<td><span style="color: #ff0000;">没有长函数、注释解释不常规的代码，bug修复,代码假设。</span></td>
<td>代码假设验证使用断言，自然的代码流，没有深层嵌套的条件和方法</td>
<td></td>
</tr>
<tr>
<td>防御性编码</td>
<td><span style="color: #ff0000;">不知道这个概念</span></td>
<td>检查代码中所有的参数，对关键的假设进行断言</td>
<td>确保检查了返回值和使代码失败的异常。</td>
<td>有自己的库来帮助防御性编程、编写单元测试模拟故障</td>
<td></td>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>错误处理</td>
<td>只给乐观的情形编码</td>
<td><span style="color: #ff0000;">基本的代码错误处理，抛出异常/生成错误</span></td>
<td>确保错误/异常留在程序中有良好的状态，资源，连接，内存都有被合适的清理。</td>
<td>在编码之前察觉可能出现的异常，在代码的所有层次中维持一致性的异常处理策略，提出整个系统的错误处理准则。</td>
<td></td>
</tr>
<tr>
<td>IDE</td>
<td>IDE大部分用来进行文本编辑</td>
<td>了解其周围的接口，能够高效地通过菜单来使用IDE</td>
<td><span style="color: #ff0000;">了解最常操作的键盘快捷键</span></td>
<td>编写自定义宏</td>
<td></td>
</tr>
<tr>
<td><acronym title="Application Programming Interface">API</acronym></td>
<td>需要频繁地查阅文档</td>
<td><span style="color: #ff0000;">把最频繁使用的<acronym title="Application Programming Interface">API</acronym>记在脑子里</span></td>
<td>广阔且深入的<acronym title="Application Programming Interface">API</acronym>知识。</td>
<td>为了使实际任务中常用<acronym title="Application Programming Interface">API</acronym>使用更加便捷，编写过<acronym title="Application Programming Interface">API</acronym>的上层库，填补<acronym title="Application Programming Interface">API</acronym>之间的缺口。</td>
<td></td>
</tr>
<tr>
<td>框架</td>
<td>没有使用过主平台外的任何框架</td>
<td><span style="color: #ff0000;">听过但没用过平台下流行的可用框架</span></td>
<td>在专业的职位中使用过一个以上的框架，通晓各框架的特色。</td>
<td>某框架的作者</td>
<td></td>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>需求分析</td>
<td><span style="color: #ff0000;">接受给定的需求和代码规格</span></td>
<td>能对规格的遗漏提出疑问</td>
<td>了解全面情况，提出需要被规格化的整体范围。</td>
<td>能够提出更好的可选方案，根据经验的浮现给出需求</td>
<td></td>
</tr>
<tr>
<td>脚本</td>
<td>不具备脚本工具的知识</td>
<td>批处理文件/shell脚本</td>
<td><acronym title="Practical Extraction and Report Language">Perl</acronym>/Python/Ruby/VBScript/Powershell</td>
<td><span style="color: #ff0000;">写过并且发表过可重用的代码</span></td>
<td></td>
</tr>
<tr>
<td>数据库</td>
<td>认为Excel就是数据库</td>
<td>知道基本的数据库概念，规范化、ACID（原子性Atomicity、一致性Consistency、隔离性Isolation、持久性Durability）、事务化，能够写简单的select语句</td>
<td>能够牢记在运行时必要查询中设计良好的规范化数据库模式， 精通用户视图，存储过程，触发器和用户定义类型。知道聚集与非聚集索引之间的差异。精通使用ORM（Object Relational Mapping对象关系映射）工具</td>
<td><span style="color: #ff0000;">能做基本的数据库管理，性能优化，索引优化，编写高级的select查询，能够使用相关sql来替换游标，理解数据内部的存储，了解如何镜像、复制数据库。知道两段数据提交如何工作</span></td>
<td></td>
</tr>
<tr>
<th colspan="5">经验 Experience</th>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>专业语言经验</td>
<td>命令式语言和面向对象语言</td>
<td>命令式语言,面向对象语言和说明型语言(<acronym title="Structured Query Language">SQL</acronym>),如果了解静态类型vs动态类型，弱类型vs强类型则有加分</td>
<td>函数式语言,如果了解延缓求值，局部套用函数，延续则有加分</td>
<td><span style="color: #ff0000;">并发语言(Erlang, Oz) 逻辑语言(Prolog)</span></td>
<td></td>
</tr>
<tr>
<td>专业平台经验</td>
<td><span style="color: #000000;">1</span></td>
<td><span style="color: #ff0000;">2-3</span></td>
<td>4-5</td>
<td>6+</td>
<td></td>
</tr>
<tr>
<td>专业经验年龄</td>
<td>1</td>
<td><span style="color: #ff0000;">2-5</span></td>
<td>6-9</td>
<td>10+</td>
<td></td>
</tr>
<tr>
<td>领域知识</td>
<td>没有该领域的知识</td>
<td><span style="color: #ff0000;">在该领域中曾经至少为一个产品工作过</span></td>
<td>在同一领域中为多个产品工作过</td>
<td>领域专家。在该领域设计和实现数种产品/方案。精通该领域使用的标准条款和协议</td>
<td></td>
</tr>
<tr>
<th colspan="5">学识 Knowledge</th>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>工具知识</td>
<td>仅限于主要的IDE(VS.Net, Eclipse等)</td>
<td>知道一些流行和标准工具的备选方案</td>
<td><span style="color: #ff0000;">对编辑器、调试器、IDE、开源的备选方案有很好的了解。比如某人了解大多数Scott Hanselman的威力工具列表中的工具，使用过ORM工具。</span></td>
<td>实际地编写过工具和脚本，如果这些被发布则有加分</td>
<td></td>
</tr>
<tr>
<td>语言接触</td>
<td>命令式语言和面向对象语言</td>
<td>命令式语言、面向对象语言和说明型语言(<acronym title="Structured Query Language">SQL</acronym>),如果了解静态类型vs动态类型、弱类型vs强类型则有加分</td>
<td>函数式语言,如果了解延缓求值、局部套用函数、continuations （源于scheme中的一种高级控制结构）则有加分</td>
<td><span style="color: #ff0000;">并发语言(Erlang, Oz) 逻辑语言(Prolog)</span></td>
<td></td>
</tr>
<tr>
<td>代码库知识</td>
<td>从来没有查询过代码库</td>
<td><span style="color: #ff0000;">基本的代码层知识，了解如果构建系统</span></td>
<td>良好的代码库工作知识，实现过几次bug修复或者完成了一些细小的特性</td>
<td>实现了代码库中多个大型特性，能够轻松地将多数特性的需求变更具体化，从容地处理bug修复。</td>
<td></td>
</tr>
<tr>
<td>下一代技术知识</td>
<td>从来没听说过即将到来的技术</td>
<td>听说过某领域即将到来的技术</td>
<td><span style="color: #ff0000;">下载过alpha preview/CTP/beta版本，并且读过一些文章和手册</span></td>
<td>试用过预览版而且实际地构建过某物，如果共享给其他人的话则有加分</td>
<td></td>
</tr>
<tr>
<td></td>
<td>2<sup>n</sup> (Level 0)</td>
<td>n<sup>2</sup> (Level 1)</td>
<td>n (Level 2)</td>
<td>log(n) (Level 3)</td>
<td></td>
</tr>
<tr>
<td>平台内部</td>
<td>对平台内部毫无所知</td>
<td><span style="color: #ff0000;">有平台基本的内部工作的知识</span></td>
<td>深度的平台内部知识，能够设想平台如何将程序转换成可执行代码。</td>
<td>编写过增强平台或者为其平台内部提供信息的工具。比如，反汇编工具，反编译工具，调试工具等。</td>
<td></td>
</tr>
<tr>
<td>书籍</td>
<td>菜鸟系列，21天系列，24小时系列，蠢货系列&#8230;</td>
<td>《代码大全》，《别让我思考》, 《精通正则表达式》</td>
<td>《设计模式》，《人件》，《代码珠玑》，《算法设计手册》，《程序员修炼之道》，《人月神话》</td>
<td><span style="color: #ff0000;">《计算机程序设计与解释》，《事务处理:概念与技术》，《计算机程序设计模型》，《计算机程序设计艺术》，《数据库系统导论》 C.J Date版，《Thinking Forth》 ，《Little Schemer》（没找到其中译本）</span></td>
<td></td>
</tr>
<tr>
<td>博客</td>
<td>听过但是从来抽不出空去接触</td>
<td>阅读一些科技/编程/软件工程的博客，并且经常的收听一些播客</td>
<td>维护一些博客的链接，收集博主分享的有用的文章和工具</td>
<td><span style="color: #ff0000;">维护一个在编程方面，分享有个人见解和思考的博客</span></td>
<td><span style="font-size: small;"><span style="line-height: 19px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px;"><span style="font-size: small;"><span style="line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br />
</span></span></span></span></td>
</tr>
</tbody>
</table><h2  class="related_post_title">随机显示文章</h2><ul class="related_post"><li>2010年07月25日 -- <a href="http://www.penglixun.com/life/%e6%af%8f%e5%91%a8%e6%8e%a8%e7%89%b9-2010-07-25.html" title="每周推特 2010-07-25">每周推特 2010-07-25</a> (0)</li><li>2010年05月14日 -- <a href="http://www.penglixun.com/tech/program/mystat_update_20100514.html" title="mystat 更新1.1.1">mystat 更新1.1.1</a> (3)</li><li>2009年07月15日 -- <a href="http://www.penglixun.com/tech/system/build_webserver_use_lighttpd_spuid_apache.html" title="Lighttpd+Squid+Apache搭建高效率Web服务器">Lighttpd+Squid+Apache搭建高效率Web服务器</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/programmer_competency_matrix.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL多服务器批量执行SQL脚本</title>
		<link>http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html</link>
		<comments>http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html#comments</comments>
		<pubDate>Wed, 10 Mar 2010 08:12:37 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[mybatsql]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[脚本]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1046</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html 主要应用场景是，假设我们... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html </p>
<p></span>主要应用场景是，假设我们有一个库DB进行了水平拆分，DB1~10都需要更改表结构，如果手动到各个服务器去执行，效率太低，写了个脚本来做这个事情，叫mybatsql。</p>
<p>用法很简单，需要两个文件，server.list和sql.list，跟脚本放在同一目录下。<br />
server.list中放服务器的主机名/<acronym title="Internet Protocol">IP</acronym>，用户名，密码；sql.list中放需要批量执行的<acronym title="Structured Query Language">SQL</acronym>。<br />
例如：<br />
server.list</p>
<blockquote><p>
127.0.0.1,root,111<br />
127.0.0.2,root,111
</p></blockquote>
<p>sql.list</p>
<blockquote><p>
show status;<br />
show variables;
</p></blockquote>
<p>这两个文件表示，我要在127.0.0.1和127.0.0.2上执行show status;show variables;语句。</p>
<p>也可以通过参数-l/-s指定这两个文件的地址：perl mybatsql.pl  -l server_server_file -s sql_file</p>
<p>Note: There is a file embedded within this post, please visit this post to download the file.<br />
<span id="more-1046"></span></p>

<div class="wp_codebox"><table><tr id="p104616"><td class="code" id="p1046code16"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Create: P.Linux</span>
<span style="color: #666666; font-style: italic;"># Function: Run DDL On Server List</span>
<span style="color: #666666; font-style: italic;"># Usage: Run on any computer with Perl</span>
<span style="color: #666666; font-style: italic;"># License: GPL v2</span>
<span style="color: #666666; font-style: italic;"># Site: PengLiXun.COM</span>
<span style="color: #666666; font-style: italic;"># Modify: </span>
<span style="color: #666666; font-style: italic;"># P.Linux 2010-03-10 </span>
<span style="color: #666666; font-style: italic;">#    -Create 1.0 Release</span>
<span style="color: #666666; font-style: italic;">########################################################</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBD<span style="color: #339933;">::</span><span style="color: #006600;">mysql</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Std</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> vars <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$opt_l</span> <span style="color: #0000ff;">$opt_s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">########################################################</span>
<span style="color: #666666; font-style: italic;"># Global Status Var</span>
<span style="color: #666666; font-style: italic;"># 全局状态变量</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@server_list</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sql</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Connect to database via DBI</span>
<span style="color: #666666; font-style: italic;"># 通过DBI连接数据库</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@dbconn</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># CmdLine Option vars</span>
<span style="color: #666666; font-style: italic;"># 命令行参数变量</span>
<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$server_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sql_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Version</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$version</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'1.0 Release'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Main Program</span>
<span style="color: #666666; font-style: italic;"># 主程序</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Get CmdLine Options</span>
<span style="color: #666666; font-style: italic;"># 获取命令行参数</span>
<span style="color: #0000ff;">&amp;get_option</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Read Server List From File</span>
<span style="color: #666666; font-style: italic;"># 从文件中读取服务器列表</span>
<span style="color: #0000ff;">&amp;read_file</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Crate MySQL Database Connect</span>
<span style="color: #666666; font-style: italic;"># 创建MySQL数据库连接</span>
<span style="color: #0000ff;">&amp;create_conn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Run SQL</span>
<span style="color: #666666; font-style: italic;"># 执行SQL</span>
<span style="color: #0000ff;">&amp;run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Disconnect from MySQL</span>
<span style="color: #666666; font-style: italic;"># 从数据库断开连接</span>
<span style="color: #0000ff;">&amp;close_conn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Print Usage</span>
<span style="color: #666666; font-style: italic;"># 打印使用方法</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_usage <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #339933;">&lt;&lt;</span>EOF
 NAME<span style="color: #339933;">:</span>
        mybatsql
&nbsp;
 SYNTAX<span style="color: #339933;">:</span>
        mybatsql  <span style="color: #339933;">-</span>l server_server_file <span style="color: #339933;">-</span><a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a> sql_file
&nbsp;
 FUNCTION<span style="color: #339933;">:</span>
        Run SQL on Server List
&nbsp;
 PARAMETER<span style="color: #339933;">:</span>
      <span style="color: #339933;">-</span>l   Database Server List File DEFAULT<span style="color: #339933;">:</span>server<span style="color: #339933;">.</span>list
      <span style="color: #339933;">-</span><a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>   SQL File DEFAULT<span style="color: #339933;">:</span>sql<span style="color: #339933;">.</span>list
EOF
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Get Options</span>
<span style="color: #666666; font-style: italic;"># 获取命令行参数</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rtn</span> <span style="color: #339933;">=</span> getopts<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'l:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;$rtn&quot;</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> print_usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$server_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$opt_l</span><span style="color: #339933;">?</span><span style="color: #0000ff;">$opt_l</span><span style="color: #339933;">:</span><span style="color: #ff0000;">'server.list'</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$sql_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$opt_s</span><span style="color: #339933;">?</span><span style="color: #0000ff;">$opt_s</span><span style="color: #339933;">:</span><span style="color: #ff0000;">'sql.list'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$server_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$sql_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$server_file</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/lc.html"><span style="color: #000066;">lc</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$server_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$sql_file</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/lc.html"><span style="color: #000066;">lc</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Read Server List &amp; SQL From File</span>
<span style="color: #666666; font-style: italic;"># 从文件中读取服务器列表和SQL。</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> read_file<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Read Server List File</span>
    <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>FILE<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;$server_file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009999;">&lt;FILE&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/chomp.html"><span style="color: #000066;">chomp</span></a><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$server_list</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/,/</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>FILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># Read SQL List File</span>
    <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>FILE<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;$sql_file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009999;">&lt;FILE&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    <a href="http://perldoc.perl.org/functions/chomp.html"><span style="color: #000066;">chomp</span></a><span style="color: #339933;">;</span>
	    <span style="color: #0000ff;">$sql</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
	    <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>FILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Create MySQL Database Connect </span>
<span style="color: #666666; font-style: italic;"># 创建MySQL数据库连接</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> create_conn<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$server</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@server_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@srv</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$server</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$pwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">#print &quot;$host.$user.$pwd\n&quot;;</span>
        <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#123;</span>
            <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>ALRM<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;连接数据库超时<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <a href="http://perldoc.perl.org/functions/alarm.html"><span style="color: #000066;">alarm</span></a> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
            <span style="color: #0000ff;">$dbconn</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:host=$host&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$user</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$pwd</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'RaiseError'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Connect to $host error:&quot;</span><span style="color: #339933;">.</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">errstr</span><span style="color: #339933;">;</span>
            <a href="http://perldoc.perl.org/functions/alarm.html"><span style="color: #000066;">alarm</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>   
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$@</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Connect to $host database error:&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$@</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
            <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Main Program to Run SQL on Server List</span>
<span style="color: #666666; font-style: italic;"># 在服务器列表上执行一组SQL</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> run<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$conn</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@dbconn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">#print $sql;</span>
        <span style="color: #0000ff;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #0000ff;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;"># Disconnect from MySQL</span>
<span style="color: #666666; font-style: italic;"># 从数据库断开连接</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> close_conn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$conn</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@dbconn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">disconnect</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年01月26日 -- <a href="http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html" title="自己写的MySQL实时监控脚本&#8211;mystat">自己写的MySQL实时监控脚本&#8211;mystat</a> (0)</li><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li><li>2011年12月23日 -- <a href="http://www.penglixun.com/tech/database/server_kill_idle_transaction.html" title="在Server层实现Kill Idle Transaction">在Server层实现Kill Idle Transaction</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mystat脚本更新Windows支持</title>
		<link>http://www.penglixun.com/tech/program/mystat_win_support.html</link>
		<comments>http://www.penglixun.com/tech/program/mystat_win_support.html#comments</comments>
		<pubDate>Sat, 06 Mar 2010 07:04:31 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[mystat]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=1012</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mystat_win_support.html mystat脚本更新为1.1版本，加入了Windows... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mystat_win_support.html </p>
<p></span>mystat脚本更新为1.1版本，加入了Windows下的支持。<br />
主要因为Linux版本使用了Bash的彩色显示，所以到了Windows下这个就不兼容了，会看到Bash下控制颜色的代码被现实出来，很难看。<br />
于是加入了操作系统的判断，Windows操作系统跳过颜色控制部分，Linux系统则显示。<br />
然后加入了Inert/Update/Delete流量的实时监控。</p>
Note: There is a file embedded within this post, please visit this post to download the file.<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年05月14日 -- <a href="http://www.penglixun.com/tech/program/mystat_update_20100514.html" title="mystat 更新1.1.1">mystat 更新1.1.1</a> (3)</li><li>2010年02月5日 -- <a href="http://www.penglixun.com/tech/program/mystat_release.html" title="mystat 1.0 release 发布">mystat 1.0 release 发布</a> (0)</li><li>2010年01月28日 -- <a href="http://www.penglixun.com/tech/program/mystat_usage.html" title="mystat 0.5 beta 的使用介绍">mystat 0.5 beta 的使用介绍</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mystat_win_support.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mystique主题与WP-PostViews Plus插件的冲突问题</title>
		<link>http://www.penglixun.com/tech/program/mystique_postviews.html</link>
		<comments>http://www.penglixun.com/tech/program/mystique_postviews.html#comments</comments>
		<pubDate>Tue, 16 Feb 2010 17:48:29 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Mystique]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=980</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mystique_postviews.html 今天升级了Mystique主题到1.72版，原来... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mystique_postviews.html </p>
<p></span>今天升级了Mystique主题到1.72版，原来的那个Bug又出现了，会在页面的顶头显示一段代码，查看了源码，发现是<acronym title="WordPress">WP</acronym>-PostViews Plus的代码和Mystique主题冲突了，我们来看原始输出的<acronym title="HyperText Markup Language">HTML</acronym>：</p>

<div class="wp_codebox"><table><tr id="p98020"><td class="code" id="p980code20"><pre class="html" style="font-family:monospace;">&lt;body class=&quot; 
&lt;!-- Start Of Script Generated By WP-PostViews Plus --&gt; 
&lt;script type=&quot;text/javascript&quot;&gt; 
/* &lt;![CDATA[ */
jQuery.ajax({type:'GET',url:'http://www.penglixun.com/wp-content/plugins/wp-postviews-plus/postviews_plus.php',data:'todowppvp=&amp;type=index&amp;id=1',cache:false,dataType:'script'});
/* ]]&gt; */
&lt;/script&gt; 
&lt;!-- End Of Script Generated By WP-PostViews Plus --&gt; 
home blog col-2-right loggedin browser-chrome&quot;&gt;</pre></td></tr></table></div>

<p>这不出问题才怪呢，正常的情况应该是</p>

<div class="wp_codebox"><table><tr id="p98021"><td class="code" id="p980code21"><pre class="html" style="font-family:monospace;">&lt;!-- Start Of Script Generated By WP-PostViews Plus --&gt; 
&lt;script type=&quot;text/javascript&quot;&gt; 
/* &lt;![CDATA[ */
jQuery.ajax({type:'GET',url:'http://www.penglixun.com/wp-content/plugins/wp-postviews-plus/postviews_plus.php',data:'todowppvp=&amp;type=index&amp;id=1',cache:false,dataType:'script'});
/* ]]&gt; */
&lt;/script&gt; 
&lt;!-- End Of Script Generated By WP-PostViews Plus --&gt; 
&lt;body class=&quot;home blog col-2-right loggedin browser-chrome&quot;&gt;</pre></td></tr></table></div>

<p>或者<body class="home blog col-2-right loggedin browser-chrome">在上面。</p>
<p>刨根问底，元凶指向mystique_body_class这个函数，发现在core.php文件中。<br />
问题在哪呢？</p>

<div class="wp_codebox"><table><tr id="p98022"><td class="code" id="p980code22"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Special classes for BODY element when a single post</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #000088;">$postID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">;</span>
    the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Adds 'single' class and class with the post ID</span>
    <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'single-post postid-'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$postID</span><span style="color: #339933;">;</span>
<span style="color: #339933;">......</span>
  <span style="color: #666666; font-style: italic;">// And tada!</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$print</span> ? <span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>就是the_post()函数，调用the_post()时，<acronym title="WordPress">WP</acronym>-PostViews Plus的代码就被加入了，而<strong>body class=</strong> 已经输出了。<br />
解决方法很简单，把the_post()转移到最后，就是print的下一行，这样就OK了。把这个函数里其他the_post()都删除，留一个在print的下一行就行。</p><h2  class="related_post_title">随机显示文章</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.penglixun.com/tech/system/ntp_synchronization_configure.html" title="NTP同步配置">NTP同步配置</a> (0)</li><li>2010年12月6日 -- <a href="http://www.penglixun.com/tech/database/percona_vs_mysql.html" title="Percona对MySQL标准版本的改进">Percona对MySQL标准版本的改进</a> (3)</li><li>2010年03月21日 -- <a href="http://www.penglixun.com/life/%e6%af%8f%e5%91%a8%e6%8e%a8%e7%89%b9-2010-03-21.html" title="每周推特 2010-03-21">每周推特 2010-03-21</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mystique_postviews.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mystat 1.0 release 发布</title>
		<link>http://www.penglixun.com/tech/program/mystat_release.html</link>
		<comments>http://www.penglixun.com/tech/program/mystat_release.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 07:21:06 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mystat]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=952</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mystat_release.html mystat是一款MySQL数据库实时监控脚本，Per... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mystat_release.html </p>
<p></span>mystat是一款MySQL数据库实时监控脚本，<acronym title="Practical Extraction and Report Language">Perl</acronym>编写，基于MySQL的show global status和show global variables结果进行分析实时显示。能通过参数控释轮询间隔、轮询次数、监控项目。使用了strict;DBI;Switch;DBD::mysql;Getopt::Std;Term::ANSIColor;这几个<acronym title="Practical Extraction and Report Language">Perl</acronym>模块，通过CPAN都可以安装。<br />
我已经尽力测试Bug，不过使用场景各异，如果发现Bug，请联系我，penglixun(at)gmail.com。</p>
<p><strong>脚本包括如下参数</strong><br />
-i    轮询间隔    默认1s<br />
-c   轮询次数    默认无限<br />
-n   监控项目    默认basic<br />
           包括: all(全部项目), basic(基本项目), innodb(InnoDB项目), myisam(MyISAM项目)四个套装<br />
           以及下面的单项：<br />
           traffic    &#8211; Network Traffic<br />
           kbuffer  &#8211; Key Buffer<br />
           qcache   &#8211; Query Cache<br />
           thcache  &#8211; Thread Cache<br />
           tbcache  &#8211; Table Cache<br />
           tmp        &#8211; Temporary Table<br />
           query     &#8211; Queries Statistics<br />
           select     &#8211; Select Statistics<br />
           sort        &#8211; Sort Statistics<br />
           innodb_bp &#8211; InnoDB Buffer Pool<br />
           项目之间可能有交叉的部分，取最大并集。<br />
-d    取消项目    默认noneDB<br />
           包括: var(变量部分), innodb(InnoDB部分), none(无)<br />
           同时跟-n出现一样的项目，则优先处理-d。也就是说-n innodb -d innodb，不会显示innodb的部分。<br />
-h    主机名    默认空<br />
-u    用户名    默认空<br />
-p    密码       默认空</p>
<p>例如：perl mystat.pl -n traffic,qcache,kbuffer,sort -d var -h 127.0.0.1 -u cactiuser -p cacti<br />
<strong>显示如下</strong><br />
<a title="Flickr 上 P.Linux 的 mystat 1.0 release" href="http://www.flickr.com/photos/penglixun/4332078758/"><img src="http://farm3.static.flickr.com/2706/4332078758_2360522596.jpg" alt="mystat 1.0 release" width="500" height="310" /></a></p>
<p><strong>猛击这里下载：</strong>Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p><span id="more-952"></span><br />
再来一张图：<br />
<a href="http://www.flickr.com/photos/penglixun/4331340149/" title="Flickr 上 P.Linux 的 mystat 1.0 release"><img src="http://farm5.static.flickr.com/4012/4331340149_70d7ab1be3.jpg" width="469" height="500" alt="mystat 1.0 release" /></a></p>
<p>显示项含义可以看代码里面的注释，非常清晰，一行计算代码有2～3行注释，说的很明白了。</p>
<p><strong>代码结构</strong><br />
print_usage //显示用法<br />
get_option  //获取命令参数，设置命令默认值<br />
set_env     //获取系统环境变量<br />
do_loop     //循环控制函数<br />
format_val  //格式化数据为K/M/G/T单位<br />
same_char   //打印若干个相同的字符，用于排版<br />
quit        //退出操作，关闭数据库连接<br />
init_val    //初始化最大值<br />
init        //初始化总控函数<br />
get_stat_traffic   //获取流量相关数据<br />
get_stat_kbuffer   //获取索引缓冲相关数据<br />
get_stat_qcache    //获取查询缓存相关数据<br />
get_stat_thcache   //获取线程缓存相关数据<br />
get_stat_tbcache   //获取表缓存相关数据<br />
get_stat_tmp       //获取临时表相关数据<br />
get_stat_select    //获取SELECT相关数据<br />
get_stat_sort      //获取排序相关数据<br />
get_stat_innodb_bp //获取InnoDB缓冲池相关数据<br />
get_stat           //获取数据总控函数<br />
display_header    //显示表头信息和版权信息<br />
display_var_title //显示数据库变量栏标题信息<br />
display_var_cache //显示数据库变量缓存相关信息<br />
display_var_buffer//显示数据库变量缓冲相关信息<br />
display_var_log   //显示数据库变量日志相关信息<br />
display_var_conn  //显示数据库变量连接相关信息<br />
display_var_query //显示数据库变量查询相关信息<br />
display_vars      //显示数据库变量总控函数<br />
display_stat_traffic   //显示数据库状态流量相关信息<br />
display_stat_kbuffer   //显示数据库状态索引缓冲相关信息<br />
display_stat_qcache    //显示数据库状态查询缓存相关信息<br />
display_stat_thcache   //显示数据库状态线程缓存相关信息<br />
display_stat_tbcache   //显示数据库状态表缓存相关信息<br />
display_stat_query     //显示数据库状态查询语句相关信息<br />
display_stat_select    //显示数据库状态选择相关信息<br />
display_stat_sort      //显示数据库状态排序相关信息<br />
display_stat_tmp       //显示数据库状态临时表相关信息<br />
display_stat_innodb_bp //显示数据库状态InnoDB缓冲池相关信息<br />
display_stat           //显示数据库状态总控函数<br />
calc_val            //计算数值模板，包括当前值/平均值/最大值/最小值<br />
calc_stat_traffic   //计算数据库状态流量相关信息<br />
calc_stat_kbuffer   //计算数据库状态索引缓冲相关信息<br />
calc_stat_qcache    //计算数据库状态查询缓存相关信息<br />
calc_stat_thcache   //计算数据库状态线程缓存相关信息<br />
calc_stat_tbcache   //计算数据库状态表缓存相关信息<br />
calc_stat_query     //计算数据库状态查询语句相关信息<br />
calc_stat_select    //计算数据库状态选择语句相关信息<br />
calc_stat_sort      //计算数据库状态排序相关信息<br />
calc_stat_tmp       //计算数据库状态临时表相关信息<br />
calc_stat_innodb_bp //计算数据库状态InnoDB缓冲池相关信息<br />
calc_stat           //计算数据库状态总控函数<br />
refresh_all  //每次刷新数据控制</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年01月28日 -- <a href="http://www.penglixun.com/tech/program/mystat_usage.html" title="mystat 0.5 beta 的使用介绍">mystat 0.5 beta 的使用介绍</a> (0)</li><li>2010年01月26日 -- <a href="http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html" title="自己写的MySQL实时监控脚本&#8211;mystat">自己写的MySQL实时监控脚本&#8211;mystat</a> (0)</li><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mystat_release.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mystat 0.5 beta 的使用介绍</title>
		<link>http://www.penglixun.com/tech/program/mystat_usage.html</link>
		<comments>http://www.penglixun.com/tech/program/mystat_usage.html#comments</comments>
		<pubDate>Thu, 28 Jan 2010 08:48:23 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mystat]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=932</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mystat_usage.html mystat脚本目前已经升级到0.5 beta版本。大... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mystat_usage.html </p>
<p></span>mystat脚本目前已经升级到0.5 beta版本。大部分主要监控参数都完善了，还有InnoDB的参数没有写在发布版内。<br />
最新版猛击这里下载：Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>我来解释下代码主要结构，<br />
# Global System Var<br />
my $USERNAME=”;<br />
my $PASSWORD=”;<br />
my $HOSTNAME=”;<br />
这三个参数分别是数据库的账户、密码、主机名，修改成自己的即可。<br />
我也会继续修改脚本把这些作为参数输入进来，不过得先完善参数再做这些。</p>
<p>然后是函数结构，<br />
init()是初始化函数，初始化各种要统计的最大值为0，以备更新。<br />
get_stat()函数获取MySQL的状态和参数，全部写入status_res和status中。<br />
format_val()是格式化数值为合适的单位：K/M/G/T。<br />
print_header()是打印标题，可有可无。<br />
display_var_*()是显示MySQL的变量。<br />
print_vars()调用display_var_*()来控制需要显示哪些变量，可以在这修改变量显示（界面绿色部分）。<br />
display_stat_*()是显示MySQL的状态。<br />
print_stat()调用display_stat_*()来控制需要显示哪些状态，可以在这里修改状态显示部分，不需要的模块注释掉。<br />
calc_stat_*()是计算MySQL的一些实时状态。<br />
sub calc_stat()调用calc_stat_*()来计算MySQL实时状态，可以在这里修改不需要计算的部分，注释即可。<br />
refresh_all()每次循环都会调用这个函数。</p>
<p>修改完成后使用很简单，直接运行就行了，有效的参数有-i -c两个，-i是监控间隔，-c是监控次数。不填的话默认每秒刷新一次，没有终止，用Ctrl+C停止，会自动捕捉键盘输入执行关闭数据库连接。</p>
<p>鉴于参数很多，最好最大化shell才能看到全部数值，或者注释掉一些部分，通过参数来控制显示模块这个版本还没支持<br />
<a href="http://www.flickr.com/photos/penglixun/4311174756/" title="Flickr 上 P.Linux 的 mystat 0.5 beta"><img src="http://farm5.static.flickr.com/4041/4311174756_f18ecd8508.jpg" width="500" height="494" alt="mystat 0.5 beta" /></a></p>
<p>大部分显示值含义应该能看懂吧，如果有不明白或者建议请留言。<br />
完整的显示数值含义我也会写出来，不过要等我把需要采集计算的数据都编写完才来写这个。</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年02月5日 -- <a href="http://www.penglixun.com/tech/program/mystat_release.html" title="mystat 1.0 release 发布">mystat 1.0 release 发布</a> (0)</li><li>2010年01月26日 -- <a href="http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html" title="自己写的MySQL实时监控脚本&#8211;mystat">自己写的MySQL实时监控脚本&#8211;mystat</a> (0)</li><li>2012年01月23日 -- <a href="http://www.penglixun.com/tech/database/case_about_innodb_faster_than_oracle.html" title="一个InnoDB性能超过Oracle的调优Case">一个InnoDB性能超过Oracle的调优Case</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mystat_usage.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自己写的MySQL实时监控脚本&#8211;mystat</title>
		<link>http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html</link>
		<comments>http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html#comments</comments>
		<pubDate>Tue, 26 Jan 2010 09:44:43 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mystat]]></category>
		<category><![CDATA[监控]]></category>
		<category><![CDATA[脚本]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=913</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html 一直想模拟一个Spotlight ... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html </p>
<p></span>一直想模拟一个Spotlight For MySQL一样的软件，能够实时监控数据库，做UI实在不擅长，看了<a href="http://www.ningoo.net">NinGoo</a>大牛的《<a href="http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html">tbstat:实时监控数据库统计状态的小工具</a>》，觉得还是写个脚本比较靠谱，就抽空折腾了一下。<br />
参考了部分NinGoo大牛的代码。<br />
因为不太会参数的输入，需要进脚本改my $USERNAME=”;my $PASSWORD=”;my $HOSTNAME=&#8217;localhost&#8217;;三个地方，分别是用户名/密码/主机名，然后直接运行就可以了，轮询间隔次数可以输入参数，跟NinGoo大牛的脚本一样。<br />
原理很简单，就是靠Show Status和Show Variables返回的结果来显示。<br />
大家可以继续修改脚本，不过希望能把改进的结果发给我看看，我也学习下。现在监控的项目还比较少，我挑选一些比较重要的慢慢完善，同时大家也可以自己加一些进去，$status_res里面包括了status和variables的全部内容。</p>
<p>猛击这里下载：Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p><a href="http://www.flickr.com/photos/penglixun/4308824500/" title="Flickr 上 P.Linux 的 mystat 0.3alpha版本"><img src="http://farm5.static.flickr.com/4069/4308824500_8b59a2dd16.jpg" width="500" height="334" alt="mystat 0.3alpha版本" /></a></p>
<p>献上脚本：</p>

<div class="wp_codebox"><table><tr id="p91324"><td class="code" id="p913code24"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #666666; font-style: italic;">#############################################</span>
<span style="color: #666666; font-style: italic;"># Create: P.Linux</span>
<span style="color: #666666; font-style: italic;"># Function: Check MySQL Status</span>
<span style="color: #666666; font-style: italic;"># Usage: Run on any computer</span>
<span style="color: #666666; font-style: italic;"># Modify: </span>
<span style="color: #666666; font-style: italic;"># P.Linux 2010-01-22 </span>
<span style="color: #666666; font-style: italic;">#    Create 0.1 Alpha</span>
<span style="color: #666666; font-style: italic;"># P.Linux 2010-01-26 </span>
<span style="color: #666666; font-style: italic;">#    Update 0.2 Alpha</span>
<span style="color: #666666; font-style: italic;">#    Add color</span>
<span style="color: #666666; font-style: italic;">#    Add stat Monitor</span>
<span style="color: #666666; font-style: italic;"># P.Linux 2010-01-27</span>
<span style="color: #666666; font-style: italic;">#    Update 0.3 Alpha </span>
<span style="color: #666666; font-style: italic;">#    Move all traffic infromation to one area</span>
<span style="color: #666666; font-style: italic;">#    Add a simple warning function using color</span>
<span style="color: #666666; font-style: italic;">#############################################</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Curses<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Std</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Term<span style="color: #339933;">::</span><span style="color: #006600;">ANSIColor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> vars <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$opt_i</span> <span style="color: #0000ff;">$opt_c</span> <span style="color: #0000ff;">$opt_n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#############################################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Catch ctrl+c Quit</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>TERM<span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>INT<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">\&amp;quit</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set env var from shell profile</span>
set_env<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Autoflush for print</span>
<span style="color: #0000ff;">$|</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Global System Var</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$USERNAME</span><span style="color: #339933;">=</span><span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$PASSWORD</span><span style="color: #339933;">=</span><span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$HOSTNAME</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'localhost'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Global Status Var</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@status</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># CmdLine Option vars</span>
<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Version</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$version</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'0.3Alpha'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#############################################</span>
<span style="color: #666666; font-style: italic;"># Main Program</span>
<span style="color: #666666; font-style: italic;">#############################################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Get CmdLine Options</span>
<span style="color: #0000ff;">&amp;get_option</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Connect to database as root via DBI</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#123;</span>
    <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>ALRM<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;连接数据库超时<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/alarm.html"><span style="color: #000066;">alarm</span></a> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$dbconn</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:host=$HOSTNAME&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$USERNAME</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$PASSWORD</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'RaiseError'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Connect to MySQL database error:&quot;</span><span style="color: #339933;">.</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">errstr</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/alarm.html"><span style="color: #000066;">alarm</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$@</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Connect to MySQL database error:&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$@</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Do loop</span>
<span style="color: #0000ff;">&amp;do_loop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Disconnect from MySQL</span>
<span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">disconnect</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">###########################################</span>
<span style="color: #666666; font-style: italic;">## Print Usage</span>
<span style="color: #666666; font-style: italic;">###########################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_usage <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #339933;">&lt;&lt;</span>EOF
 NAME<span style="color: #339933;">:</span>
        mystat
&nbsp;
 SYNTAX<span style="color: #339933;">:</span>
        mystat <span style="color: #339933;">-</span>i interval <span style="color: #339933;">-</span>c count <span style="color: #339933;">-</span>n statname
&nbsp;
 FUNCTION<span style="color: #339933;">:</span>
        Report Status Information of MySQL
&nbsp;
 PARAMETER<span style="color: #339933;">:</span>
     <span style="color: #339933;">-</span>i    interval interval <a href="http://perldoc.perl.org/functions/time.html"><span style="color: #000066;">time</span></a><span style="color: #339933;">,</span>default <span style="color: #cc66cc;">1</span> seconds
     <span style="color: #339933;">-</span>c    count        <a href="http://perldoc.perl.org/functions/times.html"><span style="color: #000066;">times</span></a>
     <span style="color: #339933;">-</span>n    name         statistics name
&nbsp;
EOF
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#########################################################</span>
<span style="color: #666666; font-style: italic;">## Get Options</span>
<span style="color: #666666; font-style: italic;">#########################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rtn</span> <span style="color: #339933;">=</span> getopts<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'i:c:n:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;$rtn&quot;</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> print_usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$interval</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$opt_i</span><span style="color: #339933;">?</span><span style="color: #0000ff;">$opt_i</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$count</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$opt_c</span><span style="color: #339933;">?</span><span style="color: #0000ff;">$opt_c</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$opt_n</span><span style="color: #339933;">?</span><span style="color: #0000ff;">$opt_n</span><span style="color: #339933;">:</span><span style="color: #ff0000;">'nothing'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$interval</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/[0-9]/</span> <span style="color: #339933;">||</span> <span style="color: #0000ff;">$count</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/[0-9]/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> print_usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/[0-9]/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/[0-9]/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> print_usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$interval</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$count</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$name</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/lc.html"><span style="color: #000066;">lc</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#############################################</span>
<span style="color: #666666; font-style: italic;">## Main Loop to get MySQL Status</span>
<span style="color: #666666; font-style: italic;">#############################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> do_loop<span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$now</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            refresh_all<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <a href="http://perldoc.perl.org/functions/sleep.html"><span style="color: #000066;">sleep</span></a> <span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># if $count == 0 then loop time is unlimit</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            refresh_all<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <a href="http://perldoc.perl.org/functions/sleep.html"><span style="color: #000066;">sleep</span></a> <span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#########################################################</span>
<span style="color: #666666; font-style: italic;">## Set env from profile</span>
<span style="color: #666666; font-style: italic;">#########################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> set_env <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'plx'</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/chomp.html"><span style="color: #000066;">chomp</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$profile</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/home/&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$user</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;/.profile&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>e <span style="color: #0000ff;">$profile</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$profile</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/home/&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$user</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;/.bash_profile&quot;</span>
    <span style="color: #009900;">&#125;</span>
    <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>NEWENV<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;. $profile &amp;&amp; env|&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009999;">&lt;NEWENV&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(\w+)=(.*)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$ENV</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;$2&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> NEWENV<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">## Catch Ctrl+C</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> quit <span style="color: #009900;">&#123;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Exit...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">disconnect</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">## Initialization</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> init <span style="color: #009900;">&#123;</span>
    get_stat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_traffic'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_sent'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_rows'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_times'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_load'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">##### Print Value</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_val <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$val</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span>
                        <span style="color: #339933;">?</span> <span style="color: #0000ff;">$val</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span>
                            <span style="color: #339933;">?</span> <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$fmt K&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$val</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #339933;">:</span> <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$fmt M&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$val</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">:</span> <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$fmt G&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$val</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">#### Print Same Char</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_char <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cnt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$cnt</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$ch&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Print Header</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_header <span style="color: #009900;">&#123;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#print &quot;+&quot;;print_char('-',75);print &quot;+\n&quot;;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">29</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;mystat Ver &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$version</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">29</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">27</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;Powered by PengLiXun.NET&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">26</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#print &quot;+&quot;;print_char('-',75);print &quot;+\n&quot;;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Version &amp; Hostname &amp; Uptime</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_title <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Display Version &amp; Hostname</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'version'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;|--MySQL $ver&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;%25s&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;@ &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'hostname'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; (&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'version_compile_machine'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;)&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># Display Uptime</span>
    <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sec</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$day</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/gmtime.html"><span style="color: #000066;">gmtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Uptime'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$day</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;   Uptime:%3sd%3sh%3sm%3ss&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sec</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;---%2ss--|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Cache</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_cache <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'query_cache_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$thd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'thread_cache_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tbl</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/defined.html"><span style="color: #000066;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'table_open_cache'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">?</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'table_open_cache'</span><span style="color: #009900;">&#125;</span>
                        <span style="color: #339933;">:</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'table_cache'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#print color(&quot;green&quot;);</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>|Query Cache: &quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%3s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Thread Cache: %5s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$thd</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Table Cache: %5s|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tbl</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#print color(&quot;reset&quot;);</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Buffer</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_buffer <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'key_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$join</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'join_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sort</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'sort_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>|Key Buffer: &quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$key</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%4s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | Sort Buffer: &quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sort</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%4s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | Join Buffer: &quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$join</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%3s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Log Status</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_log <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$g_log</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'log'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$b_log</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'log_bin'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s_log</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'log_slow_queries'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>|General Log: %5s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$g_log</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Bin Log: %10s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$b_log</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Slow Log: %8s|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$s_log</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Connections</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_conn <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_conn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'max_connections'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_used</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_used_connections'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$act_conn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Threads_connected'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$used_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$max_used</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$max_conn</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$act_conn</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$max_conn</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>|Act User:%4s(%2.0f%%)&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$act_conn</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_rate</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$used_rate</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Max Used:%5s(%2.0f%%)&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_used</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$used_rate</span><span style="color: #339933;">;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
         <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Max Used:%5s(%2.0f%%)&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_used</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$used_rate</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | Max Connect:%6s|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_conn</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Query</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_var_query <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$select</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;Com_select&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$insert</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;Com_insert&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$update</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;Com_update&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$delete</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;Com_delete&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$select</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$insert</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$update</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$delete</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$select_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$select</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$sql</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$insert_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$insert</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$sql</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$update_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$update</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$sql</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$delete_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$delete</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$sql</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>|SELECT:%5.2f%%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$select_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | INSERT:%5.2f%%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$insert_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | UPDATE:%5.2f%%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$update_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | DELETE:%5.2f%%|<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$delete_rate</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">##### Print Variables</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_vars <span style="color: #009900;">&#123;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_cache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_buffer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_log<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_conn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_var_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Traffic Information</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_stat_traffic <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_received</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sent</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_traffic</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_traffic'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_received</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_sent</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_traffic</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_traffic'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$all_received</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$all_sent</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$all_traffic</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_traffic'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Network Traffic&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Now Traf:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$now_traffic</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Now Recv:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$now_received</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Now Sent:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$now_sent</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Max Traf:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max_traffic</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Max Recv:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max_received</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Max Sent:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max_sent</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%9.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B/s&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;All Traf:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$all_traffic</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%11.4f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;All Recv:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$all_received</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%11.4f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;All Sent:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$all_sent</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%11.4f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;B&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Key Buffer</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_stat_key <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'key_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_blocks_used</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_blocks_used'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_blocks_unused</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_blocks_unused'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_used_rate</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_free_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_free_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_used</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_free</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_free'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_read_requests</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_read_requests'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_reads</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_reads'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_read_hit_rate</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_read_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_write_requests</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_write_requests'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_writes</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_writes'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_write_hit_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_write_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key_avg_hit_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_avg_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Key Buffer&quot;</span><span style="color: #339933;">;</span>
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">65</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Buffer Used:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$key_used</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%10.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;B (%6.2f%%)&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key_used_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;  |  &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Buffer Free:&quot;</span><span style="color: #339933;">;</span>
    print_val<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$key_free</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%10.2f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;B (%6.2f%%)&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key_free_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Avg Hit:%13.2f %%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key_avg_hit_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Read Hit:%12.2f %%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key_read_hit_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Write Hit:%11.2f %%<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key_write_hit_rate</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Query Cache</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_stat_qcache <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query_cache_size</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'query_cache_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_free_memory</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_free_memory'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_free_blocks</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_free_blocks'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_hits</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_hits'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_inserts</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_inserts'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_lowmem_prunes</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_lowmem_prunes'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_not_cached</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_not_cached'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_queries_in_cache</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_queries_in_cache'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_total_blocks</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_total_blocks'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_frag_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_frag_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_used_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_used_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$qcache_hit_rate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Query Cache&quot;</span><span style="color: #339933;">;</span>print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>print <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Qcache Used: %8.2f %%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$qcache_used_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Qcache Hit: %9.2f %%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$qcache_hit_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Fragmentation: %6.2f %%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$qcache_frag_rate</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Query in Cache:%8s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$qcache_queries_in_cache</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Query Low Mem Prunes:%10s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$qcache_lowmem_prunes</span><span style="color: #339933;">;</span>    
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">### Display Sort</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> display_stat_sort <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_rows</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_times</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_load</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_load'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_range</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_range'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_scan</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_scan'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_sort_merge_passes</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_merge_passes'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_sort_rows</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_sort_times</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_times'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$max_sort_load</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_load'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+&quot;</span><span style="color: #339933;">;</span>
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Sort&quot;</span><span style="color: #339933;">;</span>
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">71</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;+<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> color<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Now Rows:%7.0f Rows/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_rows</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Now Times:%5.0f Times/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_times</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Now Load:%4.0f Rows/Time&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_load</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Max Rows:%7.0f Rows/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_sort_rows</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Max Times:%5.0f Times/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_sort_times</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Max Load:%4.0f Rows/Time&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max_sort_load</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    print_char<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Scan: %9.0f Times/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_scan</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Range: %8.0f Times/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_range</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;Merge: %8.0f Times/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$now_sort_merge_passes</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">#### Print Status</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> print_stat <span style="color: #009900;">&#123;</span>
    display_stat_traffic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    display_stat_key<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_stat_qcache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    display_stat_sort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">##### Get MySQL Variables &amp; Status</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> get_stat <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Get MySQL Version </span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SELECT version();&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectrow_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;version&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ver</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Get MySQL Variables</span>
    <span style="color: #0000ff;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SHOW GLOBAL VARIABLES;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vars</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$vars</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;$row-&gt;[0]&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Get MySQL Status</span>
    <span style="color: #0000ff;">$sql</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;SHOW GLOBAL STATUS;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$stat</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$dbconn</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$stat</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;$row-&gt;[0]&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;$row-&gt;[0]&quot;</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">####### Calc Result Status</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> calc_stat <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Calc Traffic</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_traffic'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>    
&nbsp;
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_sent'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_sent'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_traffic'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_traffic'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_traffic'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_traffic'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_traffic'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_received'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_received'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_sent'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Bytes_sent'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Bytes_sent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_frag_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
            <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_total_blocks'</span><span style="color: #009900;">&#125;</span>
         <span style="color: #339933;">?</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_free_blocks'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_total_blocks'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span>
         <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_used_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
            <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'query_cache_size'</span><span style="color: #009900;">&#125;</span>
         <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'query_cache_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_free_memory'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'query_cache_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span>
         <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_hit_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
            <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_hits'</span><span style="color: #009900;">&#125;</span>
         <span style="color: #339933;">?</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_hits'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Qcache_inserts'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span>
         <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Calc Key Buffer</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_blocks_used'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_blocks_unused'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_blocks_used'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_free_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_used_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #0000ff;">*$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'key_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_free'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_free_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #0000ff;">*$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'key_buffer_size'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_write_hit_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span>  <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_writes'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_write_requests'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_read_hit_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span>  <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_reads'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_read_requests'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span> 
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_avg_hit_rate'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_write_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Key_read_hit_rate'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Calc Sort </span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_rows'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_rows'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_range'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_range'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_range'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_scan'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_scan'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_scan'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_merge_passes'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_merge_passes'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Sort_merge_passes'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> 
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_range'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_scan'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_merge_passes'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_load'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span>
           <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span>
        <span style="color: #339933;">?</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span>
        <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_times'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_times'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_times'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_rows'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_rows'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_rows'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_load'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_load'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Max_Sort_load'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$status_res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'Now_Sort_load'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #666666; font-style: italic;">###### Refresh All Status</span>
<span style="color: #666666; font-style: italic;">#######################################################</span>
<span style="color: #000000; font-weight: bold;">sub</span> refresh_all <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cnt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$now</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$now</span><span style="color: #339933;">;</span>
    get_stat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    calc_stat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://perldoc.perl.org/functions/system.html"><span style="color: #000066;">system</span></a> <span style="color: #ff0000;">&quot;clear&quot;</span><span style="color: #339933;">;</span>
    print_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    print_vars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    print_stat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年03月10日 -- <a href="http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html" title="MySQL多服务器批量执行SQL脚本">MySQL多服务器批量执行SQL脚本</a> (1)</li><li>2010年02月5日 -- <a href="http://www.penglixun.com/tech/program/mystat_release.html" title="mystat 1.0 release 发布">mystat 1.0 release 发布</a> (0)</li><li>2010年01月28日 -- <a href="http://www.penglixun.com/tech/program/mystat_usage.html" title="mystat 0.5 beta 的使用介绍">mystat 0.5 beta 的使用介绍</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/mysql_realtime_status_moniter_mystat.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>二分法实现TopK算法的方法</title>
		<link>http://www.penglixun.com/tech/program/dichotomy_topk_cpp.html</link>
		<comments>http://www.penglixun.com/tech/program/dichotomy_topk_cpp.html#comments</comments>
		<pubDate>Wed, 20 Jan 2010 10:06:51 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[TopK]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/?p=911</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/dichotomy_topk_cpp.html 在Jacky一篇关于Oracle排序算法的文章... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/dichotomy_topk_cpp.html </p>
<p></span>在Jacky一篇关于<a href="http://www.hellodba.net/2009/12/oracle_sort.html">Oracle排序算法</a>的文章中，讨论了下Oracle的Short sort算法。文章中对此算法有详细描述，这里不赘述，大致就是通过Heap来实现。<br />
虽然Heap在处理优先队列类型的问题上很有优势，但是我一致觉得它不太适合做排序，调堆的代价其实是比较高的，每加入一个元素删除一个元素都要调堆。<br />
对于TopK的问题，我还是觉得二分法实现比较好。首先按快排的算法把数据分成两堆，左大右小，再判断左边的大堆是不是数量小于了K，小于了了就使用上次的右边界进入排序流程，不到则继续二分。<br />
程序如下：</p>

<div class="wp_codebox"><table><tr id="p91126"><td class="code" id="p911code26"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;cstdlib&gt;</span>
<span style="color: #339900;">#include &lt;time.h&gt;</span>
<span style="color: #339900;">#include &lt;math.h&gt;</span>
<span style="color: #339900;">#define MAXN 10000</span>
<span style="color: #339900;">#define LIMIT 500</span>
&nbsp;
<span style="color: #0000ff;">int</span> a<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> last<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> count_sw, count_cmp<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//初始化测试数据</span>
<span style="color: #0000ff;">void</span> init <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #666666;">//参与查询的数据</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>MAXN<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		a<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span>  <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span>MAXN<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> swap<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>x, <span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> t <span style="color: #008080;">;</span>
	t <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span>
	x <span style="color: #000080;">=</span> y<span style="color: #008080;">;</span>
	y <span style="color: #000080;">=</span> t<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> <span style="color: #0000dd;">qsort</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> start, <span style="color: #0000ff;">int</span> end<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>start <span style="color: #000080;">&lt;</span> end<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">int</span> mid <span style="color: #000080;">=</span> arr<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span><span style="color: #008000;">&#40;</span>end<span style="color: #000040;">-</span>start<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> start<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> start <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> end <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span><span style="color: #000040;">++</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> mid<span style="color: #008000;">&#41;</span> count_cmp<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span><span style="color: #000040;">--</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> mid<span style="color: #008000;">&#41;</span> count_cmp<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">&gt;=</span>j<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
			swap<span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, arr<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			count_sw<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000dd;">qsort</span> <span style="color: #008000;">&#40;</span>arr, start, i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">qsort</span> <span style="color: #008000;">&#40;</span>arr, j<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>, end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> topK<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> start, <span style="color: #0000ff;">int</span> end, <span style="color: #0000ff;">int</span> k<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>start <span style="color: #000080;">&lt;</span> end<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">int</span> mid <span style="color: #000080;">=</span> arr<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span><span style="color: #008000;">&#40;</span>end<span style="color: #000040;">-</span>start<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> start<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> start <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> end <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span><span style="color: #000040;">++</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> mid<span style="color: #008000;">&#41;</span> count_cmp<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span><span style="color: #000040;">--</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> mid<span style="color: #008000;">&#41;</span> count_cmp<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">&gt;=</span>j<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
			swap<span style="color: #008000;">&#40;</span>arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, arr<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			count_sw<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>i<span style="color: #000040;">-</span>start <span style="color: #000080;">&gt;</span> k<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			last <span style="color: #000080;">=</span> i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
			topK <span style="color: #008000;">&#40;</span>arr, start, i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span>, k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span><span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">qsort</span><span style="color: #008000;">&#40;</span>arr, start, last<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	count_sw <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	count_cmp <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	topK<span style="color: #008000;">&#40;</span>c, <span style="color: #0000dd;">0</span>, MAXN<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Result:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> MAXN <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> c<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Swaps:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> count_sw <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Campare:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> count_cmp <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>测试结果比Heap要好一些。</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年07月5日 -- <a href="http://www.penglixun.com/tech/program/algorithm_power.html" title="李开复——算法的力量">李开复——算法的力量</a> (1)</li><li>2010年06月22日 -- <a href="http://www.penglixun.com/tech/database/database_algorithm_and_data_structure_cache_buffer_lock.html" title="数据库算法与数据结构——Cache&#038;Buffer&#038;Lock">数据库算法与数据结构——Cache&#038;Buffer&#038;Lock</a> (1)</li><li>2009年08月20日 -- <a href="http://www.penglixun.com/entertainment/chasing_girls_algorithm.html" title="追MM各种算法">追MM各种算法</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/dichotomy_topk_cpp.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>第一次为开源社区贡献代码</title>
		<link>http://www.penglixun.com/tech/program/frist_contribution_for_open_source.html</link>
		<comments>http://www.penglixun.com/tech/program/frist_contribution_for_open_source.html#comments</comments>
		<pubDate>Tue, 03 Nov 2009 08:58:49 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[Collabtive]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[开源]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/PLX/Blog/?p=520</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/frist_contribution_for_open_source.html 今天使用Collabtive项目管... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/frist_contribution_for_open_source.html </p>
<p></span>今天使用Collabtive项目管理平台的时候发现其中的任务列表代码有问题，</p>
<p>于是发扬了一下开源精神，修改了一下代码，并发给了作者，</p>
<p>第一次为开源社区贡献代码，虽然小的微不足道，但是很兴奋，今后要贡献更多的代码。</p>
<p><span id="more-520"></span></p>
<p>Hello, I found a bug in this projrct.<br />
In the file “\include\class.task.php” line 25 &amp; line 26.<br />
Your codes are :<br />
$this-&gt;mylog = new mylog;<br />
$this-&gt;plugin =  new plugin();</p>
<p>This will cause a error:<br />
Fatal error: Call to a member function callSignalFuncs() on a non-object in /opt/lampp/htdocs/pm/include/class.task.php on line 118</p>
<p>Because you defined like this:<br />
class task<br />
{<br />
private $mylog;<br />
private $plugins;<br />
&#8230;&#8230;<br />
}</p>
<p>But you used like this:<br />
$this-&gt;plugin =  new plugin();</p>
<p>I change these two line to:<br />
$this-&gt;mylog = new mylog();<br />
$this-&gt;plugins =  new plugin();</p>
<p>Now it run well.</p>
<p>I&#8217;m a chinese student that like Open Source and Linux.<br />
My English is poor, I hope you can understand what I said.</p><h2  class="related_post_title">随机显示文章</h2><ul class="related_post"><li>2010年03月10日 -- <a href="http://www.penglixun.com/tech/program/mysql_bat_sql_script_mybatsql.html" title="MySQL多服务器批量执行SQL脚本">MySQL多服务器批量执行SQL脚本</a> (1)</li><li>2010年06月6日 -- <a href="http://www.penglixun.com/tech/database/mysql_binlog_risk.html" title="MySQL binlog的地雷">MySQL binlog的地雷</a> (0)</li><li>2009年11月9日 -- <a href="http://www.penglixun.com/life/feeling/japanese_family_firm_culture.html" title="日本家族式企业的文化">日本家族式企业的文化</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/frist_contribution_for_open_source.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>一个晚上折腾完了J2EE的课设</title>
		<link>http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html</link>
		<comments>http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html#comments</comments>
		<pubDate>Wed, 21 Oct 2009 16:19:14 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[课设]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/PLX/Blog/?p=444</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html 本来我觉得这是个Mission Impossbl... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html </p>
<p></span>本来我觉得这是个Mission Impossble，做好准备一个晚上不睡觉，结果~竟然现在就做出完了。<br />
基本上达到要求，不过界面丑陋，操作不便，速度慢，但是我第一次用Java开发网站，将就吧……</p>
<p>总结几点吧，（我好像特喜欢一条条列表）<br />
1. 静下心来看，都是那么点东西，有了足够的编程经验，入门都是很快的。<br />
2. 计算机的底层理论和顶层理论都是不变的，变的只有表现这一层。<br />
3. 底层都是从图灵机（汗~），形象化到二进制，再到机器码，再到汇编语言，再到高级语言，然后通过数据结构的组织和算法的实现，来达到目的。<br />
4. 顶层都是由软件体系结构、软件方法学这一套下来，把方法具体化去实现。<br />
5. 所以，变化最快的就是实现这些东西的语言，基础理论（算法和数据结构）和工程理论（方法学、体系结构）才是不变的东西。<br />
6. 掌握好不变的东西，只要精通一种工具，再用其他的，都能很快入门，精通就看经验了。</p>
Note: There is a file embedded within this post, please visit this post to download the file.<h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2009年10月14日 -- <a href="http://www.penglixun.com/tech/program/orm_or_rom.html" title="ORM or ROM？">ORM or ROM？</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ORM or ROM？</title>
		<link>http://www.penglixun.com/tech/program/orm_or_rom.html</link>
		<comments>http://www.penglixun.com/tech/program/orm_or_rom.html#comments</comments>
		<pubDate>Wed, 14 Oct 2009 15:49:43 +0000</pubDate>
		<dc:creator>P.Linux</dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[ROM]]></category>

		<guid isPermaLink="false">http://www.penglixun.com/PLX/Blog/?p=424</guid>
		<description><![CDATA[本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/program/orm_or_rom.html 看了一个网站http://kinghost.mirrors.phpclasses.org/... ]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;">本文内容遵从<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh" target="_blank">CC版权协议</a>, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明</br>网址: http://www.penglixun.com/tech/program/orm_or_rom.html </p>
<p></span>看了一个网站http://kinghost.mirrors.phpclasses.org/blog/post/82-<acronym title="Pre-Hypertext Processing">PHP</acronym>-ObjectRelational-Mapping-ORM-or-ROM.html，好东西不少。<br />
又翻看了些博文，http://bighou.javaeye.com/blog/425708，http://luwenpan-gmail-com.javaeye.com/blog/262918。</p>
<p>才发现我一直理解的ORM其实是ROM。我一直以为是先设计表结构，再把表结构映射成对象；看了这些文章，如果我没有理解错的话，先设计表结构（Relationship）再映射成对象（Object）是ROM，并非ORM。Hibernate是ORM工具，也就是先设计对象，再映射为数据库表，实现持久化。<br />
可是这样一来，数据库层面的优化怎么做呢，对象直接映射为表，这是没优化过的模式。实际生产库中应该还是先设计表结构再做ROM吧，Hibernate虽然有ORM，但是实际生产中优化更重要吧，编程上的便捷损失了太多运行效率应该是得不偿失的。<br />
对J2EE不太了解，希望有开发经验的同志们能指点一下。</p><h2  class="related_post_title">类似的文章</h2><ul class="related_post"><li>2010年04月9日 -- <a href="http://www.penglixun.com/tech/system/xfce_zendstudio_error.html" title="Xfce下ZendStudio的报错处理">Xfce下ZendStudio的报错处理</a> (0)</li><li>2009年10月22日 -- <a href="http://www.penglixun.com/tech/program/j2ee_curriculum_design_done.html" title="一个晚上折腾完了J2EE的课设">一个晚上折腾完了J2EE的课设</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.penglixun.com/tech/program/orm_or_rom.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

