<?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>Sun, 05 Feb 2012 12:34:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL性能优化详解</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性能优化的最佳20+条经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4176&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/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 limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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性能优化的最佳20+条经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4176&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/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 limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&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="DeployPHP 系列第 1 部分：优化 PHP 和 Oracle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F563&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;">DeployPHP 系列第 1 部分：优化 PHP 和 Oracle</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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="MySQL优化篇-查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&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="DeployPHP 系列第 1 部分：优化 PHP 和 Oracle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F563&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;">DeployPHP 系列第 1 部分：优化 PHP 和 Oracle</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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.htm?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 limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?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 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859&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 性能优化（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="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升MySQL性能</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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.htm?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 limit查询优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?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 性能优化（show processlist中出现大量未认证的连接 建立连接缓慢 unauthenticated user）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1859&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 性能优化（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="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升MySQL性能</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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.htm?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基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据status信息对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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&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 limit查询优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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.htm?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基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据status信息对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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F743&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 limit查询优化</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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.htm?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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据status信息对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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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.htm" 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.htm?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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据status信息对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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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.htm" 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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&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="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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="mysql优化数据库对象" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&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="Mysql基础优化第一讲" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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="mysql优化数据库对象" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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>
                <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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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>
                <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.htm?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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&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;">根据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.htm" 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.htm?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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.htm" 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.htm?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="通过分区（Partition）提升MySQL性能" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374&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;">通过分区（Partition）提升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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2886&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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F447&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.htm" 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>
		<item>
		<title>根据status信息对MySQL服务器进行优化</title>
		<link>http://blog.haohtml.com/archives/1841</link>
		<comments>http://blog.haohtml.com/archives/1841#comments</comments>
		<pubDate>Tue, 16 Jun 2009 12:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=1841</guid>
		<description><![CDATA[网上有很多的文章教怎么配置MySQL服务器，但考虑到服务器硬件配置的不同，具体应用的差别，那些文章的做法只能作为初步设置参考，我们需要根据自己的情况进行配置优化，好的做法是MySQL服务器稳定运行了一段时间后运行，根据服务器的”状态”进行优化。

    mysql> show global status;

可以列出MySQL服务器运行各种状态值，另外，查询MySQL服务器配置信息语句：

    mysql> show variables;<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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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 Query Cache 查询缓存介绍(1)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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 Query Cache 查询缓存介绍(1)</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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11861&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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.htm" 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服务器，但考虑到服务器硬件配置的不同，具体应用的差别，那些文章的做法只能作为初步设置参考，我们需要根据自己的情况进行配置优化，好的做法是MySQL服务器稳定运行了一段时间后运行，根据服务器的”状态”进行优化。</p>
<blockquote><p>mysql&gt; show global status;</p></blockquote>
<p>可以列出MySQL服务器运行各种状态值，另外，查询MySQL服务器配置信息语句：</p>
<blockquote><p>mysql&gt; show variables;</p></blockquote>
<p><strong>一、慢查询</strong></p>
<blockquote>
<pre>mysql&gt; show variables like '%slow%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| log_slow_queries | ON    |
| slow_launch_time | 2     |
+------------------+-------+

mysql&gt; show global status like '%slow%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| Slow_launch_threads | 0     |
| Slow_queries        | 4148 |
+---------------------+-------+</pre>
</blockquote>
<p>配置中打开了记录慢查询，执行时间超过2秒的即为慢查询，系统显示有4148个慢查询，你可以分析慢查询日志，找出有问题的SQL语句，慢查询时间不宜设置过长，否则意义不大，最好在5秒以内，如果你需要微秒级别的慢查询，可以考虑给MySQL打补丁：<a href="http://www.percona.com/docs/wiki/release:start" target="_blank">http://www.percona.com/docs/wiki/release:start</a>，记得找对应的版本。</p>
<p>打开慢查询日志可能会对系统性能有一点点影响，如果你的MySQL是主－从结构，可以考虑打开其中一台从服务器的慢查询日志，这样既可以监控慢查询，对系统性能影响又小。</p>
<p><strong>二、连接数</strong></p>
<p>经常会遇见"MySQL: ERROR 1040: Too many connections”的情况，一种是访问量确实很高，MySQL服务器抗不住，这个时候就要考虑增加从服务器分散读压力，另外一种情况是MySQL配 置文件中max_connections值过小：</p>
<blockquote>
<pre>mysql&gt; show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 256   |
+-----------------+-------+</pre>
</blockquote>
<p>这台MySQL服务器最大连接数是256，然后查询一下服务器响应的最大连接数：</p>
<blockquote>
<pre>mysql&gt; show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Max_used_connections | 245   |
+----------------------+-------+</pre>
</blockquote>
<p>MySQL服务器过去的最大连接数是245，没有达到服务器连接数上限256，应该没有出现1040错误，比较理想的设置是：</p>
<blockquote><p>Max_used_connections / max_connections  * 100% ≈ 85%</p></blockquote>
<p>最大连接数占上限连接数的85％左右，如果发现比例在10%以下，MySQL服务器连接数上限设置的过高了。</p>
<p><strong>三、Key_buffer_size</strong></p>
<p>key_buffer_size是对MyISAM表性能影响最大的一个参数，下面一台以MyISAM为主要存储引擎服务器的配置：</p>
<blockquote>
<pre>mysql&gt; show variables like 'key_buffer_size';
+-----------------+------------+
| Variable_name   | Value      |
+-----------------+------------+
| key_buffer_size | 536870912 |
+-----------------+------------+</pre>
</blockquote>
<p>分配了512MB内存给key_buffer_size，我们再看一下key_buffer_size的使用情况：</p>
<blockquote>
<pre>mysql&gt; show global status like 'key_read%';
+------------------------+-------------+
| Variable_name          | Value       |
+------------------------+-------------+
| Key_read_requests      | 27813678764 |
| Key_reads              | 6798830     |
+------------------------+-------------+</pre>
</blockquote>
<p>一共有27813678764个索引读取请求，有6798830个请求在内存中没有找到直接从硬盘读取索引，计算索引未命中缓存的概率：</p>
<blockquote><p>key_cache_miss_rate ＝ Key_reads / Key_read_requests * 100%</p></blockquote>
<p>比如上面的数据，key_cache_miss_rate为0.0244%，4000个索引读取请求才有一个直接读硬盘，已经很BT 了，key_cache_miss_rate在0.1%以下都很好（每1000个请求有一个直接读硬盘），如果key_cache_miss_rate在 0.01%以下的话，key_buffer_size分配的过多，可以适当减少。</p>
<p>MySQL服务器还提供了key_blocks_*参数：</p>
<blockquote>
<pre>mysql&gt; show global status like 'key_blocks_u%';
+------------------------+-------------+
| Variable_name          | Value       |
+------------------------+-------------+
| Key_blocks_unused      | 0           |
| Key_blocks_used        | 413543      |
+------------------------+-------------+</pre>
</blockquote>
<p>Key_blocks_unused表示未使用的缓存簇(blocks)数，Key_blocks_used表示曾经用到的最大的blocks数， 比如这台服务器，所有的缓存都用到了，要么增加key_buffer_size，要么就是过渡索引了，把缓存占满了。比较理想的设置：</p>
<blockquote><p>Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 80%</p></blockquote>
<p><strong>四、临时表</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'created_tmp%';
+-------------------------+---------+
| Variable_name           | Value   |
+-------------------------+---------+
| Created_tmp_disk_tables | 21197   |
| Created_tmp_files       | 58      |
| Created_tmp_tables      | 1771587 |
+-------------------------+---------+</pre>
</blockquote>
<p>每次创建临时表，Created_tmp_tables增加，如果是在磁盘上创建临时表，Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服务创建的临时文件文件数，比较理想的配置是：</p>
<blockquote>
<pre>Created_tmp_disk_tables / Created_tmp_tables * 100% &lt;= 25%</pre>
</blockquote>
<p>比如上面的服务器Created_tmp_disk_tables / Created_tmp_tables * 100% ＝ 1.20%，应该相当好了。我们再看一下MySQL服务器对临时表的配置：</p>
<blockquote>
<pre>mysql&gt; show variables where Variable_name in ('tmp_table_size', 'max_heap_table_size');
+---------------------+-----------+
| Variable_name       | Value     |
+---------------------+-----------+
| max_heap_table_size | 268435456 |
| tmp_table_size      | 536870912 |
+---------------------+-----------+</pre>
</blockquote>
<p>只有256MB以下的临时表才能全部放内存，超过的就会用到硬盘临时表。</p>
<p><strong>五、Open Table情况</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables   | 919   |
| Opened_tables | 1951  |
+---------------+-------+</pre>
</blockquote>
<p>Open_tables表示打开表的数量，Opened_tables表示打开过的表数量，如果Opened_tables数量过大，说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小，我们查询一下服务器table_cache值：</p>
<blockquote>
<pre>mysql&gt; show variables like 'table_cache';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| table_cache   | 2048  |
+---------------+-------+</pre>
</blockquote>
<p>比较合适的值为：</p>
<blockquote><p>Open_tables / Opened_tables  * 100% &gt;= 85%<br />
Open_tables / table_cache * 100% &lt;= 95%</p></blockquote>
<p><strong>六、进程使用情况</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'Thread%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 46    |
| Threads_connected | 2     |
| Threads_created   | 570   |
| Threads_running   | 1     |
+-------------------+-------+</pre>
</blockquote>
<p>如果我们在MySQL服务器配置文件中设置了thread_cache_size，当客户端断开之后，服务器处理此客户的线程将会缓存起来以响应下 一个客户而不是销毁（前提是缓存数未达上限）。Threads_created表示创建过的线程数，如果发现Threads_created值过大的话， 表明MySQL服务器一直在创建线程，这也是比较耗资源，可以适当增加配置文件中thread_cache_size值，查询服务器 thread_cache_size配置：</p>
<blockquote>
<pre>mysql&gt; show variables like 'thread_cache_size';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| thread_cache_size | 64    |
+-------------------+-------+</pre>
</blockquote>
<p>示例中的服务器还是挺健康的。</p>
<p><strong>七、查询缓存(query cache)</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 22756     |
| Qcache_free_memory      | 76764704  |
| Qcache_hits             | 213028692 |
| Qcache_inserts          | 208894227 |
| Qcache_lowmem_prunes    | 4010916   |
| Qcache_not_cached       | 13385031  |
| Qcache_queries_in_cache | 43560     |
| Qcache_total_blocks     | 111212    |
+-------------------------+-----------+</pre>
</blockquote>
<p>MySQL查询缓存变量解释：</p>
<blockquote><p>Qcache_free_blocks：缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理，从而得到一个空闲块。<br />
Qcache_free_memory：缓存中的空闲内存。<br />
Qcache_hits：每次查询在缓存中命中时就增大<br />
Qcache_inserts：每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。<br />
Qcache_lowmem_prunes： 缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看；如果这个数字在不断增长，就表示可能碎片非常严重，或者内存 很少。（上面的 free_blocks和free_memory可以告诉您属于哪种情况）<br />
Qcache_not_cached：不适合进行缓存的查询的数量，通常是由于这些查询不是 <code><span style="font-family: NSimsun;">SELECT</span></code> 语句或者用了now()之类的函数。<br />
Qcache_queries_in_cache：当前缓存的查询（和响应）的数量。<br />
Qcache_total_blocks：缓存中块的数量。</p></blockquote>
<p>我们再查询一下服务器关于query_cache的配置：</p>
<blockquote>
<pre>mysql&gt; show variables like 'query_cache%';
+------------------------------+-----------+
| Variable_name                | Value     |
+------------------------------+-----------+
| query_cache_limit            | 2097152   |
| query_cache_min_res_unit     | 4096      |
| query_cache_size             | 203423744 |
| query_cache_type             | ON        |
| query_cache_wlock_invalidate | OFF       |
+------------------------------+-----------+</pre>
</blockquote>
<p>各字段的解释：</p>
<blockquote><p>query_cache_limit：超过此大小的查询将不缓存<br />
query_cache_min_res_unit：缓存块的最小大小<br />
query_cache_size：查询缓存大小<br />
query_cache_type：缓存类型，决定缓存什么样的查询，示例中表示不缓存 select sql_no_cache 查询<br />
query_cache_wlock_invalidate：当有其他客户端正在对MyISAM表进行写操作时，如果查询在query cache中，是否返回cache结果还是等写操作完成再读表获取结果。</p></blockquote>
<p>query_cache_min_res_unit的配置是一柄”双刃剑”，默认是4KB，设置值大对大数据查询有好处，但如果你的查询都是小数据查询，就容易造成内存碎片和浪费。</p>
<blockquote><p>查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%</p></blockquote>
<p>如果查询缓存碎片率超过20%，可以用FLUSH QUERY CACHE整理缓存碎片，或者试试减小query_cache_min_res_unit，如果你的查询都是小数据量的话。</p>
<blockquote><p>查询缓存利用率 = (query_cache_size - Qcache_free_memory) / query_cache_size * 100%</p></blockquote>
<p>查询缓存利用率在25%以下的话说明query_cache_size设置的过大，可适当减小；查询缓存利用率在80％以上而且Qcache_lowmem_prunes &gt; 50的话说明query_cache_size可能有点小，要不就是碎片太多。</p>
<blockquote><p>查询缓存命中率 = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%</p></blockquote>
<p>示例服务器 查询缓存碎片率 ＝ 20.46％，查询缓存利用率 ＝ 62.26％，查询缓存命中率 ＝ 1.94％，命中率很差，可能写操作比较频繁吧，而且可能有些碎片。</p>
<p><strong>八、排序使用情况</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'sort%';
+-------------------+------------+
| Variable_name     | Value      |
+-------------------+------------+
| Sort_merge_passes | 29         |
| Sort_range        | 37432840   |
| Sort_rows         | 9178691532 |
| Sort_scan         | 1860569    |
+-------------------+------------+</pre>
</blockquote>
<p>Sort_merge_passes 包括两步。MySQL 首先会尝试在内存中做排序，使用的内存大小由系统变量 Sort_buffer_size 决定，如果它的大小不够把所有的记录都读到内存中，MySQL 就会把每次在内存中排序的结果存到临时文件中，等 MySQL 找到所有记录之后，再把临时文件中的记录做一次排序。这再次排序就会增加 Sort_merge_passes。实际上，MySQL 会用另一个临时文件来存再次排序的结果，所以通常会看到 Sort_merge_passes 增加的数值是建临时文件数的两倍。因为用到了临时文件，所以速度可能会比较慢，增加 Sort_buffer_size 会减少 Sort_merge_passes 和 创建临时文件的次数。但盲目的增加 Sort_buffer_size 并不一定能提高速度，见 <a href="http://www.mysqlperformanceblog.com/2007/08/18/how-fast-can-you-sort-data-with-mysql/" target="_blank">How fast can you sort data with MySQL?</a>（引自<a href="http://qroom.blogspot.com/2007/09/mysql-select-sort.html" target="_blank">http://qroom.blogspot.com/2007/09/mysql-select-sort.html</a>，貌似被墙）</p>
<p>另外，增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处，参见：<a href="http://www.mysqlperformanceblog.com/2007/07/24/what-exactly-is-read_rnd_buffer_size/" target="_blank">http://www.mysqlperformanceblog.com/2007/07/24/what-exactly-is-read_rnd_buffer_size/</a></p>
<p><strong>九、文件打开数(open_files)</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'open_files';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_files    | 1410  |
+---------------+-------+

mysql&gt; show variables like 'open_files_limit';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| open_files_limit | 4590  |
+------------------+-------+</pre>
</blockquote>
<p>比较合适的设置：Open_files / open_files_limit * 100% &lt;= 75％</p>
<p><strong>十、表锁情况</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'table_locks%';
+-----------------------+-----------+
| Variable_name         | Value     |
+-----------------------+-----------+
| Table_locks_immediate | 490206328 |
| Table_locks_waited    | 2084912   |
+-----------------------+-----------+</pre>
</blockquote>
<p>Table_locks_immediate表示立即释放表锁数，Table_locks_waited表示需要等待的表锁数，如果 Table_locks_immediate / Table_locks_waited &gt; 5000，最好采用InnoDB引擎，因为InnoDB是行锁而MyISAM是表锁，对于高并发写入的应用InnoDB效果会好些。示例中的服务器 Table_locks_immediate / Table_locks_waited ＝ 235，MyISAM就足够了。</p>
<p><strong>十一、表扫描情况</strong></p>
<blockquote>
<pre>mysql&gt; show global status like 'handler_read%';
+-----------------------+-------------+
| Variable_name         | Value       |
+-----------------------+-------------+
| Handler_read_first    | 5803750     |
| Handler_read_key      | 6049319850  |
| Handler_read_next     | 94440908210 |
| Handler_read_prev     | 34822001724 |
| Handler_read_rnd      | 405482605   |
| Handler_read_rnd_next | 18912877839 |
+-----------------------+-------------+</pre>
</blockquote>
<p>各字段解释参见<a href="http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html" target="_blank">http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html</a>，调出服务器完成的查询请求次数：</p>
<blockquote>
<pre>mysql&gt; show global status like 'com_select';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| Com_select    | 222693559 |
+---------------+-----------+</pre>
</blockquote>
<p>计算表扫描率：</p>
<blockquote><p>表扫描率 ＝ Handler_read_rnd_next / Com_select</p></blockquote>
<p>如果表扫描率超过4000，说明进行了太多表扫描，很有可能索引没有建好，增加read_buffer_size值会有一些好处，但最好不要超过8MB。</p>
<p><strong>后记：</strong></p>
<p>文中提到一些数字都是参考值，了解基本原理就可以，除了MySQL提供的各种status值外，操作系统的一些性能指标也很重要，比如常用的top,iostat等，尤其是iostat，现在的系统瓶颈一般都在磁盘IO上，关于iostat的使用，可以参考：<a href="http://www.php-oa.com/2009/02/03/iostat.html" target="_blank">http://www.php-oa.com/2009/02/03/iostat.html</a></p>
<p><strong>参考资料：</strong></p>
<p>1.<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.htm" target="_blank">http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.htm</a></p>
<p>2.<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html" target="_blank">http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html</a></p>
<p>3.<a href="http://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html" target="_blank">http://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html</a></p>
<p>4.<a href="http://www.day32.com/MySQL/tuning-primer.sh" target="_blank">http://www.day32.com/MySQL/tuning-primer.sh</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="MySQL性能优化详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F5898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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 Query Cache 查询缓存介绍(1)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1861&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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 Query Cache 查询缓存介绍(1)</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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11861&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841">
                        <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.htm" 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/1841/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>通过分区（Partition）提升MySQL性能</title>
		<link>http://blog.haohtml.com/archives/1374</link>
		<comments>http://blog.haohtml.com/archives/1374#comments</comments>
		<pubDate>Sun, 10 May 2009 13:29:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库优化]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=1374</guid>
		<description><![CDATA[什么是数据库分区？

　　数据库分区是一种物理数据库设计技术，DBA和数据库建模人员对其相当熟悉。虽然分区技术可以实现很多效果，但其主要目的是为了在特定的SQL操作中减少数据读写的总量以缩减响应时间。
分区主要有两种形式：//这里一定要注意行和列的概念（row是行，column是列）

　　1. 水平分区（Horizontal Partitioning）这种形式分区是对表的行进行分区，通过这样的方式不同分组里面的物理列分割的数据集得以组合，从而进行个体分割（单分区）或集体分割（1个或多个分区）。所有在表中定义的列在每个数据集中都能找到，所以表的特性依然得以保持。<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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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 数据库优化（11）The Query Optimization Process" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4270&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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 数据库优化（11）The Query Optimization Process</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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><strong>什么是数据库分区？</strong></p>
<p>　　数据库分区是一种物理数据库设计技术，DBA和数据库建模人员对其相当熟悉。虽然分区技术可以实现很多效果，但其主要目的是为了在特定的SQL操作中减少数据读写的总量以缩减响应时间。<br />
分区主要有两种形式：//这里一定要注意行和列的概念（row是行，column是列）</p>
<p>　　1. 水平分区（Horizontal Partitioning）这种形式分区是对表的行进行分区，通过这样的方式不同分组里面的物理列分割的数据集得以组合，从而进行个体分割（单分区）或集体分割（1个或多个分区）。所有在表中定义的列在每个数据集中都能找到，所以表的特性依然得以保持。<br />
　　举个简单例子：一个包含十年发票记录的表可以被分区为十个不同的分区，每个分区包含的是其中一年的记录。（朋奕注：这里具体使用的分区方式我们后面再说，可以先说一点，一定要通过某个属性列来分割，譬如这里使用的列就是年份）<br />
　　2. 垂直分区（Vertical Partitioning） 这种分区方式一般来说是通过对表的垂直划分来减少目标表的宽度，使某些特定的列 被划分到特定的分区，每个分区都包含了其中的列所对应的行。<br />
　　举个简单例子：一个包含了大text和BLOB列的表，这些text和BLOB列又不经常被访问，这时候就要把这些不经常使用的text和BLOB了划分到另一个分区，在保证它们数据相关性的同时还能提高访问速度。</p>
<p>　　在数据库供应商开始在他们的数据库引擎中建立分区（主要是水平分区）时，DBA和建模者必须设计好表的物理分区结构，不要保存冗余的数据（不同表中同时都包含父表中的数据）或相互联结成一个逻辑父对象（通常是视图）。这种做法会使水平分区的大部分功能失效，有时候也会对垂直分区产生影响。</p>
<p><strong>在MySQL 5.1中进行分区</strong></p>
<p>　　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><strong>如何进行实际分区</strong></p>
<p>　　看看分区的实际效果吧。我们建立几个同样的MyISAM引擎的表，包含日期敏感的数据，但只对其中一个分区。分区的表（表名为part_tab）我们采用Range范围分区模式，通过年份进行分区：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">mysql&gt; CREATE TABLE part_tab</li>
<li>   -&gt; ( c1 int default NULL,</li>
<li>   -&gt; c2 varchar(30) default NULL,</li>
<li>   -&gt; c3 date default NULL</li>
<li>   -&gt;</li>
<li>   -&gt; ) engine=myisam</li>
<li><strong>   -&gt; PARTITION BY RANGE (year(c3)) (PARTITION p0 VALUES LESS THAN (1995),</strong></li>
<li><strong>   -&gt; PARTITION p1 VALUES LESS THAN (1996) , PARTITION p2 VALUES LESS THAN (1997) ,</strong></li>
<li><strong>   -&gt; PARTITION p3 VALUES LESS THAN (1998) , PARTITION p4 VALUES LESS THAN (1999) ,</strong></li>
<li><strong>   -&gt; PARTITION p5 VALUES LESS THAN (2000) , PARTITION p6 VALUES LESS THAN (2001) ,</strong></li>
<li><strong>   -&gt; PARTITION p7 VALUES LESS THAN (2002) , PARTITION p8 VALUES LESS THAN (2003) ,</strong></li>
<li><strong>   -&gt; PARTITION p9 VALUES LESS THAN (2004) , PARTITION p10 VALUES LESS THAN (2010),</strong></li>
<li><strong>   -&gt; PARTITION p11 VALUES LESS THAN MAXVALUE );</strong></li>
<li>Query OK, 0 rows affected (0.00 sec)</li>
</ol>
</div>
<p>　　注意到了这里的最后一行吗？这里把不属于前面年度划分的年份范围都包含了，这样才能保证数据不会出错，大家以后要记住啊，不然数据库无缘无故出错你就爽了。那下面我们建立没有分区的表（表名为no_part_tab）：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">mysql&gt; create table no_part_tab</li>
<li>   -&gt; (c1 int(11) default NULL,</li>
<li>   -&gt; c2 varchar(30) default NULL,</li>
<li>   -&gt; c3 date default NULL) engine=myisam;</li>
<li>Query OK, 0 rows affected (0.02 sec)</li>
</ol>
</div>
<p>　　下面咱写一个存储过程（感谢Peter Gulutzan给的代码，如果大家需要Peter Gulutzan的存储过程教程的中文翻译也可以跟我要，chenpengyi◎gmail.com），它能向咱刚才建立的已分区的表中平均的向每个分区插入共8百万条不同的数据。填满后，咱就给没分区的克隆表中插入相同的数据：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">mysql&gt; delimiter //</li>
<li>mysql&gt; CREATE PROCEDURE load_part_tab()</li>
<li>   -&gt; begin</li>
<li>   -&gt; declare v int default 0;</li>
<li>   -&gt; while v &lt; 8000000</li>
<li>   -&gt; do</li>
<li>   -&gt; insert into part_tab</li>
<li>   -&gt; values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));</li>
<li>   -&gt; set v = v + 1;</li>
<li>   -&gt; end while;</li>
<li>   -&gt; end</li>
<li>   -&gt; //</li>
<li>Query OK, 0 rows affected (0.00 sec)</li>
<li>mysql&gt; delimiter ;</li>
<li>mysql&gt; call load_part_tab();</li>
<li>Query OK, 1 row affected (8 min 17.75 sec)</li>
<li>mysql&gt; insert into no_part_tab select * from part_tab;</li>
<li>Query OK, 8000000 rows affected (51.59 sec)</li>
<li>Records: 8000000 Duplicates: 0 Warnings: 0</li>
</ol>
</div>
<p>　　表都准备好了。咱开始对这两表中的数据进行简单的范围查询吧。先分区了的，后没分区的，跟着有执行过程解析（MySQL Explain命令解析器），可以看到MySQL做了什么：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">mysql&gt; select count(*) from no_part_tab where</li>
<li>   -&gt; c3 &gt; date '1995-01-01' and c3 &lt; date '1995-12-31';</li>
<li>+----------+</li>
<li>| count(*) |</li>
<li>+----------+</li>
<li>| 795181 |</li>
<li>+----------+</li>
<li>1 row in set (38.30 sec)</li>
<li>mysql&gt; select count(*) from part_tab where</li>
<li>   -&gt; c3 &gt; date '1995-01-01' and c3 &lt; date '1995-12-31';</li>
<li>+----------+</li>
<li>| count(*) |</li>
<li>+----------+</li>
<li>| 795181 |</li>
<li>+----------+</li>
<li>1 row in set (3.88 sec)</li>
<li>mysql&gt; explain select count(*) from no_part_tab where</li>
<li>   -&gt; c3 &gt; date '1995-01-01' and c3 &lt; date '1995-12-31'\G</li>
<li>*************************** 1. row ***************************</li>
<li>   id: 1</li>
<li>select_type: SIMPLE</li>
<li>   table: no_part_tab</li>
<li>   type: ALL</li>
<li>possible_keys: NULL</li>
<li>   key: NULL</li>
<li>   key_len: NULL</li>
<li>   ref: NULL</li>
<li>   rows: 8000000</li>
<li>   Extra: Using where</li>
<li>1 row in set (0.00 sec)</li>
<li>mysql&gt; explain partitions select count(*) from part_tab where</li>
<li>   -&gt; c3 &gt; date '1995-01-01' and c3 &lt; date '1995-12-31'\G</li>
<li>*************************** 1. row ***************************</li>
<li>   id: 1</li>
<li>select_type: SIMPLE</li>
<li>   table: part_tab</li>
<li>   partitions: p1</li>
<li>   type: ALL</li>
<li>possible_keys: NULL</li>
<li>   key: NULL</li>
<li>   key_len: NULL</li>
<li>   ref: NULL</li>
<li>   rows: 798458</li>
<li>   Extra: Using where</li>
<li>1 row in set (0.00 sec)</li>
</ol>
</div>
<p>　　从上面结果可以容易看出，设计恰当表分区能比非分区的减少90％的响应时间。而命令解析Explain程序也告诉我们在对已分区的表的查询过程中仅对第一个分区进行了扫描，其他都跳过了。<br />
哔厉吧拉，说阿说……反正就是这个分区功能对DBA很有用拉，特别对VLDB和需要快速反应的系统。</p>
<p><strong>对Vertical Partitioning的一些看法</strong><br />
　　虽然MySQL 5.1自动实现了水平分区，但在设计数据库的时候不要轻视垂直分区。虽然要手工去实现垂直分区，但在特定场合下你会收益不少的。例如在前面建立的表中，VARCHAR字段是你平常很少引用的，那么对它进行垂直分区会不会提升速度呢？咱们看看测试结果：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">mysql&gt; desc part_tab;</li>
<li>+-------+-------------+------+-----+---------+-------+</li>
<li>| Field | Type | Null | Key | Default | Extra |</li>
<li>+-------+-------------+------+-----+---------+-------+</li>
<li>| c1 | int(11) | YES | | NULL | |</li>
<li>| c2 | varchar(30) | YES | | NULL | |</li>
<li>| c3 | date | YES | | NULL | |</li>
<li>+-------+-------------+------+-----+---------+-------+</li>
<li>3 rows in set (0.03 sec)</li>
<li>mysql&gt; alter table part_tab drop column c2;</li>
<li>Query OK, 8000000 rows affected (42.20 sec)</li>
<li>Records: 8000000 Duplicates: 0 Warnings: 0</li>
<li>mysql&gt; desc part_tab;</li>
<li>+-------+---------+------+-----+---------+-------+</li>
<li>| Field | Type | Null | Key | Default | Extra |</li>
<li>+-------+---------+------+-----+---------+-------+</li>
<li>| c1 | int(11) | YES | | NULL | |</li>
<li>| c3 | date | YES | | NULL | |</li>
<li>+-------+---------+------+-----+---------+-------+</li>
<li>2 rows in set (0.00 sec)</li>
<li>mysql&gt; select count(*) from part_tab where</li>
<li>   -&gt; c3 &gt; date '1995-01-01' and c3 &lt; date '1995-12-31';</li>
<li>+----------+</li>
<li>| count(*) |</li>
<li>+----------+</li>
<li>| 795181 |</li>
<li>+----------+</li>
<li>1 row in set (0.34 sec)</li>
</ol>
</div>
<p>　　在设计上去掉了VARCHAR字段后，不止是你，俺也发现查询响应速度上获得了另一个90％的时间节省。所以大家在设计表的时候，一定要考虑，表中的字段是否真正关联，又是否在你的查询中有用？</p>
<p><strong>补充说明</strong></p>
<p>　　这么简单的文章肯定不能说全MySQL 5.1 分区机制的所有好处和要点（虽然对自己写文章水平很有信心），下面就说几个感兴趣的：</p>
<p>　　* 支持所有存储引擎(MyISAM, Archive, InnoDB, 等等)<br />
　　* 对分区的表支持索引，包括本地索引local indexes，对其进行的是一对一的视图镜像，假设一个表有十个分区，那么其本地索引也包含十个分区。<br />
　　* 关于分区的元数据Metadata的表可以在INFORMATION_SCHEMA数据库中找到，表名为PARTITIONS。<br />
　　* All SHOW 命令支持返回分区表以及元数据的索引。<br />
　　* 对其操作的命令和实现的维护功能有（比对全表的操作还多）：</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" title="双击代码区域显示行号.">
<li class="hl-firstline">　　o ADD PARTITION</li>
<li>　　o DROP PARTITION</li>
<li>　　o COALESCE PARTITION</li>
<li>　　o REORGANIZE PARTITION</li>
<li>　　o ANALYZE PARTITION</li>
<li>　　o CHECK PARTITION</li>
<li>　　o OPTIMIZE PARTITION</li>
<li>　　o REBUILD PARTITION</li>
<li>　　o REPAIR PARTITION</li>
</ol>
</div>
<p>　　站在性能主导的观点上来说，MySQL 5.1的分区功能能给数据性能带来巨大的提升的同时减轻DBA的管理负担，如果分区合理的话。如果需要更多的资料可以去http://dev.mysql.com/doc/refman/5.1/en/partitioning.html或 http://forums.mysql.com/list.php?106获得相关资料。</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="根据status信息对MySQL服务器进行优化" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1841&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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>
                <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2821&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F898&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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 数据库优化（11）The Query Optimization Process" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4270&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F1374">
                        <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 数据库优化（11）The Query Optimization Process</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" 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/1374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

