<?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"
	>
<channel>
	<title>Comments on: MySQL challenge: LIMIT rows accessed, not rows returned</title>
	<atom:link href="http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/</link>
	<description>Stay curious!</description>
	<pubDate>Tue, 06 Jan 2009 08:13:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Diseño Web Peru</title>
		<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-15225</link>
		<dc:creator>Diseño Web Peru</dc:creator>
		<pubDate>Sun, 12 Oct 2008 20:40:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-15225</guid>
		<description>the corect code is 

SELECT * FROM  pregunta    order by  idpre desc limit 0,10</description>
		<content:encoded><![CDATA[<p>the corect code is </p>
<p>SELECT * FROM  pregunta    order by  idpre desc limit 0,10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sheeri Cabral</title>
		<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14846</link>
		<dc:creator>Sheeri Cabral</dc:creator>
		<pubDate>Tue, 01 Jul 2008 17:05:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14846</guid>
		<description>Rob -- in fact, I was aware.  That's why I said MySQL doesn't have it "yet", because indexes on functions and other similar features that stem from materializing data is something I think people will request/demand/pay for in MySQL sooner rather than later.</description>
		<content:encoded><![CDATA[<p>Rob &#8212; in fact, I was aware.  That&#8217;s why I said MySQL doesn&#8217;t have it &#8220;yet&#8221;, because indexes on functions and other similar features that stem from materializing data is something I think people will request/demand/pay for in MySQL sooner rather than later.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Wultsch</title>
		<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14845</link>
		<dc:creator>Rob Wultsch</dc:creator>
		<pubDate>Tue, 01 Jul 2008 12:03:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14845</guid>
		<description>Sheeri:
Your probably aware, but I think it is worth note just in case you are not that Postgres supports indexs on a function of a column. 
http://www.postgresql.org/docs/8.3/interactive/indexes-expressional.html
IIRC Oracle supports something similar as well.</description>
		<content:encoded><![CDATA[<p>Sheeri:<br />
Your probably aware, but I think it is worth note just in case you are not that Postgres supports indexs on a function of a column.<br />
<a href="http://www.postgresql.org/docs/8.3/interactive/indexes-expressional.html" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/www.postgresql.org');">http://www.postgresql.org/docs/8.3/interactive/indexes-expressional.html</a><br />
IIRC Oracle supports something similar as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sheeri Cabral</title>
		<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14844</link>
		<dc:creator>Sheeri Cabral</dc:creator>
		<pubDate>Tue, 01 Jul 2008 10:32:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14844</guid>
		<description>The biggest problem is that even though you're looking at an index on a column, what you really want is an index on a function of a column -- in this case, you want an index on (actor_id % 5).

As this is for an external tool I do not know how much schema changing you can do, nor do I know how much performance you want to sacrifice by having a copy of a table where you can index what you *really* want to index.

Subqueries won't help because MySQL doesn't support LIMIT in subqueries; what you'd really like is something like

explain select actor_id
 from actor
 where actor_id in (select actor_id from actor limit 20) 
   and actor_id % 5 = 0;

but that's not possible with MySQL just yet.

Another way to look at it is using relational calculus.  What you want is the intersection of 2 tables:

SELECT tbl1.actor_id FROM actor as tbl1 WHERE actor_id % 5 = 0 limit 5;
SELECT tbl2.actor_id FROM actor as tbl2 LIMIT 20;

And you can't really have 2 LIMITs in one query.

Hacks like the max_concat trick might work.  Another way to look at it is to see the query as "the top x values in a group" except it's not the "top" x values.

If this were a client I'd recommend having a separate column that's actor_id % 5 and put an index on that; although given the generic nature of your issue, that may not be the rightest solution.</description>
		<content:encoded><![CDATA[<p>The biggest problem is that even though you&#8217;re looking at an index on a column, what you really want is an index on a function of a column &#8212; in this case, you want an index on (actor_id % 5).</p>
<p>As this is for an external tool I do not know how much schema changing you can do, nor do I know how much performance you want to sacrifice by having a copy of a table where you can index what you *really* want to index.</p>
<p>Subqueries won&#8217;t help because MySQL doesn&#8217;t support LIMIT in subqueries; what you&#8217;d really like is something like</p>
<p>explain select actor_id<br />
 from actor<br />
 where actor_id in (select actor_id from actor limit 20)<br />
   and actor_id % 5 = 0;</p>
<p>but that&#8217;s not possible with MySQL just yet.</p>
<p>Another way to look at it is using relational calculus.  What you want is the intersection of 2 tables:</p>
<p>SELECT tbl1.actor_id FROM actor as tbl1 WHERE actor_id % 5 = 0 limit 5;<br />
SELECT tbl2.actor_id FROM actor as tbl2 LIMIT 20;</p>
<p>And you can&#8217;t really have 2 LIMITs in one query.</p>
<p>Hacks like the max_concat trick might work.  Another way to look at it is to see the query as &#8220;the top x values in a group&#8221; except it&#8217;s not the &#8220;top&#8221; x values.</p>
<p>If this were a client I&#8217;d recommend having a separate column that&#8217;s actor_id % 5 and put an index on that; although given the generic nature of your issue, that may not be the rightest solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14842</link>
		<dc:creator>Xaprb</dc:creator>
		<pubDate>Tue, 01 Jul 2008 04:55:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/2008/06/28/mysql-challenge-limit-rows-accessed-not-rows-returned/#comment-14842</guid>
		<description>That's an excellent point.  I had a TODO note to build some code that could figure out when HANDLER is appropriate to use.  It has some limitations of course.</description>
		<content:encoded><![CDATA[<p>That&#8217;s an excellent point.  I had a TODO note to build some code that could figure out when HANDLER is appropriate to use.  It has some limitations of course.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
