<?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: 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>Percona&#039;s MySQL &#38; InnoDB performance and scalability blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 16:45:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: tim viec lam</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-884523</link>
		<dc:creator>tim viec lam</dc:creator>
		<pubDate>Thu, 02 Feb 2012 18:43:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-884523</guid>
		<description>Thanks alot with your info!</description>
		<content:encoded><![CDATA[<p>Thanks alot with your info!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rbk</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-856194</link>
		<dc:creator>Rbk</dc:creator>
		<pubDate>Tue, 13 Dec 2011 11:27:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-856194</guid>
		<description>Hi Peter,

I have one question regarding  your comment

&quot;Sort by column in leading table if you have JOIN with ORDER BY … LIMIT you should try hard to have sorting column(s) to be in the leading table. If ORDER BY is going by field from the table which is not first in the join order index can’t be used.&quot;

I have a similar situation where I can not have sorting columns in leading table; in such case please suggest any work around.

Thanks in anticipation.
Rbk</description>
		<content:encoded><![CDATA[<p>Hi Peter,</p>
<p>I have one question regarding  your comment</p>
<p>&#8220;Sort by column in leading table if you have JOIN with ORDER BY … LIMIT you should try hard to have sorting column(s) to be in the leading table. If ORDER BY is going by field from the table which is not first in the join order index can’t be used.&#8221;</p>
<p>I have a similar situation where I can not have sorting columns in leading table; in such case please suggest any work around.</p>
<p>Thanks in anticipation.<br />
Rbk</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: james</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-805582</link>
		<dc:creator>james</dc:creator>
		<pubDate>Mon, 25 Apr 2011 09:56:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-805582</guid>
		<description>Hello Peter,

I am going through your article its found pretty much inserting. I need one help as i am displaying my property query which is displaying properly and ordering the result by following 4 column 

p.is_move_up DESC,REPLACE(IFNULL(p.is_feature,&quot;&quot;),&quot;2015-12-31&quot;,&quot;&quot;) DESC,
p.listing_order DESC,p.propid DESC 

In order site, properties are been displayed in this order: Moveup  paid, Paid featured, paid, trial then expired. However, we want to show within each group the properties random and make them rotate. Now, 
the properties do not rotate so it is not fair for users to see always the same properties in same order. So, we need the rotation to be within same group not mixing all.

Query :- 

SELECT DISTINCT(p.propid),p.category_id,p.people_sleep,p.bedrooms,p.bathrooms,p.children,
p.airport,p.features,p.pets,p.smoke, p.wheelchair,p.prop_title,p.property_type,prop_date,
display_calender,other_url_calender,is_feature 
FROM property p 
LEFT JOIN category ON (p.category_id=category.id) 
WHERE p.active IN(&quot;yes&quot;,&quot;d&quot;) AND p.is_feature &gt;= SYSDATE()
ORDER BY p.is_move_up DESC,REPLACE(IFNULL(p.is_feature,&quot;&quot;),&quot;2015-12-31&quot;,&quot;&quot;) DESC,
p.listing_order DESC,p.propid DESC  

Can you please help me into this.</description>
		<content:encoded><![CDATA[<p>Hello Peter,</p>
<p>I am going through your article its found pretty much inserting. I need one help as i am displaying my property query which is displaying properly and ordering the result by following 4 column </p>
<p>p.is_move_up DESC,REPLACE(IFNULL(p.is_feature,&#8221;"),&#8221;2015-12-31&#8243;,&#8221;") DESC,<br />
p.listing_order DESC,p.propid DESC </p>
<p>In order site, properties are been displayed in this order: Moveup  paid, Paid featured, paid, trial then expired. However, we want to show within each group the properties random and make them rotate. Now,<br />
the properties do not rotate so it is not fair for users to see always the same properties in same order. So, we need the rotation to be within same group not mixing all.</p>
<p>Query :- </p>
<p>SELECT DISTINCT(p.propid),p.category_id,p.people_sleep,p.bedrooms,p.bathrooms,p.children,<br />
p.airport,p.features,p.pets,p.smoke, p.wheelchair,p.prop_title,p.property_type,prop_date,<br />
display_calender,other_url_calender,is_feature<br />
FROM property p<br />
LEFT JOIN category ON (p.category_id=category.id)<br />
WHERE p.active IN(&#8220;yes&#8221;,&#8221;d&#8221;) AND p.is_feature &gt;= SYSDATE()<br />
ORDER BY p.is_move_up DESC,REPLACE(IFNULL(p.is_feature,&#8221;"),&#8221;2015-12-31&#8243;,&#8221;") DESC,<br />
p.listing_order DESC,p.propid DESC  </p>
<p>Can you please help me into this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dieter</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-804211</link>
		<dc:creator>Dieter</dc:creator>
		<pubDate>Mon, 11 Apr 2011 09:29:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-804211</guid>
		<description>Thanks for the quick response. VEry Nice</description>
		<content:encoded><![CDATA[<p>Thanks for the quick response. VEry Nice</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-799672</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Tue, 22 Feb 2011 20:11:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-799672</guid>
		<description>Thanks for this artikel!! Was what I was looking for.</description>
		<content:encoded><![CDATA[<p>Thanks for this artikel!! Was what I was looking for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Zaitsev</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-798705</link>
		<dc:creator>Peter Zaitsev</dc:creator>
		<pubDate>Fri, 18 Feb 2011 19:16:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-798705</guid>
		<description>Hi,

This is a hard one especially if you add ranges as MySQL will not be able to both use range for selection and sort by different column.       You may and up with large scans and sorts which can use many resources.  Sphinx  (http://www.sphinxsearch.com) is often a good alternative in such cases even if you&#039;re not using full text search query</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>This is a hard one especially if you add ranges as MySQL will not be able to both use range for selection and sort by different column.       You may and up with large scans and sorts which can use many resources.  Sphinx  (<a href="http://www.sphinxsearch.com" rel="nofollow">http://www.sphinxsearch.com</a>) is often a good alternative in such cases even if you&#8217;re not using full text search query</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Batman</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-798573</link>
		<dc:creator>Batman</dc:creator>
		<pubDate>Thu, 17 Feb 2011 20:13:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-798573</guid>
		<description>Hey Peter,

I&#039;ve been reading your articles for some time now, especially since I&#039;ve been running into issues with wrapping my head around this whole MySQL optimization stuff.

Anyways, I get the jist of it, however, I run a real estate website (displays listings, etc, in a search results page), and there are just too many variables to try and cover an index on each scenario... I&#039;d end up with 20 indexes, and I fear that any update/insert/delete queries to that table would blow up the internet.

I&#039;m not looking for anybody to write me anything, I&#039;m just looking to get a better understanding of this:

SELECT
	`id`,
        ... more *necessary* columns ...
FROM
	`listings`
WHERE
	`category` = 1
AND `country` = 1
AND `state` = 45
AND `city` = 35859
AND `visible` = 1
ORDER BY
	`price` ASC 
LIMIT 0, 10

Now, `category` numbers represent different listing types, ie. 1 = For Sale, 2 = For Rent, 3 = Pending Sale, and so on.  1 (For Sale is by the far the most popular, representing at least 75% of all listings in the table; probably more).

`country`, `state`, `city` are always in the query.  `visible` is either a 1 or 0.  1 meaning the listing has not expired, and 0 meaning it has.  Probably 85%+ instances of `visible` = 1 in the table.

Now, there are also other WHERE conditions that are possible, like WHERE `price` = 300000, or `bedrooms` = 2, or `bathrooms` = 3, etc.  And who knows what the user is going to enter in.

Then, there is the order by on top of that.

What I have been looking for is how to accommodate these kinds of variables when there are so many possible scenarios?  When I do an ORDER BY `price` DESC, it can take 4-5 seconds, maybe more... I guess that&#039;s not the end of the world, but our traffic is always growing, and that will end up being unacceptable.

Any thoughts?

- Batman</description>
		<content:encoded><![CDATA[<p>Hey Peter,</p>
<p>I&#8217;ve been reading your articles for some time now, especially since I&#8217;ve been running into issues with wrapping my head around this whole MySQL optimization stuff.</p>
<p>Anyways, I get the jist of it, however, I run a real estate website (displays listings, etc, in a search results page), and there are just too many variables to try and cover an index on each scenario&#8230; I&#8217;d end up with 20 indexes, and I fear that any update/insert/delete queries to that table would blow up the internet.</p>
<p>I&#8217;m not looking for anybody to write me anything, I&#8217;m just looking to get a better understanding of this:</p>
<p>SELECT<br />
	`id`,<br />
        &#8230; more *necessary* columns &#8230;<br />
FROM<br />
	`listings`<br />
WHERE<br />
	`category` = 1<br />
AND `country` = 1<br />
AND `state` = 45<br />
AND `city` = 35859<br />
AND `visible` = 1<br />
ORDER BY<br />
	`price` ASC<br />
LIMIT 0, 10</p>
<p>Now, `category` numbers represent different listing types, ie. 1 = For Sale, 2 = For Rent, 3 = Pending Sale, and so on.  1 (For Sale is by the far the most popular, representing at least 75% of all listings in the table; probably more).</p>
<p>`country`, `state`, `city` are always in the query.  `visible` is either a 1 or 0.  1 meaning the listing has not expired, and 0 meaning it has.  Probably 85%+ instances of `visible` = 1 in the table.</p>
<p>Now, there are also other WHERE conditions that are possible, like WHERE `price` = 300000, or `bedrooms` = 2, or `bathrooms` = 3, etc.  And who knows what the user is going to enter in.</p>
<p>Then, there is the order by on top of that.</p>
<p>What I have been looking for is how to accommodate these kinds of variables when there are so many possible scenarios?  When I do an ORDER BY `price` DESC, it can take 4-5 seconds, maybe more&#8230; I guess that&#8217;s not the end of the world, but our traffic is always growing, and that will end up being unacceptable.</p>
<p>Any thoughts?</p>
<p>- Batman</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naveen</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-797633</link>
		<dc:creator>Naveen</dc:creator>
		<pubDate>Thu, 10 Feb 2011 12:13:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-797633</guid>
		<description>Hi Peter

We have a tool which is processing 30 million records, this tool is a migration project where we are using perl script to do that 

we need to compare two tables where we are doing like this 

for using counter of 50,000 records in chunk size

and then we are using 

select column1, column2 from table  order by table limit index_num(21 million), (500,000) 

initially for smaller dataset, things are working fine (26 secs to 50 secs)

when index_num is growing, it is taking almost 30 mins to do that

We want to optimize this one.

As per your recent posts,

we need to have index, for column_1, we are creating indexes ..still it is not optimal.</description>
		<content:encoded><![CDATA[<p>Hi Peter</p>
<p>We have a tool which is processing 30 million records, this tool is a migration project where we are using perl script to do that </p>
<p>we need to compare two tables where we are doing like this </p>
<p>for using counter of 50,000 records in chunk size</p>
<p>and then we are using </p>
<p>select column1, column2 from table  order by table limit index_num(21 million), (500,000) </p>
<p>initially for smaller dataset, things are working fine (26 secs to 50 secs)</p>
<p>when index_num is growing, it is taking almost 30 mins to do that</p>
<p>We want to optimize this one.</p>
<p>As per your recent posts,</p>
<p>we need to have index, for column_1, we are creating indexes ..still it is not optimal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: reshma</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-770825</link>
		<dc:creator>reshma</dc:creator>
		<pubDate>Fri, 06 Aug 2010 14:03:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-770825</guid>
		<description>Hello Peter,

Thanks for the quick response. 

I have removed the index on category_id. After removing it Explain for the below select query is as follows:

EXPLAIN select newsid from news where category_id=53 and approval=â€™yesâ€™ order by date_created desc limit 0,10

id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra
1 	SIMPLE 	news 	index 	NULL 	date_created 	9 	NULL 	10 	Using where

This is fine i guess. 
Another question is: If there are 56 records for category_id=53 and i want to display 100 records for all given categories in news table, why does below query examine 100 records for category_id=53 when there are only 56 of them?

EXPLAIN select newsid from news where category_id=53 and approval=â€™yesâ€™ order by date_created desc limit 0,100

id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra
1 	SIMPLE 	news 	index 	NULL 	date_created 	9 	NULL 	100 	Using where

Most of the rows are displayed based on category_id, so I want to know if I am doing it correctly. 

If this is confusing I could send the table structures and queries.

In this post you have mentioned about DOS attack, do you mean paging records can cause DOS attack ?

Any help would be highly appreciated.</description>
		<content:encoded><![CDATA[<p>Hello Peter,</p>
<p>Thanks for the quick response. </p>
<p>I have removed the index on category_id. After removing it Explain for the below select query is as follows:</p>
<p>EXPLAIN select newsid from news where category_id=53 and approval=â€™yesâ€™ order by date_created desc limit 0,10</p>
<p>id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra<br />
1 	SIMPLE 	news 	index 	NULL 	date_created 	9 	NULL 	10 	Using where</p>
<p>This is fine i guess.<br />
Another question is: If there are 56 records for category_id=53 and i want to display 100 records for all given categories in news table, why does below query examine 100 records for category_id=53 when there are only 56 of them?</p>
<p>EXPLAIN select newsid from news where category_id=53 and approval=â€™yesâ€™ order by date_created desc limit 0,100</p>
<p>id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra<br />
1 	SIMPLE 	news 	index 	NULL 	date_created 	9 	NULL 	100 	Using where</p>
<p>Most of the rows are displayed based on category_id, so I want to know if I am doing it correctly. </p>
<p>If this is confusing I could send the table structures and queries.</p>
<p>In this post you have mentioned about DOS attack, do you mean paging records can cause DOS attack ?</p>
<p>Any help would be highly appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-770756</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 05 Aug 2010 17:09:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-770756</guid>
		<description>Reshma,

you seems to have poor index setup to begin with. You have &quot;index scan&quot; while you should have ref if you have (category_id,approval,date_created) for all queries. 
If Index is scanned the more rows have to be scanned to provide 10 results the more selective where clause is.</description>
		<content:encoded><![CDATA[<p>Reshma,</p>
<p>you seems to have poor index setup to begin with. You have &#8220;index scan&#8221; while you should have ref if you have (category_id,approval,date_created) for all queries.<br />
If Index is scanned the more rows have to be scanned to provide 10 results the more selective where clause is.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

