<?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>Straylight Run &#187; partitioning</title> <atom:link href="http://blog.straylightrun.net/tag/partitioning/feed/" rel="self" type="application/rss+xml" /><link>http://blog.straylightrun.net</link> <description>Software, Technology, PHP</description> <lastBuildDate>Mon, 07 Nov 2011 19:26:59 +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 Query Cache, Or Vertical Partitioning Intro</title><link>http://blog.straylightrun.net/2009/03/13/mysql-query-cache-or-vertical-partitioning-intro/</link> <comments>http://blog.straylightrun.net/2009/03/13/mysql-query-cache-or-vertical-partitioning-intro/#comments</comments> <pubDate>Fri, 13 Mar 2009 14:12:05 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[partitioning]]></category> <category><![CDATA[query cache]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2009/03/13/mysql-query-cache-or-vertical-partitioning-intro/</guid> <description><![CDATA[The MySQL Query Cache is not very hard to understand. It is at its most basic a giant hash where the literal queries are the keys and the array of result records are the values. So this query: SELECT event_name FROM events WHERE event_id = 8; is different from this query: SELECT event_name FROM events [...]]]></description> <content:encoded><![CDATA[<p>The MySQL Query Cache is not very hard to understand. It is at its most basic a giant hash where the <em>literal queries </em>are the keys and the array of result records are the values. So this query:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> event_name <span style="color: #993333; font-weight: bold;">FROM</span> events <span style="color: #993333; font-weight: bold;">WHERE</span> event_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">8</span>;</pre></div></div><p>is different from this query:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>  event_name <span style="color: #993333; font-weight: bold;">FROM</span> events <span style="color: #993333; font-weight: bold;">WHERE</span> event_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>;</pre></div></div><p>Important note!&nbsp; This means that even though your parameterized queries may look the same without the parameters, to the query cache, they are not!</p><p>As with all caches, the query cache is concerned about freshness of data. It takes perhaps the simplest approach possible to this problem by keeping track of any tables involved in your cached query. If <em>any </em>of these tables changes, it invalidates the query and removes it from the cache. This means that if your query returns frequently-changing data in its results, the query cache will invalidate the query frequently, leading to thrashing. For example, if you had a query that returned a view count of an event:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> event_name<span style="color: #66cc66;">,</span> views <span style="color: #993333; font-weight: bold;">FROM</span> events <span style="color: #993333; font-weight: bold;">WHERE</span> event_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">8</span>;</pre></div></div><p>Every time that event is viewed, the cached query will be invalidated. What&#8217;s the solution?</p><p>In general, write queries so that their result sets do not change often. In specific, mixing static attributes with frequently updated fields in a single table leads to thrashing, so separate out things like view counts and analytics into their own tables. The frequently updated data can be read with a separate query, or perhaps cached in your application in a data structure that periodically flushes to the DB.</p><p>This <a
href="http://en.wikipedia.org/wiki/Partition_(database)">vertical partitioning</a><em> </em>of a single table&#8217;s columns into multiple tables helps immensely with the query cache. What&#8217;s more is that the table with the unchanging data can be further optimized for READS, and the frequently updated table can be optimized for UPDATES.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/03/13/mysql-query-cache-or-vertical-partitioning-intro/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
