<?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: Possible optimization for sort_merge and UNION ORDER BY LIMIT</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/</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: techfounder &#187; Optimizing OR (union) operations in MySQL</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-362006</link>
		<dc:creator>techfounder &#187; Optimizing OR (union) operations in MySQL</dc:creator>
		<pubDate>Wed, 15 Oct 2008 06:12:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-362006</guid>
		<description>[...] is replacing our OR condition with two UNION&#8217;ed select statements (the inspiration is from a couple of posts over at the MySQL performance blog). Breaking the original query into a UNION form results [...]</description>
		<content:encoded><![CDATA[<p>[...] is replacing our OR condition with two UNION&#8217;ed select statements (the inspiration is from a couple of posts over at the MySQL performance blog). Breaking the original query into a UNION form results [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-318580</link>
		<dc:creator>Ken</dc:creator>
		<pubDate>Fri, 27 Jun 2008 07:45:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-318580</guid>
		<description>I&#039;m creating a shopping cart type system where users can add a bunch of products. It&#039;s important that users can change the display order of products. This way they can showcase certain items on top.

All of this is stored in the database obviously.

There is a problem though, and it&#039;s with the sort order storage in the database. For instance, if I have 5 products, I can set it like this:

01 A
10 B
20 C
30 D
40 E

The number is the &quot;sort&quot; field, and the letter is the &quot;product&quot; field. I&#039;d then set an index for the &quot;sort&quot; field and the listings would be fast.

Now, suppose I wanted to re-order E to position number two (right now it&#039;s position number 5). I could do this:

01 A
05 E
10 B
20 C
30 D

The problem here is, if I keep re-sorting, I&#039;d run out of numbers in between and will have to re-order everything. This can be a potential nightmare if I have thousands of products.

Is there a better way of doing this?</description>
		<content:encoded><![CDATA[<p>I&#8217;m creating a shopping cart type system where users can add a bunch of products. It&#8217;s important that users can change the display order of products. This way they can showcase certain items on top.</p>
<p>All of this is stored in the database obviously.</p>
<p>There is a problem though, and it&#8217;s with the sort order storage in the database. For instance, if I have 5 products, I can set it like this:</p>
<p>01 A<br />
10 B<br />
20 C<br />
30 D<br />
40 E</p>
<p>The number is the &#8220;sort&#8221; field, and the letter is the &#8220;product&#8221; field. I&#8217;d then set an index for the &#8220;sort&#8221; field and the listings would be fast.</p>
<p>Now, suppose I wanted to re-order E to position number two (right now it&#8217;s position number 5). I could do this:</p>
<p>01 A<br />
05 E<br />
10 B<br />
20 C<br />
30 D</p>
<p>The problem here is, if I keep re-sorting, I&#8217;d run out of numbers in between and will have to re-order everything. This can be a potential nightmare if I have thousands of products.</p>
<p>Is there a better way of doing this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepali</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-240352</link>
		<dc:creator>Deepali</dc:creator>
		<pubDate>Fri, 08 Feb 2008 06:37:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-240352</guid>
		<description>thanks a lot i was searching for one of my query, i would love to specially thanks to Devid. because i just wanted query which he has posted here.
many thanks.</description>
		<content:encoded><![CDATA[<p>thanks a lot i was searching for one of my query, i would love to specially thanks to Devid. because i just wanted query which he has posted here.<br />
many thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: UNION vs UNION ALL Performance &#124; MySQL Performance Blog</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-175116</link>
		<dc:creator>UNION vs UNION ALL Performance &#124; MySQL Performance Blog</dc:creator>
		<pubDate>Fri, 05 Oct 2007 21:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-175116</guid>
		<description>[...] I was comparing performance of UNION vs MySQL 5.0 index merge algorithm Sinisa pointed out I should be using UNION ALL instead of simple UNION in my benchmarks, and he was [...]</description>
		<content:encoded><![CDATA[<p>[...] I was comparing performance of UNION vs MySQL 5.0 index merge algorithm Sinisa pointed out I should be using UNION ALL instead of simple UNION in my benchmarks, and he was [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-170046</link>
		<dc:creator>David</dc:creator>
		<pubDate>Thu, 20 Sep 2007 01:11:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-170046</guid>
		<description>Peter,
Sorry I made a mistake. Finally the query 
(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10,takes only 0.03</description>
		<content:encoded><![CDATA[<p>Peter,<br />
Sorry I made a mistake. Finally the query<br />
(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10,takes only 0.03</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sinisa Milivojevic</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-169868</link>
		<dc:creator>Sinisa Milivojevic</dc:creator>
		<pubDate>Wed, 19 Sep 2007 13:58:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-169868</guid>
		<description>Peter,

You are right about rows with A=5 and B=5 being included twice, but you can add ... AND B != 5 .. etc... It would still be significantly faster then with UNION DISTINCT.</description>
		<content:encoded><![CDATA[<p>Peter,</p>
<p>You are right about rows with A=5 and B=5 being included twice, but you can add &#8230; AND B != 5 .. etc&#8230; It would still be significantly faster then with UNION DISTINCT.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-169774</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Wed, 19 Sep 2007 09:35:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-169774</guid>
		<description>David,

Can you do EXPLAIN on your queries ? 

0.25 sec is just very wrong time for such query as all what needs to be done in second query is fetching 10 rows by one index when 10 rows by another index and them sorting them.</description>
		<content:encoded><![CDATA[<p>David,</p>
<p>Can you do EXPLAIN on your queries ? </p>
<p>0.25 sec is just very wrong time for such query as all what needs to be done in second query is fetching 10 rows by one index when 10 rows by another index and them sorting them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-169678</link>
		<dc:creator>David</dc:creator>
		<pubDate>Wed, 19 Sep 2007 03:19:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-169678</guid>
		<description>testing on my notebook and utest with more than 1,000,000 rows, with mysql 5.0.45

(SELECT * FROM utest WHERE c1=5) union (SELECT * FROM utest WHERE c2=5) ORDER BY ord DESC LIMIT 10;
10 rows in set (0.31 sec)

(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10
10 rows in set (0.25 sec)

the speed is not so big differenet than you did</description>
		<content:encoded><![CDATA[<p>testing on my notebook and utest with more than 1,000,000 rows, with mysql 5.0.45</p>
<p>(SELECT * FROM utest WHERE c1=5) union (SELECT * FROM utest WHERE c2=5) ORDER BY ord DESC LIMIT 10;<br />
10 rows in set (0.31 sec)</p>
<p>(SELECT * FROM utest WHERE c1=5 ORDER BY ord DESC LIMIT 10) union (SELECT * FROM utest WHERE c2=5 ORDER BY ord DESC LIMIT 10) ORDER BY ord DESC LIMIT 10<br />
10 rows in set (0.25 sec)</p>
<p>the speed is not so big differenet than you did</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-169556</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Tue, 18 Sep 2007 18:22:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-169556</guid>
		<description>Yes, Sinisa I know 

But this is closer to what A=5 or B=5 does.    If you have UNION ALL    rows which have A=5 and B=5 will be included twice in result set :)</description>
		<content:encoded><![CDATA[<p>Yes, Sinisa I know </p>
<p>But this is closer to what A=5 or B=5 does.    If you have UNION ALL    rows which have A=5 and B=5 will be included twice in result set <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sinisa Milivojevic</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/comment-page-1/#comment-169542</link>
		<dc:creator>Sinisa Milivojevic</dc:creator>
		<pubDate>Tue, 18 Sep 2007 17:00:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/18/possible-optimization-for-sort_merge-and-union-order-by-limit/#comment-169542</guid>
		<description>Peter,

Sorry, but you made a very small mistake .... It should be UNION ALL and not just UNION . As you know, UNION without attribute is UNION DISTINCT.</description>
		<content:encoded><![CDATA[<p>Peter,</p>
<p>Sorry, but you made a very small mistake &#8230;. It should be UNION ALL and not just UNION . As you know, UNION without attribute is UNION DISTINCT.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
