<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Holding Out for a [MySQL] Hero</title>
	<atom:link href="http://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/</link>
	<description>Thoughts, rants, and even some code from the mind of Barney Boisvert.</description>
	<lastBuildDate>Thu, 11 Sep 2014 09:58:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/comment-page-1/#comment-175547</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Wed, 29 Apr 2009 00:11:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=887#comment-175547</guid>
		<description>anthony, here&#039;s a tweaked version of your query that has the correct behaviour.  Slightly slower than your original because of the extra layer.

&lt;pre&gt;
select raw.timestamp, cd.`count`
from (
  select ts.ts as timestamp,
    max(d.timestamp) as datumTs
  from (
      select &#039;2008-04-27 8:00:00&#039; + interval 20 * num minute as ts,
        &#039;2008-04-27 8:00:00&#039; + interval 20 * (num - 1) minute as pts
      from sequence
      where num between 0 and 12
    ) ts
    left outer join (
      select timestamp
      from count_datum
      where seriesId = 47
    ) d on d.timestamp &gt; ts.pts
      and d.timestamp &lt;= ts.ts
  group by ts.ts
) raw
left outer join count_datum cd on cd.seriesId = 47
  and cd.timestamp = raw.datumTs
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>anthony, here's a tweaked version of your query that has the correct behaviour.  Slightly slower than your original because of the extra layer.</p>
<pre>
select raw.timestamp, cd.`count`
from (
  select ts.ts as timestamp,
    max(d.timestamp) as datumTs
  from (
      select '2008-04-27 8:00:00' + interval 20 * num minute as ts,
        '2008-04-27 8:00:00' + interval 20 * (num - 1) minute as pts
      from sequence
      where num between 0 and 12
    ) ts
    left outer join (
      select timestamp
      from count_datum
      where seriesId = 47
    ) d on d.timestamp &gt; ts.pts
      and d.timestamp &lt;= ts.ts
  group by ts.ts
) raw
left outer join count_datum cd on cd.seriesId = 47
  and cd.timestamp = raw.datumTs
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/comment-page-1/#comment-175539</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Tue, 28 Apr 2009 22:19:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=887#comment-175539</guid>
		<description>anthony,

That&#039;s certainly faster that the double comparison, but noticeably slower than the single comparison, and it doesn&#039;t do quite the same thing.  It returns the maximum `count` value for each window in time, not the last `count` value.  E.g., for the &#039;2009-04-27 11:00:00&#039; record your query would return 8, not 6.

A different approach, though, so I&#039;ll play with it some more and see if I can get it to do the right thing.</description>
		<content:encoded><![CDATA[<p>anthony,</p>
<p>That's certainly faster that the double comparison, but noticeably slower than the single comparison, and it doesn't do quite the same thing.  It returns the maximum `count` value for each window in time, not the last `count` value.  E.g., for the '2009-04-27 11:00:00' record your query would return 8, not 6.</p>
<p>A different approach, though, so I'll play with it some more and see if I can get it to do the right thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anthony</title>
		<link>https://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/comment-page-1/#comment-175535</link>
		<dc:creator>anthony</dc:creator>
		<pubDate>Tue, 28 Apr 2009 22:03:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=887#comment-175535</guid>
		<description>I&#039;m not a mysql expert and I only briefly looked at the SQL, but couldn&#039;t you use a left join and a subselect instead of a subselect in your select statement?  I think if it&#039;s in your select it runs the query with every row (maybe?).

Something like this?  I didnt test this, so it might not even work.

&lt;pre&gt;
select ts.ts as timestamp,       max(`counted`.`count`) as `count`
from (
    select &#039;2009-04-27 8:00:00&#039; + interval 20 * num minute as ts,
      &#039;2009-04-27 8:00:00&#039; + interval 20 * (num - 1) minute as pts
    from sequence
    where num between 0 and 12
  ) ts left join

  (
    select `count`, `timestamp`
    from count_datum
    where seriesId = 47
    order by timestamp desc
  ) as `counted` on
    `counted`.`timestamp` &lt;= ts.ts and `counted`.`timestamp` &gt; ts.pts
group by ts.ts
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I'm not a mysql expert and I only briefly looked at the SQL, but couldn't you use a left join and a subselect instead of a subselect in your select statement?  I think if it's in your select it runs the query with every row (maybe?).</p>
<p>Something like this?  I didnt test this, so it might not even work.</p>
<pre>
select ts.ts as timestamp,       max(`counted`.`count`) as `count`
from (
    select '2009-04-27 8:00:00' + interval 20 * num minute as ts,
      '2009-04-27 8:00:00' + interval 20 * (num - 1) minute as pts
    from sequence
    where num between 0 and 12
  ) ts left join

  (
    select `count`, `timestamp`
    from count_datum
    where seriesId = 47
    order by timestamp desc
  ) as `counted` on
    `counted`.`timestamp` &lt;= ts.ts and `counted`.`timestamp` &gt; ts.pts
group by ts.ts
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Zack</title>
		<link>https://www.barneyb.com/barneyblog/2009/04/27/holding-out-for-a-mysql-hero/comment-page-1/#comment-175497</link>
		<dc:creator>Joe Zack</dc:creator>
		<pubDate>Tue, 28 Apr 2009 15:50:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=887#comment-175497</guid>
		<description>Watching this!</description>
		<content:encoded><![CDATA[<p>Watching this!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
