<?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>Hello, I am Sean Murphy &#187; now</title>
	<atom:link href="http://iamseanmurphy.com/tag/now/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamseanmurphy.com</link>
	<description>Thoughts, news, code by Sean Murphy</description>
	<lastBuildDate>Thu, 26 Jan 2012 02:37:51 +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 SELECT Entries Before NOW()</title>
		<link>http://iamseanmurphy.com/2008/02/19/mysql-select-entries-before-now/</link>
		<comments>http://iamseanmurphy.com/2008/02/19/mysql-select-entries-before-now/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 22:57:22 +0000</pubDate>
		<dc:creator>Sean Murphy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[now]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[query cache]]></category>
		<category><![CDATA[rounded time]]></category>

		<guid isPermaLink="false">http://iamseanmurphy.com/2008/02/19/mysql-select-entries-before-now/</guid>
		<description><![CDATA[I’m in the business of making things faster. Using NOW() in a SQL query is something I’m going to complain about. Here’s a familiar scenario from the online publishing industry where future dating articles is a commonality: You have a news site. You need to display only articles that have been published, and one of [...]]]></description>
			<content:encoded><![CDATA[<p>I’m in the business of making things faster. Using NOW() in a SQL query is something I’m going to complain about. Here’s a familiar scenario from the online publishing industry where future dating articles is a commonality:</p>
<p>You have a news site. You need to display only articles that have been published, and one of the criteria is that they need to have a publish_date before now. Easy, peasy, lemon squeezy.</p>
<p><span id="more-3"></span>
<pre name="code" class="sql">SELECT author, title, body FROM articles WHERE publish_date &lt;= NOW();</pre>
<p>That works, right? Yeeeeah, it works, but it isn’t <em>optimal</em>. The problem is that MySQL can’t use the query cache on any query that has NOW() in it (or CURRENT_TIME() or any of <a href="http://dev.mysql.com/doc/refman/5.0/en/query-cache-how.html" title="How MySQL Query Cache Works">these other functions</a> for that matter).  The solution I like to use is have PHP generate the timestamp. Even better is to have PHP round the timestamp, like so:</p>
<pre name="code" class="php">// Calculate time to nearest 15 minutes
$roundness = 60 * 15;
$rounded_now = (round(time() / $roundness) * $roundness);
$sql = "SELECT author, title, body FROM articles WHERE publish_date &lt;= $rounded_now";</pre>
<p>Of course, depending on how time sensitive your application is, you may need to change the code from rounding to 15 minutes to something like 5 minutes, or 1 minute. Hey, even rounding to 30 seconds would be better than using NOW() because you can use query cache!</p>
]]></content:encoded>
			<wfw:commentRss>http://iamseanmurphy.com/2008/02/19/mysql-select-entries-before-now/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

