<?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>一个故事@MySQL DBA &#187; query cache</title>
	<atom:link href="http://www.orczhou.com/index.php/tag/query-cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.orczhou.com</link>
	<description>一个故事@MySQL DBA</description>
	<lastBuildDate>Tue, 20 Dec 2011 15:51:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Query Cache，看上去很美</title>
		<link>http://www.orczhou.com/index.php/2009/08/query-cache-1/</link>
		<comments>http://www.orczhou.com/index.php/2009/08/query-cache-1/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 10:56:52 +0000</pubDate>
		<dc:creator>orczhou</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query cache]]></category>
		<category><![CDATA[图解]]></category>

		<guid isPermaLink="false">http://orczhou.com/?p=669</guid>
		<description><![CDATA[<p>当你的数据库打开了Query Cache（简称QC）功能后，数据库在执行SELECT语句时，会将其结果放到QC中，当下一次处理同样的SELECT请求时，数据库就会从QC取得结果，而不需要去数据表中查询。</p>
<p>[......]</p><p class='read-more'><a href='http://www.orczhou.com/index.php/2009/08/query-cache-1/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>当你的数据库打开了Query Cache（简称QC）功能后，数据库在执行SELECT语句时，会将其结果放到QC中，当下一次处理同样的SELECT请求时，数据库就会从QC取得结果，而不需要去数据表中查询。</p>
<p><span id="more-669"></span></p>
<p><a title="Query-Cache-1 by orczhou, on Flickr" href="http://www.flickr.com/photos/26825745@N06/3854812215/"><img src="http://farm4.static.flickr.com/3498/3854812215_c316040392_o.png" alt="Query-Cache-1" width="505" height="321" /></a></p>
<p>在这个“Cache为王”的时代，我们总是通过不同的方式去缓存我们的结果从而提高响应效率，但一个缓存机制是否有效，效果如何，却是一个需要好好思考的问题。在MySQL中的Query Cache就是一个适用较少情况的缓存机制。在上图中，如果缓存命中率非常高的话，有测试表明在极端情况下可以提高效率238%<sup>[1]</sup>。但实际情况如何？<strong>Query Cache有如下规则，如果数据表被更改，那么和这个数据表相关的全部Cache全部都会无效，并删除之。这里“数据表更改”包括: </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/insert.html"><strong>INSERT</strong></a><strong>, </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/update.html"><strong>UPDATE</strong></a><strong>, </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/delete.html"><strong>DELETE</strong></a><strong>, </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/truncate.html"><strong>TRUNCATE</strong></a><strong>, </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/alter-table.html"><strong>ALTER TABLE</strong></a><strong>, </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/drop-table.html"><strong>DROP TABLE</strong></a><strong>, or </strong><a href="http://dev.mysql.com/doc/refman/5.0/en/drop-database.html"><strong>DROP DATABASE</strong></a><strong>等。</strong>举个例子，如果数据表posts访问频繁，那么意味着它的很多数据会被QC缓存起来，但是每一次posts数据表的更新，无论更新是不是影响到了cache的数据，都会将全部和posts表相关的cache清除。如果你的数据表更新频繁的话，那么Query Cache将会成为系统的负担。有实验表明，糟糕时，QC会降低系统13%<sup>[1]</sup>的处理能力。</p>
<p>如果你的应用对数据库的更新很少，那么QC将会作用显著。比较典型的如博客系统，一般博客更新相对较慢，数据表相对稳定不变，这时候QC的作用会比较明显。</p>
<p>再如，一个更新频繁的BBS系统。下面是一个实际运行的论坛数据库的状态参数：</p>
<table border="0" cellspacing="0" cellpadding="2" width="300">
<tbody>
<tr>
<td width="150" valign="top">QCache_hit</td>
<td width="150" valign="top">5280438</td>
</tr>
<tr>
<td width="150" valign="top">QCache_insert</td>
<td width="150" valign="top">8008948</td>
</tr>
<tr>
<td width="150" valign="top">Qcache_not_cache</td>
<td width="150" valign="top">95372</td>
</tr>
<tr>
<td width="150" valign="top">Com select</td>
<td width="150" valign="top">8104159</td>
</tr>
</tbody>
</table>
<p>可以看到，数据库一共往QC中写入了约800W次缓存，但是实际命中的只有约500W次。也就是说，每一个缓存的使用率约为0.66次。很难说，该缓存的作用是否大于QC系统所带来的开销。但是有一点是很肯定的，QC缓存的作用是很微小的，如果应用层能够实现缓存，将可以忽略QC的效果。</p>
<p style="text-align: left;">-------------下面是关于QC的一些其他细节-----------------</p>
<p style="text-align: left;">一、Query Cache相关参数：</p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_query_cache_size">query_cache_size</a> QC占用空间大小，通过将其设置为0关闭QC功能</li>
<li>query_cache_type 0表示关闭QC；1表示正常缓存；2表示SQL_CACHE才缓存</li>
<li>query_cache_limit 最大缓存结果集</li>
<li>query_cache_min_res_unit 手册上说，QC会按照这个值分配缓存block的大小。</li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html#statvar_Qcache_lowmem_prunes">Qcache_lowmem_prunes</a> 这是一个状态变量(show status)，当缓存空间不够需要释放旧的缓存时，该值会自增。</li>
</ul>
<p style="text-align: left;">二、Query Cache观察：</p>
<pre class="brush:sql">CREATE TABLE t1(id INT,var1 varchar(10));
	//Com_select:8	Qcache_hits:1
INSERT INTO t1 VALUES(1,’WWW’);
	//Com_select:8	Qcache_hits:1
SELECT * FROM t1 WHERE id=1;
	//Com_select:9	Qcache_hits:1
SELECT * FROM t1 WHERE id=1;
	//Com_select:9	Qcache_hits:2 Qcache_queries_in_cache:1
INSERT INTO t1 VALUES(2,’RRRR’);
	//Com_select:9	Qcache_hits:2 Qcache_queries_in_cache:0
SELECT * FROM t1 WHERE id=1; //INSERT后Cache失效
	//Com_select:10  Qcache_hits:2 Qcache_queries_in_cache:1</pre>
<p>参考：</p>
<ol>
<li><a title="http://dev.mysql.com/doc/refman/5.0/en/query-cache.html" href="http://dev.mysql.com/doc/refman/5.0/en/query-cache.html">http://dev.mysql.com/doc/refman/5.0/en/query-cache.html</a></li>
<li><a title="http://dev.mysql.com/doc/refman/5.0/en/query-cache.html" href="http://dev.mysql.com/doc/refman/5.0/en/query-cache.html"></a><a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_query_cache_min_res_unit">http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_query_cache_min_res_unit"></a><a href="http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-cache/">http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-cache/</a></li>
</ol>
<p>(全文完)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.orczhou.com/index.php/2009/08/query-cache-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

