<?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: Using delayed JOIN to optimize count(*) and LIMIT queries</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Praca</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-490123</link>
		<dc:creator>Praca</dc:creator>
		<pubDate>Thu, 26 Feb 2009 10:20:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-490123</guid>
		<description>To optimize your mysql queries use LIMIT on the end in query:)

ex. $sql = &quot;SELECT id FROM users where email = &#039;uniq@email.com&#039; LIMIT 1&quot;;</description>
		<content:encoded><![CDATA[<p>To optimize your mysql queries use LIMIT on the end in query:)</p>
<p>ex. $sql = &#8220;SELECT id FROM users where email = &#8216;uniq@email.com&#8217; LIMIT 1&#8243;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Added by a PAL to FAQ PAL</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-363327</link>
		<dc:creator>Added by a PAL to FAQ PAL</dc:creator>
		<pubDate>Sun, 19 Oct 2008 23:04:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-363327</guid>
		<description>&lt;strong&gt;Using delayed JOIN to optimize count(*) and LIMIT queries...&lt;/strong&gt;

If you&#039;re executing count(*) queries for such result sets MySQL will perform the join even if you use LEFT JOIN so it is not needed which slows down things considerably. In similar way MySQL generates full rows while executing queries with limit befor...</description>
		<content:encoded><![CDATA[<p><strong>Using delayed JOIN to optimize count(*) and LIMIT queries&#8230;</strong></p>
<p>If you&#8217;re executing count(*) queries for such result sets MySQL will perform the join even if you use LEFT JOIN so it is not needed which slows down things considerably. In similar way MySQL generates full rows while executing queries with limit befor&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: benlumley.co.uk - &#187; MySQL Query Optimiser Left Lacking &#8230;..</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-317515</link>
		<dc:creator>benlumley.co.uk - &#187; MySQL Query Optimiser Left Lacking &#8230;..</dc:creator>
		<pubDate>Wed, 25 Jun 2008 08:04:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-317515</guid>
		<description>[...] http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-querie... [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-querie.." rel="nofollow">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-querie..</a>. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-160938</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 30 Aug 2007 20:20:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-160938</guid>
		<description>Sure. You have to ensure the number of rows do not change to use this optimization.  In other cases - like if you&#039;re join to the same table it is just granted.</description>
		<content:encoded><![CDATA[<p>Sure. You have to ensure the number of rows do not change to use this optimization.  In other cases &#8211; like if you&#8217;re join to the same table it is just granted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boia Alexandru</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-158549</link>
		<dc:creator>Boia Alexandru</dc:creator>
		<pubDate>Thu, 23 Aug 2007 12:41:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-158549</guid>
		<description>Sorry for reposting:)...but it seems that your comment parser acts a little weard:P

While this works when performing LEFT JOINS, with INNER JOINS it&#039;s a different thing.
Let&#039;s say we want 10 records, starting from offset 500000, where i &amp; 10000, like in the above example, but let&#039;s perform an innerj join instead...
The first subquery will return the requested 10 results, and asuming in the res table there are rows that don&#039;t match some rows returned by the subquery, then the result will be less than 10 rows...</description>
		<content:encoded><![CDATA[<p>Sorry for reposting:)&#8230;but it seems that your comment parser acts a little weard:P</p>
<p>While this works when performing LEFT JOINS, with INNER JOINS it&#8217;s a different thing.<br />
Let&#8217;s say we want 10 records, starting from offset 500000, where i &amp; 10000, like in the above example, but let&#8217;s perform an innerj join instead&#8230;<br />
The first subquery will return the requested 10 results, and asuming in the res table there are rows that don&#8217;t match some rows returned by the subquery, then the result will be less than 10 rows&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHP &#38; MySQL Optimierungtips - CoreBlog</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-156114</link>
		<dc:creator>PHP &#38; MySQL Optimierungtips - CoreBlog</dc:creator>
		<pubDate>Wed, 15 Aug 2007 11:59:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-156114</guid>
		<description>[...] Using delayed JOIN to optimize count(*) and LIMIT queries [...]</description>
		<content:encoded><![CDATA[<p>[...] Using delayed JOIN to optimize count(*) and LIMIT queries [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: i.ndustrio.us - &#187; Optimize MySQL queries w/delayed JOIN &#38; LIMIT</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-137123</link>
		<dc:creator>i.ndustrio.us - &#187; Optimize MySQL queries w/delayed JOIN &#38; LIMIT</dc:creator>
		<pubDate>Tue, 19 Jun 2007 05:58:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-137123</guid>
		<description>[...] Perfomancing Blog had a nice article on using delayed JOIN to optimize count(*) and LIMIT queries. I just personally optimized a couple of nagging queries with the &#8220;delayed limit&#8221; [...]</description>
		<content:encoded><![CDATA[<p>[...] Perfomancing Blog had a nice article on using delayed JOIN to optimize count(*) and LIMIT queries. I just personally optimized a couple of nagging queries with the &#8220;delayed limit&#8221; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#187; Easy MySQL Performance Tips</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-132965</link>
		<dc:creator>&#187; Easy MySQL Performance Tips</dc:creator>
		<pubDate>Wed, 06 Jun 2007 12:12:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-132965</guid>
		<description>[...] joining two large tables, but only searching against one, put the join statement at the end. Why join the two entire tables when you only have to join the matching [...]</description>
		<content:encoded><![CDATA[<p>[...] joining two large tables, but only searching against one, put the join statement at the end. Why join the two entire tables when you only have to join the matching [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: -TMA-1- &#187; links for 2007-04-26</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-114152</link>
		<dc:creator>-TMA-1- &#187; links for 2007-04-26</dc:creator>
		<pubDate>Thu, 26 Apr 2007 00:20:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-114152</guid>
		<description>[...] MySQL Performance Blog » Using delayed JOIN to optimize count(*) and LIMIT queries (tags: Tech Database MySQL Performance Tips) [...]</description>
		<content:encoded><![CDATA[<p>[...] MySQL Performance Blog » Using delayed JOIN to optimize count(*) and LIMIT queries (tags: Tech Database MySQL Performance Tips) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peufeu</title>
		<link>http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/comment-page-1/#comment-109175</link>
		<dc:creator>Peufeu</dc:creator>
		<pubDate>Mon, 16 Apr 2007 10:54:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/#comment-109175</guid>
		<description>I repost since your comment-parser chokes on &quot;greater&quot; and &quot;lower&quot; characters !

Yes I often use this too, in MySQL and in Postgres, it is just one of the tricks of the trade ! I believe the optimizer should be able to move ORDER BY and LIMIT around inside the execution plan by itself but since it isn&#039;t, doing it yourself is the way.

Recently I had a huge search query (in Postgres) which was horrendously slow. It has various conditions like GPS coordinates, date, item type, etc, and the all-time favourite, union from a huge archive table and an active table, plus a 5-way JOIN with a few aggregates.

The archive + active is quite common so I&#039;ll drop my way of doing it :

- first query the active table and store the results in a temporary table.
- since i use ORDER BY + LIMIT, I can then compute the max() or min() (depending on the order) or the relevant row
- then I query the archive table, but I know that rows which have the ORDER BY field greater  (or lower depending on the order) to the value computed above will not appear in the final result set since the first query from the active table already gave me enough rows to satisfy my LIMIT. I then add a WHERE condition to filter out the rows BEFORE sorting them. Postgres will bitmap-index-scan on a boolean OR/AND mix of my indexes so it will never even look at those rows that don&#039;t match.
- I insert the results of this query in the temp table
- I sort and limit the temp table and fetch it
- I perform all my joins from the temp table (no freaky IN() from php)

It went from a few minutes to a few tens of milliseconds. Not bad I may say. Use this trick if you need ! It works very very well with MySQL too.</description>
		<content:encoded><![CDATA[<p>I repost since your comment-parser chokes on &#8220;greater&#8221; and &#8220;lower&#8221; characters !</p>
<p>Yes I often use this too, in MySQL and in Postgres, it is just one of the tricks of the trade ! I believe the optimizer should be able to move ORDER BY and LIMIT around inside the execution plan by itself but since it isn&#8217;t, doing it yourself is the way.</p>
<p>Recently I had a huge search query (in Postgres) which was horrendously slow. It has various conditions like GPS coordinates, date, item type, etc, and the all-time favourite, union from a huge archive table and an active table, plus a 5-way JOIN with a few aggregates.</p>
<p>The archive + active is quite common so I&#8217;ll drop my way of doing it :</p>
<p>- first query the active table and store the results in a temporary table.<br />
- since i use ORDER BY + LIMIT, I can then compute the max() or min() (depending on the order) or the relevant row<br />
- then I query the archive table, but I know that rows which have the ORDER BY field greater  (or lower depending on the order) to the value computed above will not appear in the final result set since the first query from the active table already gave me enough rows to satisfy my LIMIT. I then add a WHERE condition to filter out the rows BEFORE sorting them. Postgres will bitmap-index-scan on a boolean OR/AND mix of my indexes so it will never even look at those rows that don&#8217;t match.<br />
- I insert the results of this query in the temp table<br />
- I sort and limit the temp table and fetch it<br />
- I perform all my joins from the temp table (no freaky IN() from php)</p>
<p>It went from a few minutes to a few tens of milliseconds. Not bad I may say. Use this trick if you need ! It works very very well with MySQL too.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
