查看完整版本: MySQL优化全攻略-相关数据库命令

lily 2007-8-9 14:16

MySQL优化全攻略-相关数据库命令

             <p>我们讨论的是<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%CA%FD%BE%DD%BF%E2');" target="_self"><u><strong>数据库</strong></u></a>性能优化的另一方面,即运用数据库服务器内建的工具辅助性能分析和优化。 <BR><BR>▲ SHOW <BR><BR>执行下面这个命令可以了解服务器的运行状态:<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, 'mysql');" target="_self"><u><strong>mysql</strong></u></a> &gt;show status; <BR><BR>该命令将显示出一长列状态变量及其对应的值,其中包括:被中止访问的用户数量,被中止的连接数量,尝试连接的次数,并发连接数量最大值,以及其他许多有用的信息。这些信息对于确定系统问题和效率低下的原因是十分有用的。 <BR><BR>SHOW命令除了能够显示出MySQL服务器整体状态信息之外,它还能够显示出有关<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%C8%D5%D6%BE');" target="_self"><u><strong>日志</strong></u></a>文件、指定数据库、表、索引、进程和许可权限表的宝贵信息。 <BR><BR>▲ EXPLAIN <BR><BR>EXPLAIN能够分析SELECT命令的处理过程。这不仅对于决定是否要为表加上索引很有用,而且对于了解MySQL处理复杂连接的过程也很有用。 <BR><BR>下面这个例子显示了如何用EXPLAIN提供的信息逐步地优化连接查询。(本例来自MySQL文档,见<A href="http://www.mysql.com/doc/E/X/EXPLAIN.html" target=_blank>http://www.mysql.com/doc/E/X/EXPLAIN.html</A>。原文写到这里似乎有点潦草了事,特加上此例。) <BR><BR>假定用EXPLAIN分析的SELECT命令如下所示: <BR><BR>EXPLAIN SELECT tt.TicketNumber, tt.TimeIn, <BR>&nbsp; &nbsp;&nbsp; &nbsp;tt.ProjectReference, tt.EstimatedShipDate, <BR>&nbsp; &nbsp;&nbsp; &nbsp;tt.ActualShipDate, tt.ClientID, <BR>&nbsp; &nbsp;&nbsp; &nbsp;tt.ServiceCodes, tt.RepetitiveID, <BR>&nbsp; &nbsp;&nbsp; &nbsp;tt.CurrentProcess, tt.CurrentDPPerson, <BR>&nbsp; &nbsp;&nbsp; &nbsp;tt.RecordVolume, tt.DPPrinted, et.COUNTRY, <BR>&nbsp; &nbsp;&nbsp; &nbsp;et_1.COUNTRY, do.CUSTNAME <BR>&nbsp; &nbsp; FROM tt, et, et AS et_1, do <BR>&nbsp; &nbsp; WHERE tt.SubmitTime IS NULL <BR>&nbsp; &nbsp;&nbsp; &nbsp;AND tt.ActualPC = et.EMPLOYID <BR>&nbsp; &nbsp;&nbsp; &nbsp;AND tt.AssignedPC = et_1.EMPLOYID <BR>&nbsp; &nbsp;&nbsp; &nbsp;AND tt.ClientID = do.CUSTNMBR;<BR><BR>SELECT命令中出现的表定义如下: <BR><BR>表定义 <BR><BR>表&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 列&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;列类型&nbsp;&nbsp;<BR>tt&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; ActualPC&nbsp; &nbsp;&nbsp; &nbsp;CHAR(10)&nbsp;&nbsp;<BR>tt&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; AssignedPC&nbsp; &nbsp; CHAR(10)&nbsp;&nbsp;<BR>tt&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; ClientID&nbsp; &nbsp;&nbsp; &nbsp;CHAR(10)&nbsp;&nbsp;<BR>et&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; EMPLOYID&nbsp; &nbsp;&nbsp; &nbsp;CHAR(15)&nbsp;&nbsp;<BR>do&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; CUSTNMBR&nbsp; &nbsp;&nbsp; &nbsp;CHAR(15)<BR><BR>索引 <BR><BR>表&nbsp;&nbsp;索引&nbsp;&nbsp;<BR>tt&nbsp;&nbsp;ActualPC&nbsp;&nbsp;<BR>tt&nbsp;&nbsp;AssignedPC&nbsp;&nbsp;<BR>tt&nbsp;&nbsp;ClientID&nbsp;&nbsp;<BR>et&nbsp;&nbsp;EMPLOYID (主键)&nbsp;&nbsp;<BR>do&nbsp;&nbsp;CUSTNMBR (主键)<BR><BR>tt.ActualPC值分布不均匀 <BR><BR>在进行任何优化之前,EXPLAIN对SELECT执行分析的结果如下: <BR><BR>table type possible_keys&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;key key_len ref rows Extra <BR>et&nbsp;&nbsp;ALL PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;NULL NULL&nbsp;&nbsp;NULL 74 <BR>do&nbsp;&nbsp;ALL PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;NULL NULL&nbsp;&nbsp;NULL 2135 <BR>et_1 ALL PRIMARY&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;NULL NULL&nbsp;&nbsp;NULL 74 <BR>tt&nbsp;&nbsp;ALL AssignedPC,ClientID,ActualPC NULL NULL&nbsp;&nbsp;NULL 3872 <BR>&nbsp; &nbsp;range checked for each record (key map: 35)<BR><BR>每一个表的type都是ALL,它表明MySQL为每一个表进行了完全连接!这个操作是相当耗时的,因为待处理行的数量达到每一个表行数的乘积!即,这里的总处理行数为74 * 2135 * 74 * 3872 = 45,268,558,720。 <BR><BR>这里的问题之一在于,如果数据库列的声明不同,MySQL(还)不能有效地运用列的索引。在这个问题上,VARCHAR和CHAR是一样的,除非它们声明的长度不同。由于tt.ActualPC声明为CHAR(10),而et.EMPLOYID声明为CHAR(15),因此这里存在列长度不匹配问题。 <BR><BR>为了解决这两个列的长度不匹配问题,用ALTER TABLE命令把ActualPC列从10个字符扩展到15字符,如下所示:mysql &gt; ALTER TABLE tt MODIFY ActualPC VARCHAR(15); <BR><BR>现在tt.ActualPC和et.EMPLOYID都是VARCHAR(15)了,执行EXPLAIN进行分析得到的结果如下所示: <BR><BR>table type&nbsp;&nbsp;possible_keys&nbsp;&nbsp;key&nbsp; &nbsp;key_len ref&nbsp; &nbsp;&nbsp;&nbsp;rows&nbsp;&nbsp;Extra <BR>tt&nbsp;&nbsp;ALL&nbsp;&nbsp;AssignedPC,ClientID,ActualPC NULL NULL NULL 3872&nbsp;&nbsp;where used <BR>do&nbsp;&nbsp;ALL&nbsp;&nbsp;PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp; &nbsp; 2135 <BR>&nbsp; &nbsp;range checked for each record (key map: 1) <BR>et_1 ALL&nbsp;&nbsp;PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp; &nbsp; 74 <BR>&nbsp; &nbsp;range checked for each record (key map: 1) <BR><BR>et&nbsp;&nbsp;eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.ActualPC 1<BR><BR>这还算不上完美,但已经好多了(行数的乘积现在少了一个系数74)。现在这个SQL命令执行大概需要数秒钟时间。 为了避免tt.AssignedPC = et_1.EMPLOYID以及tt.ClientID = do.CUSTNMBR比较中的列长度不匹配,我们可以进行如下改动: <BR><BR><BR>mysql &gt; ALTER TABLE tt MODIFY AssignedPC VARCHAR(15), <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;MODIFY ClientID&nbsp;&nbsp;VARCHAR(15);<BR><BR>现在EXPLAIN显示的结果如下: <BR><BR>table type&nbsp;&nbsp;possible_keys&nbsp;&nbsp;key&nbsp; &nbsp;key_len ref&nbsp; &nbsp;&nbsp; &nbsp;rows&nbsp; &nbsp;Extra <BR>et&nbsp;&nbsp;ALL&nbsp;&nbsp;PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp;&nbsp;NULL&nbsp; &nbsp;&nbsp; &nbsp;74 <BR>tt&nbsp;&nbsp;ref&nbsp;&nbsp;AssignedPC,ClientID,ActualPC ActualPC 15 et.EMPLOYID 52 where used <BR>et_1 eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.AssignedPC 1 <BR>do&nbsp;&nbsp;eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.ClientID&nbsp;&nbsp;1<BR><BR>这个结果已经比较令人满意了。余下的问题在于,默认情况下,MySQL假定tt.ActualPC列的值均匀分布,而事实上tt表的情况并非如此。幸而,我们可以很容易地让MySQL知道这一点: <BR><BR>shell &gt; myisamchk --analyze PATH_TO_MYSQL_DATABASE/tt <BR>shell &gt; mysqladmin refresh<BR><BR>现在这个连接操作已经非常理想,EXPLAIN分析的结果如下: <BR><BR>table type&nbsp;&nbsp;possible_keys&nbsp;&nbsp;key&nbsp; &nbsp;key_len ref&nbsp; &nbsp;&nbsp; &nbsp;rows&nbsp;&nbsp;Extra <BR>tt&nbsp;&nbsp;ALL&nbsp;&nbsp;AssignedPC,ClientID,ActualPC NULL NULL NULL&nbsp;&nbsp;3872&nbsp;&nbsp;where used <BR>et&nbsp;&nbsp;eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.ActualPC&nbsp;&nbsp;1 <BR>et_1 eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.AssignedPC 1 <BR>do&nbsp;&nbsp;eq_ref PRIMARY&nbsp; &nbsp;&nbsp;&nbsp;PRIMARY 15&nbsp; &nbsp;tt.ClientID&nbsp;&nbsp;1<BR><BR>▲ OPTIMIZE <BR><BR>OPTIMIZE能够恢复和整理磁盘空间以及数据碎片,一旦对包含变长行的表进行了大量的更新或者删除,进行这个操作就非常有必要了。OPTIMIZE当前只能用于MyISAM和BDB表。 <BR><BR>结束语: <BR><BR>从编译数据库服务器开始、贯穿整个管理过程,能够改善MySQL性能的因素实在非常多,本文只涉及了其中很小的一部分。尽管如此,我们希望本文讨论的内容能够对你有所帮助。 <BR><SCRIPT>var YahooCNADConfig=new Array();YahooCNADConfig['adid']=710YahooCNADConfig['wid']=50397YahooCNADConfig['w']=468YahooCNADConfig['h']=60var YahooCustConfig=new Array();YahooCustConfig['ad_width']=468YahooCustConfig['ad_height']=60YahooCustConfig['default_keyword_number']=8YahooCustConfig['keyword_bg_color']='B99CDD'YahooCustConfig['keyword_fr_color']='FFFFFF'YahooCustConfig['border_color']='9C73CF'</SCRIPT><SCRIPT src="http://view.aliunion.cn.yahoo.com/showad.php" type=text/javascript></SCRIPT></p>     <center><input type="image" onclick=copyToClipBoard() src="http://www.phpchina.com/images/phpcn_book_bu_tj.gif" border="0"></center>
页: [1]
查看完整版本: MySQL优化全攻略-相关数据库命令
PageRank