<?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: ORDER BY &#8230; LIMIT Performance Optimization</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/</link>
	<description>Everything about MySQL Performance</description>
	<pubDate>Tue, 02 Dec 2008 13:41:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: noob</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-376802</link>
		<dc:creator>noob</dc:creator>
		<pubDate>Wed, 12 Nov 2008 22:11:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-376802</guid>
		<description>Hi, was wondering if there is a way to stop a SELECT COUNT(*) from continuing once it reached a max number of rows. Eg, i want to do something like:

SELECT count(*) FROM table_a LIMIT 1000;

So if the table has more than 1k rows, it should just return and say "1000". What i'm trying to avoid is have the entire table scanned, in particular if it has millions of rows. Basically the question i want to ask is: "Does table X have at least N rows?".</description>
		<content:encoded><![CDATA[<p>Hi, was wondering if there is a way to stop a SELECT COUNT(*) from continuing once it reached a max number of rows. Eg, i want to do something like:</p>
<p>SELECT count(*) FROM table_a LIMIT 1000;</p>
<p>So if the table has more than 1k rows, it should just return and say &#8220;1000&#8243;. What i&#8217;m trying to avoid is have the entire table scanned, in particular if it has millions of rows. Basically the question i want to ask is: &#8220;Does table X have at least N rows?&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365944</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Sun, 26 Oct 2008 20:43:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365944</guid>
		<description>Hey Radek,

Yes, I was talking about this: "Typically we do a count first to create all the pagination stuff. Current page, total pages, etc." - you have to create a counter for pagination information.

Thank you for your solution, maybe I will try it on other projects. Right now I'm fine with solution posted above by me.</description>
		<content:encoded><![CDATA[<p>Hey Radek,</p>
<p>Yes, I was talking about this: &#8220;Typically we do a count first to create all the pagination stuff. Current page, total pages, etc.&#8221; - you have to create a counter for pagination information.</p>
<p>Thank you for your solution, maybe I will try it on other projects. Right now I&#8217;m fine with solution posted above by me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radek</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365016</link>
		<dc:creator>Radek</dc:creator>
		<pubDate>Fri, 24 Oct 2008 21:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365016</guid>
		<description>Yes Sergio, You dont need to do the count as usually you have the page number or offset in the get varaible as you go from page to page. Typically we do a count first to create all the pagination stuff.  Current page, total pages, etc.

Example of it in action here.  http://www.pinkbike.com/photo/list/?date=all&#38;page=6
You can page to page 100,000 (24 per page, so the offset is 2.4 million) without and slow down.  If this was done in one call it literally takes seconds to generate offsets of 2,000,000

Yes, you can order by in the first query by id no problem.  The id is indexed so no penalty there.  Also your result set can be ordered by also but that is fast as you only have 24 items, or however many you show on the page.

Cheers</description>
		<content:encoded><![CDATA[<p>Yes Sergio, You dont need to do the count as usually you have the page number or offset in the get varaible as you go from page to page. Typically we do a count first to create all the pagination stuff.  Current page, total pages, etc.</p>
<p>Example of it in action here.  <a href="http://www.pinkbike.com/photo/list/?date=all&amp;page=6" rel="nofollow">http://www.pinkbike.com/photo/list/?date=all&amp;page=6</a><br />
You can page to page 100,000 (24 per page, so the offset is 2.4 million) without and slow down.  If this was done in one call it literally takes seconds to generate offsets of 2,000,000</p>
<p>Yes, you can order by in the first query by id no problem.  The id is indexed so no penalty there.  Also your result set can be ordered by also but that is fast as you only have 24 items, or however many you show on the page.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365000</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Fri, 24 Oct 2008 20:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-365000</guid>
		<description>Radek, and what about if I want to order the resultset?
eg: select id .... order by id desc .. 

Have you tried?

I'm Joining multiple tables, so the join must be done in the second query..

Unfortunately, with your solution I have to use 3 queries:  
- SELECT COUNT(*) .. (for total rows that meets the criteria blabla)
- SELECT id ... LIMIT x,y 
- SELECT id, name .. IN (x1,x2…x20)

Anyway.. thank you for the tweak!</description>
		<content:encoded><![CDATA[<p>Radek, and what about if I want to order the resultset?<br />
eg: select id &#8230;. order by id desc .. </p>
<p>Have you tried?</p>
<p>I&#8217;m Joining multiple tables, so the join must be done in the second query..</p>
<p>Unfortunately, with your solution I have to use 3 queries:<br />
- SELECT COUNT(*) .. (for total rows that meets the criteria blabla)<br />
- SELECT id &#8230; LIMIT x,y<br />
- SELECT id, name .. IN (x1,x2…x20)</p>
<p>Anyway.. thank you for the tweak!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radek</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-361997</link>
		<dc:creator>Radek</dc:creator>
		<pubDate>Wed, 15 Oct 2008 04:30:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-361997</guid>
		<description>One tweak that we do for large limit offsets on pagination is to first select columns that are part of the index.  Typically this would be the id's.  After that we use an "IN" to select the the other data columns from the same table.  We find that there is a vast improvement on selects that use large offsets.

Example (category,id index)
select photo.id, photo.description from photo where category=1 limit 30000000,20 
Query time: 4 seconds

To speed things up we split the call into 2 queries

select photo.id from photo where category=1 limit 30000000,20
Query time: 0.030  (this just uses the index so no traversing the data table) 
Select photo.id, photo.description from photo where photo.id IN (x1,x2...x20)              
The x1 - x20 are the results the first query returns
Query time: 0.020 

So with a simple change like this we sped things up by a couple of orders of magnitude and allow our users to use large pagination.

I assume it has something to do that the the first query only uses the index to get the ids and does not have to jump around getting a larger data set.

Cheers.</description>
		<content:encoded><![CDATA[<p>One tweak that we do for large limit offsets on pagination is to first select columns that are part of the index.  Typically this would be the id&#8217;s.  After that we use an &#8220;IN&#8221; to select the the other data columns from the same table.  We find that there is a vast improvement on selects that use large offsets.</p>
<p>Example (category,id index)<br />
select photo.id, photo.description from photo where category=1 limit 30000000,20<br />
Query time: 4 seconds</p>
<p>To speed things up we split the call into 2 queries</p>
<p>select photo.id from photo where category=1 limit 30000000,20<br />
Query time: 0.030  (this just uses the index so no traversing the data table)<br />
Select photo.id, photo.description from photo where photo.id IN (x1,x2&#8230;x20)<br />
The x1 - x20 are the results the first query returns<br />
Query time: 0.020 </p>
<p>So with a simple change like this we sped things up by a couple of orders of magnitude and allow our users to use large pagination.</p>
<p>I assume it has something to do that the the first query only uses the index to get the ids and does not have to jump around getting a larger data set.</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-337907</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 31 Jul 2008 12:33:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-337907</guid>
		<description>I have this query, but the orderby and the last 2 left joins affect considerably to the performance.
Any idea how I can optimize this query?

Thanks.

SELECT
       *
FROM
       message

       LEFT JOIN
       accounts a ON message_account = a.id

       LEFT JOIN
       opportunities o ON message_project = o.id

       LEFT JOIN
       leads l1 ON (UCASE(message_from) LIKE CONCAT(\'%\', UCASE(l1.email1), \'%\')
AND l1.email1  \'\') OR (UCASE(message_from) LIKE CONCAT(\'%\',
UCASE(l1.email2), \'%\') AND l1.email2  \'\')

       LEFT JOIN
       leads l2 ON (UCASE(message_to) LIKE CONCAT(\'%\', UCASE(l2.email1), \'%\')
AND l2.email1  \'\') OR (UCASE(message_to) LIKE CONCAT(\'%\',
UCASE(l2.email2), \'%\') AND l2.email2  \'\')

WHERE
       message_visibility
       AND message_mailbox = UCASE(\'user\')
       AND message_visibility  1

ORDERBY
       message_ocode DESC</description>
		<content:encoded><![CDATA[<p>I have this query, but the orderby and the last 2 left joins affect considerably to the performance.<br />
Any idea how I can optimize this query?</p>
<p>Thanks.</p>
<p>SELECT<br />
       *<br />
FROM<br />
       message</p>
<p>       LEFT JOIN<br />
       accounts a ON message_account = a.id</p>
<p>       LEFT JOIN<br />
       opportunities o ON message_project = o.id</p>
<p>       LEFT JOIN<br />
       leads l1 ON (UCASE(message_from) LIKE CONCAT(\&#8217;%\&#8217;, UCASE(l1.email1), \&#8217;%\&#8217;)<br />
AND l1.email1  \&#8217;\') OR (UCASE(message_from) LIKE CONCAT(\&#8217;%\&#8217;,<br />
UCASE(l1.email2), \&#8217;%\&#8217;) AND l1.email2  \&#8217;\')</p>
<p>       LEFT JOIN<br />
       leads l2 ON (UCASE(message_to) LIKE CONCAT(\&#8217;%\&#8217;, UCASE(l2.email1), \&#8217;%\&#8217;)<br />
AND l2.email1  \&#8217;\') OR (UCASE(message_to) LIKE CONCAT(\&#8217;%\&#8217;,<br />
UCASE(l2.email2), \&#8217;%\&#8217;) AND l2.email2  \&#8217;\')</p>
<p>WHERE<br />
       message_visibility<br />
       AND message_mailbox = UCASE(\&#8217;user\&#8217;)<br />
       AND message_visibility  1</p>
<p>ORDERBY<br />
       message_ocode DESC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-330195</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Thu, 17 Jul 2008 13:48:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-330195</guid>
		<description>Here SELECT MAX(post_id) FROM (SELECT ...) is not necessary. It's redundant 
(it's obvious 'cos that subquery extracts only one post_id - see LIMIT x, 1) 

SELECT *
FROM forum_posts AS pa
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id
LEFT JOIN forum_users ON user_id = post_poster
WHERE post_topic_id = '224'
AND pa.post_id &#62;= (
SELECT post_id
FROM forum_posts
WHERE post_topic_id = '224'
ORDER BY post_id ASC
LIMIT 6075 , 1)
ORDER BY pa.post_id ASC
LIMIT 15</description>
		<content:encoded><![CDATA[<p>Here SELECT MAX(post_id) FROM (SELECT &#8230;) is not necessary. It&#8217;s redundant<br />
(it&#8217;s obvious &#8216;cos that subquery extracts only one post_id - see LIMIT x, 1) </p>
<p>SELECT *<br />
FROM forum_posts AS pa<br />
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id<br />
LEFT JOIN forum_users ON user_id = post_poster<br />
WHERE post_topic_id = &#8216;224&#8242;<br />
AND pa.post_id &gt;= (<br />
SELECT post_id<br />
FROM forum_posts<br />
WHERE post_topic_id = &#8216;224&#8242;<br />
ORDER BY post_id ASC<br />
LIMIT 6075 , 1)<br />
ORDER BY pa.post_id ASC<br />
LIMIT 15</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-329823</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Wed, 16 Jul 2008 23:10:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-329823</guid>
		<description>... LIMIT optimization :)

SELECT * 
FROM forum_posts AS pa
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id
LEFT JOIN forum_users ON user_id = post_poster
WHERE post_topic_id = '450'
ORDER BY pa.post_id ASC 
LIMIT 5475 , 15 

versus

SELECT * 
FROM forum_posts AS pa
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id
LEFT JOIN forum_users ON user_id = post_poster
WHERE post_topic_id = '224'
AND pa.post_id &#62;= ( 
SELECT MAX( post_id ) 
FROM (
SELECT post_id
FROM forum_posts
WHERE post_topic_id = '224'
ORDER BY post_id ASC 
LIMIT 6075 , 1
) AS tmp ) 
ORDER BY pa.post_id ASC 
LIMIT 15 

~ 0.09 sec versus ~ 0.004 sec

Tables with posts: 2x292,000+ rows (25MB + 89MB)
Topic: 6092 rows (rows that meet condition WHERE post_topic_id = '224')

Source: http://www.easemarry.com/blog/mysql-limit-optimization/

If you found something better, I would be glad to hear.</description>
		<content:encoded><![CDATA[<p>&#8230; LIMIT optimization <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>SELECT *<br />
FROM forum_posts AS pa<br />
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id<br />
LEFT JOIN forum_users ON user_id = post_poster<br />
WHERE post_topic_id = &#8216;450&#8242;<br />
ORDER BY pa.post_id ASC<br />
LIMIT 5475 , 15 </p>
<p>versus</p>
<p>SELECT *<br />
FROM forum_posts AS pa<br />
LEFT JOIN forum_posts_text AS pb ON pa.post_id = pb.post_id<br />
LEFT JOIN forum_users ON user_id = post_poster<br />
WHERE post_topic_id = &#8216;224&#8242;<br />
AND pa.post_id &gt;= (<br />
SELECT MAX( post_id )<br />
FROM (<br />
SELECT post_id<br />
FROM forum_posts<br />
WHERE post_topic_id = &#8216;224&#8242;<br />
ORDER BY post_id ASC<br />
LIMIT 6075 , 1<br />
) AS tmp )<br />
ORDER BY pa.post_id ASC<br />
LIMIT 15 </p>
<p>~ 0.09 sec versus ~ 0.004 sec</p>
<p>Tables with posts: 2&#215;292,000+ rows (25MB + 89MB)<br />
Topic: 6092 rows (rows that meet condition WHERE post_topic_id = &#8216;224&#8242;)</p>
<p>Source: <a href="http://www.easemarry.com/blog/mysql-limit-optimization/" rel="nofollow">http://www.easemarry.com/blog/mysql-limit-optimization/</a></p>
<p>If you found something better, I would be glad to hear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prashant</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-326212</link>
		<dc:creator>prashant</dc:creator>
		<pubDate>Fri, 11 Jul 2008 05:49:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-326212</guid>
		<description>How to optimized this query in PHP

$sqls="select id,state from states";
$rss=mysql_query($sqls) or die(mysql_error());
$num_rowss=mysql_num_rows($rss);
						  
$count=1;						

							
while($num_rowss &#38;&#38; $datas=mysql_fetch_assoc($rss))
{
								
$state_id=$datas['id'];
$state=$datas['state'];
$sqlc="select * from companies,package where comp_status=1 and comp_account_site=0 and comp_balance &#62;0 and comp_mem_pac=pac_id order by pac_amount DESC, rand()";
								
$rsc=mysql_query($sqlc) or die(mysql_error());
$num_rowsc=mysql_num_rows($rsc);
							
	$checkonlyonetime=1; // header
	while($num_rowsc &#38;&#38; $datac=mysql_fetch_assoc($rsc)) // loop of company
								{
								  
		$comp_id=$datac['comp_id'];								
									
		$comp_name=$datac['comp_name'];
									$comp_mapped_city=$datac['comp_mapped_city'];
									@$comp_mapped_city=explode(",",trim($comp_mapped_city,","));

      }
}</description>
		<content:encoded><![CDATA[<p>How to optimized this query in PHP</p>
<p>$sqls=&#8221;select id,state from states&#8221;;<br />
$rss=mysql_query($sqls) or die(mysql_error());<br />
$num_rowss=mysql_num_rows($rss);</p>
<p>$count=1;						</p>
<p>while($num_rowss &amp;&amp; $datas=mysql_fetch_assoc($rss))<br />
{</p>
<p>$state_id=$datas['id'];<br />
$state=$datas['state'];<br />
$sqlc=&#8221;select * from companies,package where comp_status=1 and comp_account_site=0 and comp_balance &gt;0 and comp_mem_pac=pac_id order by pac_amount DESC, rand()&#8221;;</p>
<p>$rsc=mysql_query($sqlc) or die(mysql_error());<br />
$num_rowsc=mysql_num_rows($rsc);</p>
<p>	$checkonlyonetime=1; // header<br />
	while($num_rowsc &amp;&amp; $datac=mysql_fetch_assoc($rsc)) // loop of company<br />
								{</p>
<p>		$comp_id=$datac['comp_id'];								</p>
<p>		$comp_name=$datac['comp_name'];<br />
									$comp_mapped_city=$datac['comp_mapped_city'];<br />
									@$comp_mapped_city=explode(&#8221;,&#8221;,trim($comp_mapped_city,&#8221;,&#8221;));</p>
<p>      }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathuvathanan Mou</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-324686</link>
		<dc:creator>Mathuvathanan Mou</dc:creator>
		<pubDate>Tue, 08 Jul 2008 06:14:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-324686</guid>
		<description>Peter, Thanks....

This is where I found very useful matters about optimization which I have been looking for so long.

Thanks again.

Mathu Mou</description>
		<content:encoded><![CDATA[<p>Peter, Thanks&#8230;.</p>
<p>This is where I found very useful matters about optimization which I have been looking for so long.</p>
<p>Thanks again.</p>
<p>Mathu Mou</p>
]]></content:encoded>
	</item>
</channel>
</rss>
