<?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>haohtml&#039;s blog &#187; 数据库优化</title>
	<atom:link href="http://blog.haohtml.com/tag/%e6%95%b0%e6%8d%ae%e5%ba%93%e4%bc%98%e5%8c%96/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.haohtml.com</link>
	<description>haohtml&#039;s life and works</description>
	<lastBuildDate>Tue, 15 May 2012 09:00:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>MongoDB数据库优化：Mongo Database Profiler</title>
		<link>http://blog.haohtml.com/archives/12898</link>
		<comments>http://blog.haohtml.com/archives/12898#comments</comments>
		<pubDate>Thu, 10 May 2012 09:39:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[nosql]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=12898</guid>
		<description><![CDATA[在MySQL中，慢查询日志是经常作为我们优化数据库的依据，那在MongoDB中是否有类似的功能呢?答案是肯定的，那就是Mongo Database Profiler.不仅有，而且还有一些比MySQL的Slow Query Log更详细的信息。它就是我们这篇文章的主题。 　　开启 Profiling 功能 有两种方式可以控制 Profiling 的开关和级别，第一种是直接在启动参数里直接进行设置。 启动MongoDB时加上–profile=级别 即可。 也可以在客户端调用db.setProfilingLevel(级别) 命令来实时配置。可以通过db.getProfilingLevel()命令来获取当前的Profile级别。 &#160; 　　&#62; db.setProfilingLevel(2); {"was" : 0 , "ok" : 1} &#62; db.getProfilingLevel() &#160; 　上面斜体的级别可以取0，1，2 三个值，他们表示的意义如下： 0 – 不开启 1 – 记录慢命令 (默认为&#62;100ms) 2 – 记录所有命令 Profile 记录在级别1时会记录慢命令，那么这个慢的定义是什么?上面我们说到其默认为100ms，当然有默认就有设置，其设置方法和级别一样有两种，一种是通过添加–slowms启动参数配置。第二种是调用db.setProfilingLevel时加上第二个参数： &#160; 　　db.setProfilingLevel( level , slowms ) db.setProfilingLevel( 1 , 10 ); &#160; 　　查询 Profiling 记录 与MySQL的慢查询日志不同，Mongo Profile 记录是直接存在系统db里的，记录位置 system.profile ，所以，我们只要查询这个Collection的记录就可以获取到我们的 Profile 记录了。 &#160; 　　&#62; db.system.profile.find() {"ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" : "query test.$cmd ntoreturn:1 reslen:66 nscanned:0 query: { profile: 2 } nreturned:1 bytes:50" , "millis" : 0} db.system.profile.find( { info: /test.foo/ } ) {"ts" : "Thu Jan 29 2009 15:19:40 GMT-0500 (EST)" , "info" : "insert test.foo" , "millis" : 0} {"ts" : "Thu Jan 29 2009 15:19:42 GMT-0500 (EST)" , "info" : "insert test.foo" , "millis" : 0} [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 客户端 MongoVue" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12984&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/12/25942526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 客户端 MongoVue</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mongodb亿级数据量的性能测试" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12878&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068564.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mongodb亿级数据量的性能测试</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb索引讲解与性能调优" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12873&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072358.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb索引讲解与性能调优</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>在MySQL中，慢查询日志是经常作为我们优化数据库的依据，那在MongoDB中是否有类似的功能呢?答案是肯定的，那就是Mongo Database Profiler.不仅有，而且还有一些比MySQL的Slow Query Log更详细的信息。它就是我们这篇文章的主题。</p>
<p><strong>　　开启 Profiling 功能</strong></p>
<p>有两种方式可以控制 Profiling 的开关和级别，第一种是直接在启动参数里直接进行设置。</p>
<p>启动MongoDB时加上–profile=级别 即可。</p>
<p>也可以在客户端调用db.setProfilingLevel(级别) 命令来实时配置。可以通过db.getProfilingLevel()命令来获取当前的Profile级别。<span id="more-12898"></span></p>
<p>&nbsp;</p>
<table width="95%" border="0" cellspacing="0" cellpadding="6" align="center">
<tbody>
<tr>
<td bgcolor="#f3f3f3">　　&gt; db.setProfilingLevel(2);<br />
{"was" : 0 , "ok" : 1}<br />
&gt; db.getProfilingLevel()</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><span style="color: #0000ff;">　上面斜体的级别可以取0，1，2 三个值，他们表示的意义如下：</span></p>
<p>0 – 不开启</p>
<p>1 – 记录慢命令 (默认为&gt;100ms)</p>
<p>2 – 记录所有命令</p>
<p>Profile 记录在级别1时会记录慢命令，那么这个慢的定义是什么?上面我们说到其默认为100ms，当然有默认就有设置，其设置方法和级别一样有两种，一种是通过添加–slowms启动参数配置。第二种是调用db.setProfilingLevel时加上第二个参数：</p>
<p>&nbsp;</p>
<table width="95%" border="0" cellspacing="0" cellpadding="6" align="center">
<tbody>
<tr>
<td bgcolor="#f3f3f3">　　db.setProfilingLevel( level , slowms )<br />
db.setProfilingLevel( 1 , 10 );</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>　　查询 Profiling 记录</strong></p>
<p>与MySQL的慢查询日志不同，Mongo Profile 记录是直接存在系统db里的，记录位置 system.profile ，所以，我们只要查询这个Collection的记录就可以获取到我们的 Profile 记录了。</p>
<p>&nbsp;</p>
<table width="95%" border="0" cellspacing="0" cellpadding="6" align="center">
<tbody>
<tr>
<td bgcolor="#f3f3f3">　　&gt; db.system.profile.find()<br />
{"ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" : "query test.$cmd ntoreturn:1 reslen:66 nscanned:0<br />
query: { profile: 2 } nreturned:1 bytes:50" , "millis" : 0}<br />
db.system.profile.find( { info: /test.foo/ } )<br />
{"ts" : "Thu Jan 29 2009 15:19:40 GMT-0500 (EST)" , "info" : "insert test.foo" , "millis" : 0}<br />
{"ts" : "Thu Jan 29 2009 15:19:42 GMT-0500 (EST)" , "info" : "insert test.foo" , "millis" : 0}<br />
{"ts" : "Thu Jan 29 2009 15:19:45 GMT-0500 (EST)" , "info" : "query test.foo ntoreturn:0 reslen:102 nscanned:2<br />
query: {} nreturned:2 bytes:86" , "millis" : 0}<br />
{"ts" : "Thu Jan 29 2009 15:21:17 GMT-0500 (EST)" , "info" : "query test.foo ntoreturn:0 reslen:36 nscanned:2<br />
query: { $not: { x: 2 } } nreturned:0 bytes:20" , "millis" : 0}<br />
{"ts" : "Thu Jan 29 2009 15:21:27 GMT-0500 (EST)" , "info" : "query test.foo ntoreturn:0 exception bytes:53" , "millis" : 88}</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>列出执行时间长于某一限度(5ms)的 Profile 记录：</p>
<p>&nbsp;</p>
<table width="95%" border="0" cellspacing="0" cellpadding="6" align="center">
<tbody>
<tr>
<td bgcolor="#f3f3f3">　　&gt; db.system.profile.find( { millis : { $gt : 5 } } )<br />
{"ts" : "Thu Jan 29 2009 15:21:27 GMT-0500 (EST)" , "info" : "query test.foo ntoreturn:0 exception bytes:53" , "millis" : 88}</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>查看最新的 Profile 记录：</p>
<p>db.system.profile.find().sort({$natural:-1})</p>
<p>Mongo Shell 还提供了一个比较简洁的命令show profile，可列出最近5条执行时间超过1ms的 Profile 记录。</p>
<p><strong>　　Profile 信息内容详解：</strong></p>
<p>ts-该命令在何时执行.</p>
<p>millis Time-该命令执行耗时，以毫秒记.</p>
<p>info-本命令的详细信息.</p>
<p>query-表明这是一个query查询操作.</p>
<p>ntoreturn-本次查询客户端要求返回的记录数.比如, findOne()命令执行时 ntoreturn 为 1.有limit(n) 条件时ntoreturn为n.</p>
<p>query-具体的查询条件(如x&gt;3).</p>
<p>nscanned-本次查询扫描的记录数.</p>
<p>reslen-返回结果集的大小.</p>
<p>nreturned-本次查询实际返回的结果集.</p>
<p>update-表明这是一个update更新操作.</p>
<p>fastmod-Indicates a fast modify operation. See Updates. These operations are normally quite fast.</p>
<p>fastmodinsert – indicates a fast modify operation that performed an upsert.</p>
<p>upsert-表明update的upsert参数为true.此参数的功能是如果update的记录不存在，则用update的条件insert一条记录.</p>
<p>moved-表明本次update是否移动了硬盘上的数据，如果新记录比原记录短，通常不会移动当前记录，如果新记录比原记录长，那么可能会移动记录到其它位置，这时候会导致相关索引的更新.磁盘操作更多，加上索引更新，会使得这样的操作比较慢.</p>
<p>insert-这是一个insert插入操作.</p>
<p>getmore-这是一个getmore 操作，getmore通常发生在结果集比较大的查询时，第一个query返回了部分结果，后续的结果是通过getmore来获取的。</p>
<p><strong>　　MongoDB 查询优化</strong></p>
<p>如果nscanned(扫描的记录数)远大于nreturned(返回结果的记录数)的话，那么我们就要考虑通过加索引来优化记录定位了。</p>
<p>reslen 如果过大，那么说明我们返回的结果集太大了，这时请查看find函数的第二个参数是否只写上了你需要的属性名。(类似 于MySQL中不要总是select *)</p>
<p>对于创建索引的建议是：如果很少读，那么尽量不要添加索引，因为索引越多，写操作会越慢。如果读量很大，那么创建索引还是比较划算的。(和RDBMS一样，貌似是废话 -_-!!)</p>
<p><strong>　　MongoDB 更新优化</strong></p>
<p>如果写查询量或者update量过大的话，多加索引是会有好处的。以及～～～～(省略N字，和RDBMS差不多的道理)</p>
<p>Use fast modify operations when possible (and usually with these, an index). See Updates.</p>
<p><strong>　　Profiler 的效率</strong></p>
<p>Profiling 功能肯定是会影响效率的，但是不太严重，原因是他使用的是system.profile 来记录，而system.profile 是一个capped collection 这种collection 在操作上有一些限制和特点，但是效率更高。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 客户端 MongoVue" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12984&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/12/25942526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 客户端 MongoVue</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mongodb亿级数据量的性能测试" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12878&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068564.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mongodb亿级数据量的性能测试</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb索引讲解与性能调优" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12873&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072358.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb索引讲解与性能调优</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/12898/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB 索引数据类型优化，节省60％内存</title>
		<link>http://blog.haohtml.com/archives/12868</link>
		<comments>http://blog.haohtml.com/archives/12868#comments</comments>
		<pubDate>Mon, 07 May 2012 03:43:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[nosql]]></category>
		<category><![CDATA[索引]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[数据库优化]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=12868</guid>
		<description><![CDATA[最近trunk.ly的工程师通过mongostat发现了大量的page fault，然后通过检查发现，他们的索引已经超出内存限制了（没有keep all index in RAM）。于是他们决定开始减小索引大小，通过测试得出了如下的数据，不同的数据类型的索引大小有2到3倍的差距。 虽然能够想像得到，但是直观的数据图可能让我们更深刻的认识到。他们的测试再一次告诉我们：给索引定一个好的数据结构是多么重要。 这是测试结果图，分别是用int、MongoDB的ObjectID、base64和md5的字符串做索引产生的索引大小： 测试过程也非常简单，首先用下面脚本将各种不同数据结构的数据写入到不同的collection里： #!/usr/bin/env python import pymongo import bson from pymongo import Connection db = connection.test_database print('ObjectID') for i in range(1, 1000000): db.objectids.insert({'i': i}) print('int') for i in range(1, 1000000): db.ints.insert({'_id': i, 'i': i}) print('Base64 BSON') for i in range(1, 1000000): db.base64s.insert({'_id': \ bson.Binary(hashlib.md5(str(i)).digest(), bson.binary.MD5_SUBTYPE), 'i': i}) print('string') for [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb索引讲解与性能调优" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12873&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072358.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb索引讲解与性能调优</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb的监控与性能优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12865&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072349.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb的监控与性能优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 客户端 MongoVue" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12984&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/12/25942526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 客户端 MongoVue</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>最近trunk.ly的工程师通过mongostat发现了大量的page fault，然后通过检查发现，他们的索引已经超出内存限制了（没有<a href="http://blog.boxedice.com/2010/12/13/mongodb-monitoring-keep-in-it-ram/">keep all index in RAM</a>）。于是他们决定开始减小索引大小，通过测试得出了如下的数据，不同的数据类型的索引大小有2到3倍的差距。</p>
<p>虽然能够想像得到，但是直观的数据图可能让我们更深刻的认识到。他们的测试再一次告诉我们：给索引定一个好的数据结构是多么重要。</p>
<p>这是测试结果图，分别是用int、MongoDB的ObjectID、base64和md5的字符串做索引产生的索引大小：</p>
<p><a href="http://blog.haohtml.com/wp-content/uploads/2012/05/mongodb_indexes_optimize.jpeg" rel="lightbox[12868]"><img class="aligncenter size-full wp-image-12871" title="mongodb_indexes_optimize" src="http://blog.haohtml.com/wp-content/uploads/2012/05/mongodb_indexes_optimize.jpeg" alt="" /></a></p>
<p>测试过程也非常简单，首先用下面脚本将各种不同数据结构的数据写入到不同的collection里：</p>
<div>
<pre>#!/usr/bin/env python

import pymongo
import bson
from pymongo import Connection

db = connection.test_database

print('ObjectID')
for i in range(1, 1000000):
    db.objectids.insert({'i': i})

print('int')
for i in range(1, 1000000):
    db.ints.insert({'_id': i, 'i': i})

print('Base64 BSON')
for i in range(1, 1000000):
    db.base64s.insert({'_id': \
        bson.Binary(hashlib.md5(str(i)).digest(),
        bson.binary.MD5_SUBTYPE), 'i': i})

print('string')
for i in range(1, 1000000):
    db.strings.insert({'_id': hashlib.md5(str(i)).digest(), 'i': i})</pre>
</div>
<p>然后获取每个collection的index大小，得到如下的结果，画成上面的图：</p>
<pre>&gt; db.base64s.stats()
{
        "totalIndexSize" : 67076096,
}
&gt; db.objectids.stats()
{
        "totalIndexSize" : 41598976,
}
&gt; db.ints.stats()
{
        "totalIndexSize" : 32522240,
}
&gt; db.strings.stats()
{
        "totalIndexSize" : 90914816,

}</pre>
<p>原文链接：<a href="http://notes.alexdong.com/choose-the-right-data-type-for-mongodb">How to save 200% RAM by selecting the right key data type for #MongoDB</a></p>
<p><strong>相关教程：</strong></p>
<p><a href="http://blog.haohtml.com/archives/12837" target="_blank">三招解决MongoDB的磁盘IO问题</a></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb索引讲解与性能调优" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12873&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072358.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb索引讲解与性能调优</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mongodb的监控与性能优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12865&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25072349.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mongodb的监控与性能优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 客户端 MongoVue" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12984&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/12/25942526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 客户端 MongoVue</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/12868/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL性能优化详解</title>
		<link>http://blog.haohtml.com/archives/5898</link>
		<comments>http://blog.haohtml.com/archives/5898#comments</comments>
		<pubDate>Thu, 30 Sep 2010 02:09:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=5898</guid>
		<description><![CDATA[MySQL数据库性能优化是本文的主要核心，将从数据库的优化设计，到具体的操作。好的优化能使服务器性能提升较大的空间，希望本文对大家有所帮助。 1. MySQL性能优化简介 在Web应用程序体系架构中，数据持久层(通常是一个关系数据库)是关键的核心部分，它对系统的性能有非常重要的影响。MySQL是目前使用最多的开源数据库，但是MySQL数据库的默认设置性能非常的差，仅仅是一个玩具数据库。因此在产品中使用MySQL数据库必须进行必要的优化。 优化是一个复杂的任务，本文描述MySQL相关的数据库设计和查询优化，服务器端优化，存储引擎优化。 2. 数据库设计和查询优化 在MySQL性能优化中，首先要考虑的就是Database Schema设计，这一点是非常重要的。一个糟糕的Schema设计即使在性能调优的MySQL Server上运行，也会表现出很差的性能;和Schema相似，查询语句的设计也会影响MySQL的性能，应该避免写出低效的SQL查询。这一节将详细讨论这两方面的优化。 2.1 Schema Design Schema的优化取决于将要运行什么样的query，不同的query会有不同的Schema优化方案。2.2节将介绍Query Design的优化。Schema设计同样受到预期数据集大小的影响。Schema设计时主要考虑：标准化，数据类型，索引。 2.1.1 标准化 标准化是在数据库中组织数据的过程。其中包括，根据设计规则创建表并在这些表间建立关系;通过取消冗余度与不一致相关性，该设计规则可以同时保护数据并提高数据的灵活性。通常数据库标准化是让数据库设计符合某一级别的范式，通常满足第三范式即可。也有第四范式(也称为 Boyce Codd范式，BCNF))与第五范式存在，但是在实际设计中很少考虑。忽视这些规则可能使得数据库的设计不太完美，但这不应影响功能。 标准化的特点： 1) 所有的“对象”都在它自己的table中，没有冗余。 2) 数据库通常由E-R图生成。 3) 简洁，更新属性通常只需要更新很少的记录。 4) Join操作比较耗时。 5) Select，sort优化措施比较少。 6) 适用于OLTP应用。 非标准化的特点： 1) 在一张表中存储很多数据，数据冗余。 2) 更新数据开销很大，更新一个属性可能会更新很多表，很多记录。 3) 在删除数据是有可能丢失数据。 4) Select，order有很多优化的选择。 5) 适用于DSS应用。 标准化和非标准化都有各自的优缺点，通常在一个数据库设计中可以混合使用，一部分表格标准化，一部分表格保留一些冗余数据： 1) 对OLTP使用标准化，对DSS使用非标准化 2) 使用物化视图。MySQL不直接支持该数据库特性，但是可以用MyISAM表代替。 3) 冗余一些数据在表格中，例如将ref_id和name存在同一张表中。但是要注意更新问题。 4) 对于一些简单的对象，直接使用value作为建。例如IP address等 5) [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql基础优化第一讲</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>MySQL数据库性能优化是本文的主要核心，将从数据库的优化设计，到具体的操作。好的优化能使服务器性能提升较大的空间，希望本文对大家有所帮助。</p>
<p><strong>1. MySQL性能优化简介</strong></p>
<p>在Web应用程序体系架构中，数据持久层(通常是一个关系数据库)是关键的核心部分，它对系统的性能有非常重要的影响。MySQL是目前使用最多的开源数据库，但是MySQL数据库的默认设置性能非常的差，仅仅是一个玩具数据库。因此在产品中使用MySQL数据库必须进行必要的优化。</p>
<p>优化是一个复杂的任务，本文描述MySQL相关的数据库设计和查询优化，服务器端优化，存储引擎优化。</p>
<p><strong> 2. 数据库设计和查询优化</strong></p>
<p>在MySQL性能优化中，首先要考虑的就是Database Schema设计，这一点是非常重要的。一个糟糕的Schema设计即使在性能调优的MySQL Server上运行，也会表现出很差的性能;和Schema相似，查询语句的设计也会影响MySQL的性能，应该避免写出低效的SQL查询。这一节将详细讨论这两方面的优化。</p>
<p>2.1 Schema Design<span id="more-5898"></span></p>
<p>Schema的优化取决于将要运行什么样的query，不同的query会有不同的Schema优化方案。2.2节将介绍Query Design的优化。Schema设计同样受到预期数据集大小的影响。Schema设计时主要考虑：标准化，数据类型，索引。</p>
<p>2.1.1 标准化</p>
<p>标准化是在数据库中组织数据的过程。其中包括，根据设计规则创建表并在这些表间建立关系;通过取消冗余度与不一致相关性，该设计规则可以同时保护数据并提高数据的灵活性。通常数据库标准化是让数据库设计符合某一级别的范式，通常满足第三范式即可。也有第四范式(也称为 Boyce Codd范式，BCNF))与第五范式存在，但是在实际设计中很少考虑。忽视这些规则可能使得数据库的设计不太完美，但这不应影响功能。</p>
<p>标准化的特点：</p>
<p>1) 所有的“对象”都在它自己的table中，没有冗余。</p>
<p>2) 数据库通常由E-R图生成。</p>
<p>3) 简洁，更新属性通常只需要更新很少的记录。</p>
<p>4) Join操作比较耗时。</p>
<p>5) Select，sort优化措施比较少。</p>
<p>6) 适用于OLTP应用。</p>
<p>非标准化的特点：</p>
<p>1) 在一张表中存储很多数据，数据冗余。</p>
<p>2) 更新数据开销很大，更新一个属性可能会更新很多表，很多记录。</p>
<p>3) 在删除数据是有可能丢失数据。</p>
<p>4) Select，order有很多优化的选择。</p>
<p>5) 适用于DSS应用。</p>
<p>标准化和非标准化都有各自的优缺点，通常在一个数据库设计中可以混合使用，一部分表格标准化，一部分表格保留一些冗余数据：</p>
<p>1) 对OLTP使用标准化，对DSS使用非标准化</p>
<p>2) 使用物化视图。MySQL不直接支持该数据库特性，但是可以用MyISAM表代替。</p>
<p>3) 冗余一些数据在表格中，例如将ref_id和name存在同一张表中。但是要注意更新问题。</p>
<p>4) 对于一些简单的对象，直接使用value作为建。例如IP address等</p>
<p>5) Reference by PRIMARY/UNIQUE KEY。MySQL可以优化这种操作，例如：</p>
<p>java 代码</p>
<p>select city_name from city,state where state_id=state.id and state.code=‘CA’” converted to “select city_name from city where state_id=12</p>
<p>2.1.2 数据类型</p>
<p>最基本的优化之一就是使表在磁盘上占据的空间尽可能小。这能带来性能非常大的提升，因为数据小，磁盘读入较快，并且在查询过程中表内容被处理所占用的内存更少。同时，在更小的列上建索引，索引也会占用更少的资源。</p>
<p>可以使用下面的技术可以使表的性能更好并且使存储空间最小：</p>
<p>1) 使用正确合适的类型，不要将数字存储为字符串。</p>
<p>2) 尽可能地使用最有效(最小)的数据类型。MySQL有很多节省磁盘空间和内存的专业化类型。</p>
<p>3) 尽可能使用较小的整数类型使表更小。例如，MEDIUMINT经常比INT好一些，因为MEDIUMINT列使用的空间要少25%。</p>
<p>4) 如果可能，声明列为NOT NULL。它使任何事情更快而且每列可以节省一位。注意如果在应用程序中确实需要NULL，应该毫无疑问使用它，只是避免 默认地在所有列上有它。</p>
<p>5) 对于MyISAM表，如果没有任何变长列(VARCHAR、TEXT或BLOB列)，使用固定尺寸的记录格式。这比较快但是不幸地可能会浪费一些空间。即使你已经用CREATE选项让VARCHAR列ROW_FORMAT=fixed，也可以提示想使用固定长度的行。</p>
<p>6) 使用sample character set，例如latin1。尽量少使用utf-8，因为utf-8占用的空间是latin1的3倍。可以在不需要使用utf-8的字段上面使用 latin1，例如mail，url等。</p>
<p>2.1.3 索引</p>
<p>所有MySQL列类型可以被索引。对相关列使用索引是提高SELECT操作性能的最佳途径。使用索引应该注意以下几点：</p>
<p>1) MySQL只会使用前缀，例如key(a, b) …where b=5 将使用不到索引。</p>
<p>2) 要选择性的使用索引。在变化很少的列上使用索引并不是很好，例如性别列。</p>
<p>3) 在Unique列上定义Unique index。</p>
<p>4) 避免建立使用不到的索引。</p>
<p>5) 在Btree index中(InnoDB使用Btree)，可以在需要排序的列上建立索引。</p>
<p>6) 避免重复的索引。</p>
<p>7) 避免在已有索引的前缀上建立索引。例如：如果存在index(a，b)则去掉index(a)。</p>
<p> <img src='http://blog.haohtml.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> 控制单个索引的长度。使用key(name(8))在数据的前面几个字符建立索引。</p>
<p>9) 越是短的键值越好，最好使用integer。</p>
<p>10) 在查询中要使用到索引(使用explain查看)，可以减少读磁盘的次数，加速读取数据。</p>
<p>11) 相近的键值比随机好。Auto_increment就比uuid好。</p>
<p>12) Optimize table可以压缩和排序index，注意不要频繁运行。</p>
<p>13) Analyze table可以更新数据。</p>
<p>2.2 Designing queries</p>
<p>查询语句的优化是一个Case by case的问题，不同的sql有不同的优化方案，在这里我只列出一些通用的技巧。</p>
<p>1) 在有index的情况下，尽量保证查询使用了正确的index。可以使用EXPLAIN select …查看结果，分析查询。</p>
<p>2) 查询时使用匹配的类型。例如select * from a where id=5， 如果这里id是字符类型，同时有index，这条查询则使用不到index，会做全表扫描，速度会很慢。正确的应该是 … where id=”5” ，加上引号表明类型是字符。</p>
<p>3) 使用--log-slow-queries –long-query-time=2查看查询比较慢的语句。然后使用explain分析查询，做出优化。</p>
<p><strong>3. 服务器端优化</strong></p>
<p>3.1 MySQL安装</p>
<p>MySQL有很多发行版本，最好使用MySQL AB发布的二进制版本。也可以下载源代码进行编译安装，但是编译器和类库的一些bug可能会使编译完成的MySQL存在潜在的问题。</p>
<p>如果安装 MySQL的服务器使用的是Intel公司的处理器，可以使用intel c++编译的版本，在Linux World2005的一篇PPT中提到，使用intel C++编译器编译的MySQL查询速度比正常版本快30%左右。Intel c++编译版本可以在MySQL官方网站下载。</p>
<p>3.2 服务器设置优化</p>
<p>MySQL默认的设置性能很差，所以要做一些参数的调整。这一节介绍一些通用的参数调整，不涉及具体的存储引擎(主要指MyISAM，InnoDB，相关优化在4中介绍)。</p>
<p>--character-set：如果是单一语言使用简单的character set例如latin1。尽量少用Utf-8，utf-8占用空间较多。</p>
<p>--memlock：锁定MySQL只能运行在内存中，避免 swapping，但是如果内存不够时有可能出现错误。</p>
<p>--max_allowed_packet：要足够大，以适应比较大的SQL查询，对性能没有太大影响，主要是避免出现packet错误。</p>
<p>--max_connections：server允许的最大连接。太大的话会出现out of memory。</p>
<p>--table_cache：MySQL在同一时间保持打开的table的数量。打开table开销比较大。一般设置为512。</p>
<p>--query_cache_size： 用于缓存查询的内存大小。</p>
<p>--datadir：mysql存放数据的根目录，和安装文件分开在不同的磁盘可以提高一点性能。</p>
<p><strong>4. 存储引擎优化</strong></p>
<p>MySQL支持不同的存储引擎，主要使用的有MyISAM和InnoDB。</p>
<p>4.1 MyISAM</p>
<p>MyISAM管理非事务表。它提供高速存储和检索，以及全文搜索能力。MyISAM在所有MySQL配置里被支持，它是默认的存储引擎，除非配置MySQL默认使用另外一个引擎。</p>
<p>4.1.1 MyISAM特性</p>
<p>4.1.1.1 MyISAM Properties</p>
<p>1) 不支持事务，宕机会破坏表</p>
<p>2) 使用较小的内存和磁盘空间</p>
<p>3) 基于表的锁，并发更新数据会出现严重性能问题</p>
<p>4) MySQL只缓存Index，数据由OS缓存</p>
<p>4.1.1.2 Typical MyISAM usages</p>
<p>1) 日志系统</p>
<p>2) 只读或者绝大部分是读操作的应用</p>
<p>3) 全表扫描</p>
<p>4) 批量导入数据</p>
<p>5) 没有事务的低并发读/写</p>
<p>4.1.2 MyISAM优化要点</p>
<p>1) 声明列为NOT NULL，可以减少磁盘存储。</p>
<p>2) 使用optimize table做碎片整理，回收空闲空间。注意仅仅在非常大的数据变化后运行。</p>
<p>3) Deleting/updating/adding大量数据的时候禁止使用index。使用ALTER TABLE t DISABLE KEYS。</p>
<p>4) 设置myisam_max_[extra]_sort_file_size足够大，可以显著提高repair table的速度。</p>
<p>4.1.3 MyISAM Table Locks</p>
<p>1) 避免并发insert，update。</p>
<p>2) 可以使用insert delayed，但是有可能丢失数据。</p>
<p>3) 优化查询语句。</p>
<p>4) 水平分区。</p>
<p>5) 垂直分区。</p>
<p>6) 如果都不起作用，使用InnoDB。</p>
<p>4.1.4 MyISAM Key Cache</p>
<p>1) 设置key_buffer_size variable。MyISAN最主要的cache设置，用于缓存MyISAM表格的index数据，该参数只对MyISAM有影响。通常在只使用 MyISAM的Server中设置25-33%的内存大小。</p>
<p>2) 可以使用几个不同的Key Caches(对一些hot data)。</p>
<p>a) SET GLOBAL test.key_buffer_size=512*1024;</p>
<p>b) CACHE INDEX t1.i1, t2.i1, t3 IN test;</p>
<p>2) Preload index到Cache中可以提高查询速度。因为preloading index是顺序的，所以非常快。</p>
<p>a) LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES;</p>
<p>4.2 InnoDB</p>
<p>InnoDB 给MySQL提供了具有提交，回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎。InnoDB提供row level lock，并且也在SELECT语句提供一个Oracle风格一致的非锁定读。这些特色增加了多用户部署和性能。没有在InnoDB中扩大锁定的需要，因为在InnoDB中row level lock适合非常小的空间。InnoDB也支持FOREIGN KEY约束。在SQL查询中，你可以自由地将InnoDB类型的表与其它MySQL的表的类型混合起来，甚至在同一个查询中也可以混合。</p>
<p>InnoDB 是为在处理巨大数据量时获得最大性能而设计的。它的CPU使用效率非常高。</p>
<p>InnoDB存储引擎已经完全与MySQL服务器整合，InnoDB存储引擎为在内存中缓存数据和索引而维持它自己的缓冲池。 InnoDB存储它的表&amp;索引在一个表空间中，表空间可以包含数个文件(或原始磁盘分区)。这与MyISAM表不同，比如在MyISAM表中每个表被存在分离的文件中。InnoDB 表可以是任何大小，即使在文件尺寸被限制为2GB的操作系统上。</p>
<p>许多需要高性能的大型数据库站点上使用了 InnoDB引擎。著名的Internet新闻站点Slashdot.org运行在InnoDB上。 Mytrix, Inc.在InnoDB上存储超过1TB的数据，还有一些其它站点在InnoDB上处理平均每秒800次插入/更新的负荷。</p>
<p>4.2.1 InnoDB特性</p>
<p>4.2.1.1 InnoDB Properties</p>
<p>1) 支持事务，ACID，外键。</p>
<p>2) Row level locks。</p>
<p>3) 支持不同的隔离级别。</p>
<p>4) 和MyISAM相比需要较多的内存和磁盘空间。</p>
<p>5) 没有键压缩。</p>
<p>6) 数据和索引都缓存在内存hash表中。</p>
<p>4.2.1.2 InnoDB Good For</p>
<p>1) 需要事务的应用。</p>
<p>2) 高并发的应用。</p>
<p>3) 自动恢复。</p>
<p>4) 较快速的基于主键的操作。</p>
<p>4.2.2 InnoDB优化要点</p>
<p>1) 尽量使用short，integer的主键。</p>
<p>2) Load/Insert数据时按主键顺序。如果数据没有按主键排序，先排序然后再进行数据库操作。</p>
<p>3) 在Load数据是为设置SET UNIQUE_CHECKS=0，SET FOREIGN_KEY_CHECKS=0，可以避免外键和唯一性约束检查的开销。</p>
<p>4) 使用prefix keys。因为InnoDB没有key压缩功能。</p>
<p>4.2.3 InnoDB服务器端设定</p>
<p>innodb_buffer_pool_size：这是InnoDB最重要的设置，对InnoDB性能有决定性的影响。默认的设置只有8M，所以默认的数据库设置下面InnoDB性能很差。在只有InnoDB存储引擎的数据库服务器上面，可以设置60-80%的内存。更精确一点，在内存容量允许的情况下面设置比InnoDB tablespaces大10%的内存大小。</p>
<p>innodb_data_file_path：指定表数据和索引存储的空间，可以是一个或者多个文件。最后一个数据文件必须是自动扩充的，也只有最后一个文件允许自动扩充。这样，当空间用完后，自动扩充数据文件就会自动增长(以8MB为单位)以容纳额外的数据。例如： innodb_data_file_path=/disk1/ibdata1:900M;/disk2/ibdata2:50M:autoextend两个数据文件放在不同的磁盘上。数据首先放在ibdata1中，当达到900M以后，数据就放在ibdata2中。一旦达到50MB，ibdata2将以 8MB为单位自动增长。如果磁盘满了，需要在另外的磁盘上面增加一个数据文件。</p>
<p>innodb_autoextend_increment: 默认是8M, 如果一次insert数据量比较多的话, 可以适当增加.</p>
<p>innodb_data_home_dir：放置表空间数据的目录，默认在mysql的数据目录，设置到和MySQL安装文件不同的分区可以提高性能。</p>
<p>innodb_log_file_size：该参数决定了recovery speed。太大的话recovery就会比较慢，太小了影响查询性能，一般取256M可以兼顾性能和recovery的速度。</p>
<p>innodb_log_buffer_size：磁盘速度是很慢的，直接将log写道磁盘会影响InnoDB的性能，该参数设定了log buffer的大小，一般4M。如果有大的blob操作，可以适当增大。</p>
<p>innodb_flush_logs_at_trx_commit=2： 该参数设定了事务提交时内存中log信息的处理。</p>
<p>1) =1时，在每个事务提交时，日志缓冲被写到日志文件，对日志文件做到磁盘操作的刷新。Truly ACID。速度慢。</p>
<p>2) =2时，在每个事务提交时，日志缓冲被写到文件，但不对日志文件做到磁盘操作的刷新。只有操作系统崩溃或掉电才会删除最后一秒的事务，不然不会丢失事务。</p>
<p>3) =0时， 日志缓冲每秒一次地被写到日志文件，并且对日志文件做到磁盘操作的刷新。任何mysqld进程的崩溃会删除崩溃前最后一秒的事务</p>
<p>innodb_file_per_table：可以存储每个InnoDB表和它的索引在它自己的文件中。</p>
<p>transaction-isolation=READ-COMITTED: 如果应用程序可以运行在READ-COMMITED隔离级别，做此设定会有一定的性能提升。</p>
<p>innodb_flush_method： 设置InnoDB同步IO的方式：</p>
<p>1) Default – 使用fsync()。</p>
<p>2) O_SYNC 以sync模式打开文件，通常比较慢。</p>
<p>3) O_DIRECT，在Linux上使用Direct IO。可以显著提高速度，特别是在RAID系统上。避免额外的数据复制和double buffering(mysql buffering 和OS buffering)。</p>
<p>innodb_thread_concurrency： InnoDB kernel最大的线程数。</p>
<p>1) 最少设置为(num_disks+num_cpus)*2。</p>
<p>2) 可以通过设置成1000来禁止这个限制</p>
<p><strong> 5. 缓存</strong></p>
<p>缓存有很多种，为应用程序加上适当的缓存策略会显著提高应用程序的性能。由于应用缓存是一个比较大的话题，所以这一部分还需要进一步调研。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql基础优化第一讲</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/5898/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tokyo Cabinet Key-Value数据库及其扩展应用PPT[原创]</title>
		<link>http://blog.haohtml.com/archives/3273</link>
		<comments>http://blog.haohtml.com/archives/3273#comments</comments>
		<pubDate>Sun, 04 Apr 2010 03:52:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[数据库类]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=3273</guid>
		<description><![CDATA[以下是我4月2日在“2010年数据库技术大会”的演讲PPT，介绍了 Tokyo Cabinet Key-Value 数据库的性能优化关键参数，以及金山逍遥网在Tokyo Cabinet基础上实现的TCSQL实时列表缓存数据库(Version 1.3)。 点击下载dtcc_tc_tt_tcsql.pdf<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql基础优化第一讲</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">根据status信息对MySQL服务器进行优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>以下是我4月2日在“<a style="text-decoration: none; color: #4f6371;" href="http://dtcc.it168.com/" target="_blank"><span style="color: blue;"><span style="text-decoration: underline;">2010年数据库技术大会</span></span></a>”的演讲PPT，介绍了 Tokyo Cabinet Key-Value 数据库的性能优化关键参数，以及金山逍遥网在Tokyo Cabinet基础上实现的TCSQL实时列表缓存数据库(Version 1.3)。</p>
<p><a href="/wp-content/uploads/2010/04/dtcc_tc_tt_tcsql.pdf">点击下载dtcc_tc_tt_tcsql.pdf</a></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql基础优化第一讲</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3273">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">根据status信息对MySQL服务器进行优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/3273/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL优化篇-查询优化</title>
		<link>http://blog.haohtml.com/archives/2821</link>
		<comments>http://blog.haohtml.com/archives/2821#comments</comments>
		<pubDate>Fri, 08 Jan 2010 05:51:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=2821</guid>
		<description><![CDATA[可以参考一下官方文档中的解释。 http://dev.mysql.com/doc/refman/5.1/zh/optimization.html 7. 优化 7.1. 优化概述 7.1.1. MySQL设计局限与折衷 7.1.2. 为可移植性设计应用程序 7.1.3. 我们已将MySQL用在何处？ 7.1.4. MySQL基准套件 7.1.5. 使用自己的基准 7.2. 优化SELECT语句和其它查询 7.2.1. EXPLAIN语法（获取SELECT相关信息） 7.2.2. 估计查询性能 7.2.3. SELECT查询的速度 7.2.4. MySQL怎样优化WHERE子句 7.2.5. 范围优化 7.2.6. 索引合并优化 7.2.7. MySQL如何优化IS NULL 7.2.8. MySQL如何优化DISTINCT 7.2.9. MySQL如何优化LEFT JOIN和RIGHT JOIN 7.2.10. MySQL如何优化嵌套Join 7.2.11. MySQL如何简化外部联合 7.2.12. MySQL如何优化ORDER BY 7.2.13. MySQL如何优化GROUP BY 7.2.14. MySQL如何优化LIMIT 7.2.15. 如何避免表扫描 7.2.16. INSERT语句的速度 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL分区技术 分享" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL分区技术 分享</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<div>可以参考一下官方文档中的解释。</p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/zh/optimization.html" target="_blank">http://dev.mysql.com/doc/refman/5.1/zh/optimization.html</a></p>
<p>7. 优化<br />
7.1. 优化概述<br />
7.1.1. MySQL设计局限与折衷<br />
7.1.2. 为可移植性设计应用程序<br />
7.1.3. 我们已将MySQL用在何处？<br />
7.1.4. MySQL基准套件<br />
7.1.5. 使用自己的基准<br />
7.2.  优化SELECT语句和其它查询<br />
7.2.1. EXPLAIN语法（获取SELECT相关信息）<br />
7.2.2. 估计查询性能<br />
7.2.3.  SELECT查询的速度<br />
7.2.4. MySQL怎样优化WHERE子句<br />
7.2.5. 范围优化<br />
7.2.6. 索引合并优化<br />
7.2.7. MySQL如何优化IS NULL<br />
7.2.8. MySQL如何优化DISTINCT<br />
7.2.9.  MySQL如何优化LEFT JOIN和RIGHT JOIN<br />
7.2.10. MySQL如何优化嵌套Join<br />
7.2.11.  MySQL如何简化外部联合<br />
7.2.12. MySQL如何优化ORDER BY<br />
7.2.13. MySQL如何优化GROUP BY<br />
7.2.14. MySQL如何优化LIMIT<br />
7.2.15. 如何避免表扫描<br />
7.2.16. INSERT语句的速度<br />
7.2.17. UPDATE语句的速度<br />
7.2.18. DELETE语句的速度<br />
7.2.19. 其它优化技巧</p></div>
<p><!--End_rbody_62004818//--></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL分区技术 分享" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL分区技术 分享</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/2821/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL分区技术 分享</title>
		<link>http://blog.haohtml.com/archives/2783</link>
		<comments>http://blog.haohtml.com/archives/2783#comments</comments>
		<pubDate>Mon, 04 Jan 2010 05:57:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[分区]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/index.php/archives/2783</guid>
		<description><![CDATA[通过分区（Partition）提升MySQL性能
——MySQL5.1新特性翻译系列
几年前，俺写过一篇题为“The Foundation of Excellent Performance”的文章(现在仍然可以在http://www.tdan.com/i016fe03.htm看到)，俺对SQL语句是影响数据库驱动系统性能的第一要素的观点有点质疑。其实在那时我在文章中就坚信数据库的物理设计在对高级数据库的性能影响上远比其他因素重要。同时俺还给大家看了Oracle的研究，他们解释了为什么拙劣的物理设计是数据库停机（无论是有计划的还是没计划的）背后的主要原因。这么多年都过来啦（幸好没多少人朝俺扔砖头），俺的观点是改变了一些，但在这点上俺还是坚持DBA如果想要高性能的数据库就必须在数据库的物理设计上多思考的观点，这样才能减少响应时间使终端用户满意而不是引来骂声一片。（陈朋奕语：不要那么严肃，嘿嘿）
俺今天这么激动又想写文章的原因是MySQL5.1的发布带来了设计超强动力数据库的强有力的武器，任何MySQL的DBA都应该尽快学习并使用它。俺觉得如果能很好滴使用这个5.1版带来的新特性，DBA可以使自己管理的VLDB（不知道什么是VLDB？告诉你，是好大好大的数据库的意思，Very Large DB）或数据仓库奇迹般的获得巨大的性能提升。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql中出现的＂MySQL Got error 139 from storage engine＂的原因" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12886&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql中出现的＂MySQL Got error 139 from storage engine＂的原因</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>通过分区（Partition）提升MySQL性能<br />
——MySQL5.1新特性翻译系列<br />
几年前，俺写过一篇题为“The Foundation of Excellent Performance”的文章(现在仍然可以在http://www.tdan.com/i016fe03.htm看到)，俺对SQL语句是影响数据库驱动系统性能的第一要素的观点有点质疑。其实在那时我在文章中就坚信数据库的物理设计在对高级数据库的性能影响上远比其他因素重要。同时俺还给大家看了Oracle的研究，他们解释了为什么拙劣的物理设计是数据库停机（无论是有计划的还是没计划的）背后的主要原因。这么多年都过来啦（幸好没多少人朝俺扔砖头），俺的观点是改变了一些，但在这点上俺还是坚持DBA如果想要高性能的数据库就必须在数据库的物理设计上多思考的观点，这样才能减少响应时间使终端用户满意而不是引来骂声一片。（陈朋奕语：不要那么严肃，嘿嘿）<br />
俺今天这么激动又想写文章的原因是MySQL5.1的发布带来了设计超强动力数据库的强有力的武器，任何MySQL的DBA都应该尽快学习并使用它。俺觉得如果能很好滴使用这个5.1版带来的新特性，DBA可以使自己管理的VLDB（不知道什么是VLDB？告诉你，是好大好大的数据库的意思，Very Large DB）或数据仓库奇迹般的获得巨大的性能提升。</p>
<p>什么是数据库分区？<br />
数据库分区是一种物理数据库设计技术，DBA和数据库建模人员对其相当熟悉。虽然分区技术可以实现很多效果，但其主要目的是为了在特定的SQL操作中减少数据读写的总量以缩减响应时间。<br />
分区主要有两种形式：//这里一定要注意行和列的概念（row是行，column是列）<br />
水平分区（Horizontal Partitioning） 这种形式分区是对表的行进行分区，通过这样的方式不同分组里面的物理列分割的数据集得以组合，从而进行个体分割（单分区）或集体分割（1个或多个分区）。所有在表中定义的列在每个数据集中都能找到，所以表的特性依然得以保持。<br />
举个简单例子：一个包含十年发票记录的表可以被分区为十个不同的分区，每个分区包含的是其中一年的记录。（朋奕注：这里具体使用的分区方式我们后面再说，可以先说一点，一定要通过某个属性列来分割，譬如这里使用的列就是年份） 垂直分区（Vertical Partitioning） 这种分区方式一般来说是通过对表的垂直划分来减少目标表的宽度，使某些特定的列被划分到特定的分区，每个分区都包含了其中的列所对应的行。<br />
举个简单例子：一个包含了大text和BLOB列的表，这些text和BLOB列又不经常被访问，这时候就要把这些不经常使用的text和BLOB了划分到另一个分区，在保证它们数据相关性的同时还能提高访问速度。</p>
<p>在数据库供应商开始在他们的数据库引擎中建立分区（主要是水平分区）时，DBA和建模者必须设计好表的物理分区结构，不要保存冗余的数据（不同表中同时都包含父表中的数据）或相互联结成一个逻辑父对象（通常是视图）。这种做法会使水平分区的大部分功能失效，有时候也会对垂直分区产生影响。</p>
<p>在MySQL 5.1中进行分区<br />
     MySQL5.1中最激动人心的新特性应该就是对水平分区的支持了。这对MySQL的使用者来说确实是个好消息，而且她已经支持分区大部分模式：<br />
         Range（范围） – 这种模式允许DBA将数据划分不同范围。例如DBA可以将一个表通过年份划分成三个分区，80年代（1980's）的数据，90年代（1990's）的数据以及任何在2000年（包括2000年）后的数据。<br />
         Hash（哈希） – 这中模式允许DBA通过对表的一个或多个列的Hash Key进行计算，最后通过这个Hash码不同数值对应的数据区域进行分区，。例如DBA可以建立一个对表主键进行分区的表。<br />
         Key（键值） – 上面Hash模式的一种延伸，这里的Hash Key是MySQL系统产生的。<br />
         List（预定义列表） – 这种模式允许系统通过DBA定义的列表的值所对应的行数据进行分割。例如：DBA建立了一个横跨三个分区的表，分别根据2004年2005年和2006年值所对应的数据。<br />
         Composite（复合模式） - 很神秘吧，哈哈，其实是以上模式的组合使用而已，就不解释了。举例：在初始化已经进行了Range范围分区的表上，我们可以对其中一个分区再进行hash哈希分区。</p>
<p>    分区带来的好处太多太多了，有多少？俺也不知道，自己猜去吧，要是觉得没有多少就别用，反正俺也不求你用。不过在这里俺强调两点好处：</p>
<p>性能的提升（Increased performance） - 在扫描操作中，如果MySQL的优化器知道哪个分区中才包含特定查询中需要的数据，它就能直接去扫描那些分区的数据，而不用浪费很多时间扫描不需要的地方了。需要举个例子？好啊，百万行的表划分为10个分区，每个分区就包含十万行数据，那么查询分区需要的时间仅仅是全表扫描的十分之一了，很明显的对比。同时对十万行的表建立索引的速度也会比百万行的快得多得多。如果你能把这些分区建立在不同的磁盘上，这时候的I/O读写速度就“不堪设想”（没用错词，真的太快了，理论上100倍的速度提升啊，这是多么快的响应速度啊，所以有点不堪设想了）了。<br />
对数据管理的简化（Simplified data management） - 分区技术可以让DBA对数据的管理能力提升。通过优良的分区，DBA可以简化特定数据操作的执行方式。例如：DBA在对某些分区的内容进行删除的同时能保证余下的分区的数据完整性(这是跟对表的数据删除这种大动作做比较的)。<br />
此外分区是由MySQL系统直接管理的，DBA不需要手工的去划分和维护。例如：这个例如没意思，不讲了，如果你是DBA，只要你划分了分区，以后你就不用管了就是了。</p>
<p>站在性能设计的观点上，俺们对以上的内容也是相当感兴趣滴。通过使用分区和对不同的SQL操作的匹配设计，数据库的性能一定能获得巨大提升。下面咱们一起用用这个MySQL 5.1的新功能看看。<br />
下面所有的测试都在Dell Optiplex box with a Pentium 4 3.00GHz processor, 1GB of RAM机器上（炫耀啊……），Fedora Core 4和MySQL 5.1.6 alpha上运行通过。</p>
<p>如何进行实际分区<br />
看看分区的实际效果吧。我们建立几个同样的MyISAM引擎的表，包含日期敏感的数据，但只对其中一个分区。分区的表（表名为part_tab）我们采用Range范围分区模式，通过年份进行分区：<br />
mysql> CREATE TABLE part_tab<br />
    ->      ( c1 int default NULL,<br />
    -> c2 varchar(30) default NULL,<br />
    -> c3 date default NULL<br />
    -><br />
    ->      ) engine=myisam<br />
    ->      PARTITION BY RANGE (year(c3)) (PARTITION p0 VALUES LESS THAN (1995),<br />
    ->      PARTITION p1 VALUES LESS THAN (1996) , PARTITION p2 VALUES LESS THAN (1997) ,<br />
    ->      PARTITION p3 VALUES LESS THAN (1998) , PARTITION p4 VALUES LESS THAN (1999) ,<br />
    ->      PARTITION p5 VALUES LESS THAN (2000) , PARTITION p6 VALUES LESS THAN (2001) ,<br />
    ->      PARTITION p7 VALUES LESS THAN (2002) , PARTITION p8 VALUES LESS THAN (2003) ,<br />
    ->      PARTITION p9 VALUES LESS THAN (2004) , PARTITION p10 VALUES LESS THAN (2010),<br />
    ->      PARTITION p11 VALUES LESS THAN MAXVALUE );<br />
Query OK, 0 rows affected (0.00 sec)<br />
注意到了这里的最后一行吗？这里把不属于前面年度划分的年份范围都包含了，这样才能保证数据不会出错，大家以后要记住啊，不然数据库无缘无故出错你就爽了。那下面我们建立没有分区的表（表名为no_part_tab）：<br />
mysql> create table no_part_tab<br />
    -> (c1 int(11) default NULL,<br />
    -> c2 varchar(30) default NULL,<br />
    -> c3 date default NULL) engine=myisam;<br />
Query OK, 0 rows affected (0.02 sec)<br />
下面咱写一个存储过程（感谢Peter Gulutzan给的代码，如果大家需要Peter Gulutzan的存储过程教程的中文翻译也可以跟我要，chenpengyi◎gmail.com），它能向咱刚才建立的已分区的表中平均的向每个分区插入共8百万条不同的数据。填满后，咱就给没分区的克隆表中插入相同的数据：<br />
mysql> delimiter //<br />
mysql> CREATE PROCEDURE load_part_tab()<br />
    -> begin<br />
    -> declare v int default 0;<br />
    ->          while v < 8000000<br />
    -> do<br />
    -> insert into part_tab<br />
    -> values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));<br />
    -> set v = v + 1;<br />
    -> end while;<br />
    -> end<br />
    -> //<br />
Query OK, 0 rows affected (0.00 sec)<br />
mysql> delimiter ;<br />
mysql> call load_part_tab();<br />
Query OK, 1 row affected (8 min 17.75 sec)<br />
mysql> insert into no_part_tab select * from part_tab;<br />
Query OK, 8000000 rows affected (51.59 sec)<br />
Records: 8000000 Duplicates: 0 Warnings: 0</p>
<p>表都准备好了。咱开始对这两表中的数据进行简单的范围查询吧。先分区了的，后没分区的，跟着有执行过程解析（MySQL Explain命令解析器），可以看到MySQL做了什么：<br />
mysql> select count(*) from no_part_tab where<br />
    -> c3 > date '1995-01-01' and c3 < date '1995-12-31';<br />
+----------+<br />
| count(*) |<br />
+----------+<br />
|   795181 |<br />
+----------+<br />
1 row in set (38.30 sec)</p>
<p>mysql> select count(*) from part_tab where<br />
    -> c3 > date '1995-01-01' and c3 < date '1995-12-31';<br />
+----------+<br />
| count(*) |<br />
+----------+<br />
|   795181 |<br />
+----------+<br />
1 row in set (3.88 sec)</p>
<p>mysql> explain select count(*) from no_part_tab where<br />
    -> c3 > date '1995-01-01' and c3 < date '1995-12-31'\G<br />
*************************** 1. row ***************************<br />
           id: 1<br />
select_type: SIMPLE<br />
        table: no_part_tab<br />
         type: ALL<br />
possible_keys: NULL<br />
          key: NULL<br />
      key_len: NULL<br />
          ref: NULL<br />
         rows: 8000000<br />
        Extra: Using where<br />
1 row in set (0.00 sec)</p>
<p>mysql> explain partitions select count(*) from part_tab where<br />
    -> c3 > date '1995-01-01' and c3 < date '1995-12-31'\G<br />
*************************** 1. row ***************************<br />
           id: 1<br />
select_type: SIMPLE<br />
        table: part_tab<br />
   partitions: p1<br />
         type: ALL<br />
possible_keys: NULL<br />
          key: NULL<br />
      key_len: NULL<br />
          ref: NULL<br />
         rows: 798458<br />
        Extra: Using where<br />
1 row in set (0.00 sec)<br />
从上面结果可以容易看出，设计恰当表分区能比非分区的减少90％的响应时间。而命令解析Explain程序也告诉我们在对已分区的表的查询过程中仅对第一个分区进行了扫描，其他都跳过了。<br />
哔厉吧拉，说阿说……反正就是这个分区功能对DBA很有用拉，特别对VLDB和需要快速反应的系统。</p>
<p>对Vertical Partitioning的一些看法<br />
虽然MySQL 5.1自动实现了水平分区，但在设计数据库的时候不要轻视垂直分区。虽然要手工去实现垂直分区，但在特定场合下你会收益不少的。例如在前面建立的表中，VARCHAR字段是你平常很少引用的，那么对它进行垂直分区会不会提升速度呢？咱们看看测试结果：<br />
mysql> desc part_tab;<br />
+-------+-------------+------+-----+---------+-------+<br />
| Field | Type        | Null | Key | Default | Extra |<br />
+-------+-------------+------+-----+---------+-------+<br />
| c1    | int(11)     | YES |     | NULL    |       |<br />
| c2    | varchar(30) | YES |     | NULL    |       |<br />
| c3    | date        | YES |     | NULL    |       |<br />
+-------+-------------+------+-----+---------+-------+<br />
3 rows in set (0.03 sec)</p>
<p>mysql> alter table part_tab drop column c2;<br />
Query OK, 8000000 rows affected (42.20 sec)<br />
Records: 8000000 Duplicates: 0 Warnings: 0</p>
<p>mysql> desc part_tab;<br />
+-------+---------+------+-----+---------+-------+<br />
| Field | Type    | Null | Key | Default | Extra |<br />
+-------+---------+------+-----+---------+-------+<br />
| c1    | int(11) | YES |     | NULL    |       |<br />
| c3    | date    | YES |     | NULL    |       |<br />
+-------+---------+------+-----+---------+-------+<br />
2 rows in set (0.00 sec)</p>
<p>mysql> select count(*) from part_tab where<br />
    -> c3 > date '1995-01-01' and c3 < date '1995-12-31';<br />
+----------+<br />
| count(*) |<br />
+----------+<br />
|   795181 |<br />
+----------+<br />
1 row in set (0.34 sec)<br />
在设计上去掉了VARCHAR字段后，不止是你，俺也发现查询响应速度上获得了另一个90％的时间节省。所以大家在设计表的时候，一定要考虑，表中的字段是否真正关联，又是否在你的查询中有用？</p>
<p>补充说明</p>
<p>这么简单的文章肯定不能说全MySQL 5.1 分区机制的所有好处和要点（虽然对自己写文章水平很有信心），下面就说几个感兴趣的：<br />
支持所有存储引擎(MyISAM, Archive, InnoDB, 等等) 对分区的表支持索引，包括本地索引local indexes，对其进行的是一对一的视图镜像，假设一个表有十个分区，那么其本地索引也包含十个分区。 关于分区的元数据Metadata的表可以在INFORMATION_SCHEMA数据库中找到，表名为PARTITIONS。 All SHOW 命令支持返回分区表以及元数据的索引。 对其操作的命令和实现的维护功能有（比对全表的操作还多）： ADD PARTITION DROP PARTITION COALESCE PARTITION REORGANIZE PARTITION ANALYZE PARTITION CHECK PARTITION OPTIMIZE PARTITION REBUILD PARTITION REPAIR PARTITION</p>
<p>站在性能主导的观点上来说，MySQL 5.1的分区功能能给数据性能带来巨大的提升的同时减轻DBA的管理负担，如果分区合理的话。如果需要更多的资料可以去http://dev.mysql.com/doc/refman/5.1/en/partitioning.html或 http://forums.mysql.com/list.php?106获得相关资料。<br />
关于MySQL分区的使用方法很快发布上来，这里有什么错误欢迎指出，或给我来信<br />
——2006-05-05陈朋奕</p>
<p>(http://www.fanqiang.com)</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mysql中出现的＂MySQL Got error 139 from storage engine＂的原因" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12886&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mysql中出现的＂MySQL Got error 139 from storage engine＂的原因</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/2783/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SHOW INDEX语法 查看索引状态</title>
		<link>http://blog.haohtml.com/archives/1889</link>
		<comments>http://blog.haohtml.com/archives/1889#comments</comments>
		<pubDate>Fri, 19 Jun 2009 03:16:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/index.php/archives/1889</guid>
		<description><![CDATA[SHOW INDEX FROM tbl_name [FROM db_name]

　　SHOW INDEX会返回表索引信息。其格式与ODBC中的SQLStatistics调用相似。

　　SHOW INDEX会返回以下字段：

　　· Table

　　表的名称。

　　· Non_unique

　　如果索引不能包括重复词，则为0。如果可以，则为1。

　　· Key_name

　　索引的名称。

　　· Seq_in_index

　　索引中的列序列号，从1开始。

　　· Column_name

　　列名称。

　　· Collation

　　列以什么方式存储在索引中。在MySQL中，有值‘A’（升序）或NULL（无分类）。

　　· Cardinality

　　索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数，所以即使对于小型表，该值也没有必要是精确的。基数越大，当进行联合时，MySQL使用该索引的机会就越大。

　　· Sub_part

　　如果列只是被部分地编入索引，则为被编入索引的字符的数目。如果整列被编入索引，则为NULL。

　　· Packed

　　指示关键字如何被压缩。如果没有被压缩，则为NULL。

　　· Null

　　如果列含有NULL，则含有YES。如果没有，则该列含有NO。

　　· Index_type

　　用过的索引方法（BTREE, FULLTEXT, HASH, RTREE）。

　　· Comment

　　多种评注。

　　您可以使用db_name.tbl_name作为tbl_name FROM db_name语法的另一种形式。这两个语句是等价的：

　　mysql> SHOW INDEX FROM mytable FROM mydb;

　　mysql> SHOW INDEX FROM mydb.mytable;

　　SHOW KEYS是SHOW INDEX的同义词。您也可以使用mysqlshow -k db_name tbl_name命令列举一个表的索引。

　　SHOW INNODB STATUS语法

　　SHOW INNODB STATUS

　　在MySQL 5.1中，这是SHOW ENGINE INNODB STATUS的同义词，但不赞成使用。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MySQL 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql优化数据库对象" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql优化数据库对象</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>SHOW INDEX FROM tbl_name [FROM db_name]</p>
<p>SHOW INDEX会返回表索引信息。其格式与ODBC中的SQLStatistics调用相似。</p>
<p>SHOW INDEX会返回以下字段：</p>
<p><strong>· Table</strong></p>
<p>表的名称。</p>
<p><strong>· Non_unique</strong></p>
<p>如果索引不能包括重复词，则为0。如果可以，则为1。</p>
<p><strong>· Key_name</strong></p>
<p>索引的名称。</p>
<p><strong>· Seq_in_index</strong></p>
<p>索引中的列序列号，从1开始。</p>
<p><strong>· Column_name</strong></p>
<p>列名称。</p>
<p><strong>· Collation</strong></p>
<p>列以什么方式存储在索引中。在MySQL中，有值‘A’（升序）或NULL（无分类）。</p>
<p><strong>· Cardinality</strong></p>
<p>索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数，所以即使对于小型表，该值也没有必要是精确的。基数越大，当进行联合时，MySQL使用该索引的机会就越大。</p>
<p><strong>· Sub_part</strong></p>
<p>如果列只是被部分地编入索引，则为被编入索引的字符的数目。如果整列被编入索引，则为NULL。</p>
<p><strong>· Packed</strong></p>
<p>指示关键字如何被压缩。如果没有被压缩，则为NULL。</p>
<p><strong>· Null</strong></p>
<p>如果列含有NULL，则含有YES。如果没有，则该列含有NO。</p>
<p><strong>· Index_type</strong></p>
<p>用过的索引方法（BTREE, FULLTEXT, HASH, RTREE）。</p>
<p><strong>· Comment</strong></p>
<p>多种评注。</p>
<p>您可以使用db_name.tbl_name作为tbl_name FROM db_name语法的另一种形式。这两个语句是等价的：</p>
<p>mysql&gt; SHOW INDEX FROM mytable FROM mydb;</p>
<p>mysql&gt; SHOW INDEX FROM mydb.mytable;</p>
<p>SHOW KEYS是SHOW INDEX的同义词。您也可以使用mysqlshow -k db_name tbl_name命令列举一个表的索引。</p>
<p>SHOW INNODB STATUS语法</p>
<p>SHOW INNODB STATUS</p>
<p>在MySQL 5.1中，这是SHOW ENGINE INNODB STATUS的同义词，但不赞成使用。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="MySQL 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql优化数据库对象" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1889">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql优化数据库对象</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/1889/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changed limits: max_open_files: 2048 max_connections: 1024 table_cache: 507</title>
		<link>http://blog.haohtml.com/archives/1864</link>
		<comments>http://blog.haohtml.com/archives/1864#comments</comments>
		<pubDate>Thu, 18 Jun 2009 17:37:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=1864</guid>
		<description><![CDATA[Changed limits: max_open_files: 2048 max_connections: 1024 table_cache: 507

这个问题怎么解决啊！

在windows下安装Mysql系统日志出现 max_open_files: 2048 max_connections: 510 table_cache: 764 类似错误是因为 max_connections 最大连接数和max_open_files、table_cache 不匹配。适当的降低max_connections 或调整其他两个数值
    解决办法在 mysql bin > 中输入
mysql-nt --table_cache=764
mysql-nt --innodb_open_files=2048   即可！！

table_cache和max_connections 在my.ini 里可调<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>Changed limits: max_open_files: 2048 max_connections: 1024 table_cache: 507</p>
<p>这个问题怎么解决啊！</p>
<p>在windows下安装Mysql系统日志出现 max_open_files: 2048 max_connections: 510 table_cache: 764 类似错误是因为 <strong>max_connections</strong> 最大连接数和<strong>max_open_files</strong>、<strong>table_cache</strong> 不匹配。适当的降低max_connections 或调整其他两个数值<br />
解决办法在 mysql bin &gt; 中输入<br />
mysql-nt --table_cache=764<br />
mysql-nt --innodb_open_files=2048   即可！！</p>
<p>table_cache和max_connections 在my.ini 里可调</p>
<p>Changed limits:<br />
max_open_files: 2048<br />
max_connections: 1024<br />
table_cache: 507</p>
<p>max_connections=1024</p>
<p>table_cache=500</p>
<p>OK,问题不一定解决,只要MYSQL运行正常就行.</p>
<p>按以上的方法处理后还是有啊！</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB数据库优化：Mongo Database Profiler" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB数据库优化：Mongo Database Profiler</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MongoDB 索引数据类型优化，节省60％内存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12868&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1864">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/05/07/25068958.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MongoDB 索引数据类型优化，节省60％内存</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/1864/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySql Query Cache 查询缓存介绍(1)</title>
		<link>http://blog.haohtml.com/archives/1861</link>
		<comments>http://blog.haohtml.com/archives/1861#comments</comments>
		<pubDate>Thu, 18 Jun 2009 16:25:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=1861</guid>
		<description><![CDATA[MySql Query Cache 和 Oracle  Query Cache 是不同的， Oracle Query Cache 是缓存执行计划的，而MySql Query Cache 不缓存执行计划而是整个结果集。缓存整个结果集的好处不言而喻，但由于缓存的是结果集因此Query必须是完全一样的，这样带来的后果就是平均 Hit Rate 命中率一般不会太高。 Query Cache 对于一些小型应用程序或者数据表的数据量不大的情况下效果是最为明显的。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化的最佳20+条经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4176&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4023187.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化的最佳20+条经验</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>MySql Query Cache 和 Oracle  Query Cache 是不同的， Oracle Query Cache 是缓存执行计划的，而MySql Query Cache 不缓存执行计划而是整个结果集。缓存整个结果集的好处不言而喻，但由于缓存的是结果集因此Query必须是完全一样的，这样带来的后果就是平均 Hit Rate 命中率一般不会太高。 Query Cache 对于一些小型应用程序或者数据表的数据量不大的情况下效果是最为明显的。</p>
<p>作为一个新的特性，MySql Query Cache 有什么特典和局限呢？ 咱一个一个来说：</p>
<p>1、Cache 机制对应用程序是透明的。在应用程序中只是改变查询语句的语义，也能得到缓存中的查询结果集。如果你没有使用 query_cache_wlock_invalidate=ON   来提示MySql 锁表将要进行写操作，那么此时的查询即使表在锁Lock状态下或者预备更新的状态下，仍然可以从缓存中获得结果集；</p>
<p>2、只缓存整个查询结果集，即对子查询，内联视图和部分UNION的查询是不缓存的；</p>
<p>3、缓存机制工作在Packet 级别，第二项的只缓存整个查询结果集就是因为局限于这个机制的原因。由于没有额外的转换和处理，所以保证缓存结果集返回能够非常快；</p>
<p>4、缓存处理在解析查询前进行，保证缓存高性能的一个原因就是查询缓存在执行查询解析前先查找是否已经存在缓存，如果已经存在查询缓存，则直接返回结果集。</p>
<p>5、 查询必须绝对完全同，由于在查找缓存是否存在前不进行查询解析( Query Parser )所以查询并没有经过规范化处理（Normalized），因此缓存查找的过程是按字节顺序进行的 ( Byte by byte )。更具体点说吧：在每次查询时包不同的注释、多余的空格以及大小写不同等等，都不会指向同一个缓存结果集。</p>
<p>6、只有 SELECT 语句被缓存。 插入、删除、更新当然不需要进行缓存了，同时 SHOW 命令和 存储过程 stored procedure （包括存储过程中的SELECT）也不会进入缓存结果集。</p>
<p>7、空格和注释不要出现在查询语句的最前面，当查找缓存时第一个字幕如果不是"S" ,就会停止查询缓存结果集了。第5、6项已经解释过了；</p>
<p>8、不支持预备查询 prepared statement 和 游标 cursors 。 （ ？ ）</p>
<p>9、或许不支持事务处理。（？）</p>
<p>10、查询结果必须完全一致，才能进入缓存结果集。比如：查询语句中有 UUID , RAND , CONNECTION_ID 等会动态改变查询结果集的函数，都不会进入缓存结果集的；</p>
<p>11、查询缓存失效的粒度级别的是表，当表被修改时，所有与改表相关的缓存立即失效( invalidation )。</p>
<p>12、过长时间的查询缓存容易造成碎片 fragmentation  ，这一点和Windows的磁盘管理的碎片整理类似，长时间查询缓存产生的碎片对执行效率有一定影响。可以把查询缓存碎片看作是是查询缓存可用内存（Qcache_free_memory）的块（Qcache_free_blocks ）。FLUSH QUERY CACHE  命令可以削除这种情况。</p>
<p>13、设定适当大小的查询缓存用的内存，由于前面提到的一些原因，一般情况下MySql 的查询缓存机制对内存的需求不可能无限增长，因此设定一个适当的查询缓存内存值是比较经济的做法。可以通过查看 Qcache_free_memory 和 Qcache_lowmem_prunes 的状态来进行适当设置。</p>
<p>14、查询缓存的运行模式，默认情况下开启缓存后MySql 的缓存机制对全局的有效，如果你只想对特定的查询语句使用缓存，可以通过把 query_cache_type  设定为 “DEMAND” 并且在查询语句中加入： SQL_CACHE  来进行，比如：SELECT SQL_CACHE <a href="http://www.domolo.com/seo_software">DomoloSeoHelper</a> from domolo where author='tianchunfeng' 。</p>
<p>上面为你介绍了 Mysql 查询缓存的一些基本特点，那么如何监控Mysql 查询缓存的运行时状态呢？比如监控查询缓存的命中率，调节查询缓存的内存大小等等数据。</p>
<p>可以使用下面的命令：</p>
<p>mysql&gt; show status like ‘Qcache%’;</p>
<p>输出:</p>
<pre>+-------------------------+----------+</pre>
<pre>| Variable_name           | Value    |</pre>
<pre>+-------------------------+----------+</pre>
<pre>| Qcache_free_blocks      | 1        |</pre>
<pre>| Qcache_free_memory      | 16766912 |</pre>
<pre>| Qcache_hits             | 3        |</pre>
<pre>| Qcache_inserts          | 1        |</pre>
<pre>| Qcache_lowmem_prunes    | 0        |</pre>
<pre>| Qcache_not_cached       | 6        |</pre>
<pre>| Qcache_queries_in_cache | 1        |</pre>
<pre>| Qcache_total_blocks     | 4        |</pre>
<pre>+-------------------------+----------+</pre>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="mysql limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">mysql limit查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化的最佳20+条经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4176&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4023187.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化的最佳20+条经验</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/1861/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）</title>
		<link>http://blog.haohtml.com/archives/1859</link>
		<comments>http://blog.haohtml.com/archives/1859#comments</comments>
		<pubDate>Thu, 18 Jun 2009 16:14:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/index.php/archives/1859</guid>
		<description><![CDATA[  症状：
    MySQL重启后，发现连接非常慢，建立连接后做普通操作还是非常快的，通过Show processlist发现大量unauthenticated user连接

    解决办法：
    MySQL启动参数增加一个skip-name-resolve，即不启用DNS反响解析

    解决过程（推荐学习 :)）

   1. 同一局域网不同机器上连接MySQL服务器，观察响应速度；（非常慢）
   2. 连接建立后做一些简单的操作（非常快 和上面不符，说明不是服务器压力导致的）
   3. 通过tcpdump查看连接的时候详细的响应过程（能看到连接的时候和服务器的多次通讯及所耗费的时间）
   4. 发现正常响应很快，用户名、密码发送完后很长一段时间服务器才给响应
   5. 在服务器上进行同样的连接连接操作，试着分别用IP和机器名去连接看响应速度（看是不是IP解析的时候耗费时间了）
   6. 修改/etc/hosts文件，加入服务器、客户端的IP对应（没有作用 :()
   7. 增加启动参数skip-name-resolve速度立即加快了（问题解决）<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL分区技术 分享" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL分区技术 分享</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>症状：<br />
MySQL重启后，发现连接非常慢，建立连接后做普通操作还是非常快的，通过Show processlist发现大量unauthenticated user连接</p>
<p>解决办法：<br />
MySQL启动参数增加一个skip-name-resolve，即不启用DNS反响解析</p>
<p>解决过程（推荐学习 <img src='http://blog.haohtml.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ）</p>
<p>1. 同一局域网不同机器上连接MySQL服务器，观察响应速度；（非常慢）<br />
2. 连接建立后做一些简单的操作（非常快 和上面不符，说明不是服务器压力导致的）<br />
3. 通过tcpdump查看连接的时候详细的响应过程（能看到连接的时候和服务器的多次通讯及所耗费的时间）<br />
4. 发现正常响应很快，用户名、密码发送完后很长一段时间服务器才给响应<br />
5. 在服务器上进行同样的连接连接操作，试着分别用IP和机器名去连接看响应速度（看是不是IP解析的时候耗费时间了）<br />
6. 修改/etc/hosts文件，加入服务器、客户端的IP对应（没有作用 <img src='http://blog.haohtml.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> )<br />
7. 增加启动参数skip-name-resolve速度立即加快了（问题解决）</p>
<p>原因：<br />
MySQL的认证实际上是user+host的形式（也就是说user可以相同），所以MySQL在处理新连接时会试着去解析客户端连接的IP（查呀查，查不到就算了，可是时间浪费了啊），启用参数skip-name-resolve后MySQL授权的时候就只能用纯IP的形式了，MySQL在这里处理的有点弱智，通过IP连过来的查什么查，又查不到:)<br />
如果用的localhost连接的话,一旦添加上了上面的参数,将出现拒绝ip访问的错误信息!</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="优化MySQL语句的十个建议" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12895&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/04/28/24019484.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">优化MySQL语句的十个建议</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL优化篇-查询优化</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL分区技术 分享" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2783&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL分区技术 分享</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">MySQL性能优化详解</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/1859/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

