<?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>about:benjie &#187; Databases</title>
	<atom:link href="http://www.benjiegillam.com/category/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benjiegillam.com</link>
	<description>Benjie Gillam's blog, and home of MythPyWii - probably the best Wii remote (Wiimote) interface to MythTV - see sidebar.</description>
	<lastBuildDate>Tue, 06 Apr 2010 14:14:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL limitations &#8211; triggers and subqueries &#8211; and solution!</title>
		<link>http://www.benjiegillam.com/2007/09/mysql-limitations-triggers-and-subqueries-and-solution/</link>
		<comments>http://www.benjiegillam.com/2007/09/mysql-limitations-triggers-and-subqueries-and-solution/#comments</comments>
		<pubDate>Fri, 21 Sep 2007 11:30:09 +0000</pubDate>
		<dc:creator>Benjie</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://benjiegillam.com/serendipity/archives/27-guid.html</guid>
		<description><![CDATA[A comment on my blog just prompted me to write a new post that hopefully helps someone out.
I have just started using triggers to improve the performance of Blog Friends. It appears that MySQL&#8217;s support of triggers is rather annoying&#8230; 
One fault it has is that it will not let you update a table that [...]]]></description>
			<content:encoded><![CDATA[<p>A comment on my blog just prompted me to write a new post that hopefully helps someone out.</p>
<p>I have just started using triggers to improve the performance of <a href="http://soton.facebook.com/apps/application.php?id=3221375004" target="_blank" title="Blog Friends Facebook About Page">Blog Friends</a>. It appears that MySQL&#8217;s support of triggers is rather annoying&#8230; </p>
<p>One fault it has is that it will not let you update a table that the trigger was called from &#8211; you get the following error:</p>
<blockquote><p>&quot;#1442 &#8211; Can&#8217;t update table &#8216;users_users&#8217; in stored function/trigger because it is already used by statement which invoked this stored function/trigger. &quot;</p>
</blockquote>
<p>Now, as it happens, I was not actually updating that table, I was updating a different table, just using that as a JOIN:</p>
<blockquote><p>UPDATE othertable JOIN users_users ON (conditions) <b>SET othertable.value</b> = value+27 WHERE conditions;</p>
</blockquote>
<p>See &#8211; no writing to users_users at all! But still mysql blocked it.</p>
<p>Now, I thought, no problem, I will re-write it as a subquery:</p>
<blockquote><p>UPDATE othertable SET value=value+27 WHERE id IN (SELECT id FROM users_users WHERE conditions) AND moreconditions;</p>
</blockquote>
<p>But MySQL detects this as a DEPENDENT SUBQUERY even though it was not (my one wasn&#8217;t, at least&#8230; it only referenced NEW.values and not anything from othertable). It seems MySQL has many issues with SUBQUERYs. Well; it worked, but OMG was it slow?!! I almost had time to go and make lunch before it finished just working on 10 users&#8230; This was not good.</p>
<p>After a lot of moaning and no answers on #mysql on irc.freenode.net (which is unusual &#8211; normally there is people in there who know the answers to my questions!) a thought suddenly struck me&#8230;</p>
<blockquote><p>INSERT INTO othertable(id,value) SELECT id, 27 FROM users_users WHERE conditions <a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html"><b>ON DUPLICATE KEY UPDATE</b></a> value=value+27;</p>
</blockquote>
<p>Now in my application, the row should already exist, so there is no problem (and even if it didn&#8217;t the INSERT would do no harm), but if you intend to use it I suggest that you ensure that this INSERT INTO does no damage.</p>
<p>I hope this helps you! If it does, please leave a comment, and I will put more help!</p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benjiegillam.com/2007/09/mysql-limitations-triggers-and-subqueries-and-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP Tips: Disable Database Caching</title>
		<link>http://www.benjiegillam.com/2007/06/cakephp-tips-disable-database-caching/</link>
		<comments>http://www.benjiegillam.com/2007/06/cakephp-tips-disable-database-caching/#comments</comments>
		<pubDate>Fri, 29 Jun 2007 17:57:11 +0000</pubDate>
		<dc:creator>Benjie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://benjiegillam.com/serendipity/archives/14-guid.html</guid>
		<description><![CDATA[Cake caches database queries. This is good, generally, it speeds  things up a lot. But it is not always good. For example, if I want to  query the database once a second to look for updates (e.g. for my chat  application) the you don&#8217;t want the cache. Here&#8217;s how to clear it:
1) [...]]]></description>
			<content:encoded><![CDATA[<p>Cake caches database queries. This is good, generally, it speeds  things up a lot. But it is not always good. For example, if I want to  query the database once a second to look for updates (e.g. for my <a href="http://www.benjiegillam.com/serendipity/archives/13-How-I-made-an-AJAX-chat-client-more-efficient..html" title="How I made an AJAX chat client more efficient.">chat  application</a>) the you don&#8217;t want the cache. Here&#8217;s how to clear it:</p>
<p>1) Create /app/app_model.php, and add in it the following</p>
<blockquote><p>&lt;?php</p>
<p>class AppModel extends Model{  <br />    function _clearDBCache() {<br />      $db =&amp; ConnectionManager::getDataSource($this-&gt;useDbConfig);<br />      $db-&gt;_queryCache = array();<br />    }<br />  }</p>
</blockquote>
<p>Note: you don&#8217;t always need a close &quot;?&gt;&quot;</p>
<p>2) Call $modelNameHere-&gt;_clearDBCache() whereever you need the DB cache cleared.</p>
<p>That&#8217;s it! Have fun!</p>
<p>Whilst I am on the subject of CakePHP tips, why not look into using <a href="http://bakery.cakephp.org/articles/view/an-improvement-to-unbindmodel-on-model-side" title="expects() function for CakePHP">expects()</a> to make your CakePHP queries more efficient &#8211; only request the data you need. Much better than using unbindModel and bindModel all over the place!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benjiegillam.com/2007/06/cakephp-tips-disable-database-caching/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
