<?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>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: mohamed</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-678939</link>
		<dc:creator>mohamed</dc:creator>
		<pubDate>Tue, 17 Nov 2009 23:10:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-678939</guid>
		<description>I have a query that takes 14 seconds to execute:

SELECT posts.* FROM posts force index (idx_post_date) INNER JOIN follow ON follow.followuserid = posts.userid WHERE follow.userid=&#039;61585&#039; ORDER BY date DESC LIMIT 0, 20;

The table posts has ~1000000 records and the number of records in the posts table belonging to userid 61585 is 0.

The table follow has 63537 records.

This is how my indexes are set:-

mysql&gt; show index from follow;
+--------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
&#124; Table  &#124; Non_unique &#124; Key_name &#124; Seq_in_index &#124; Column_name  &#124; Collation &#124; Cardinality &#124; Sub_part &#124; Packed &#124; Null &#124; Index_type &#124; Comment &#124;
+--------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
&#124; follow &#124;          0 &#124; PRIMARY  &#124;            1 &#124; userid       &#124; A         &#124;        NULL &#124;     NULL &#124; NULL   &#124;      &#124; BTREE      &#124;         &#124; 
&#124; follow &#124;          0 &#124; PRIMARY  &#124;            2 &#124; followuserid &#124; A         &#124;       63537 &#124;     NULL &#124; NULL   &#124;      &#124; BTREE      &#124;         &#124; 
+--------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
 
mysql&gt; show index from posts;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
&#124; Table &#124; Non_unique &#124; Key_name         &#124; Seq_in_index &#124; Column_name &#124; Collation &#124; Cardinality &#124; Sub_part &#124; Packed &#124; Null &#124; Index_type &#124; Comment &#124;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
&#124; posts &#124;          0 &#124; PRIMARY          &#124;            1 &#124; id          &#124; A         &#124;     1047467 &#124;     NULL &#124; NULL   &#124;      &#124; BTREE      &#124;         &#124; 
&#124; posts &#124;          1 &#124; idx_post_date    &#124;            1 &#124; date        &#124; A         &#124;     1047467 &#124;     NULL &#124; NULL   &#124;      &#124; BTREE      &#124;         &#124; 
&#124; posts &#124;          1 &#124; idx_posts_userid &#124;            1 &#124; userid      &#124; A         &#124;       22771 &#124;     NULL &#124; NULL   &#124;      &#124; BTREE      &#124;         &#124; 
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

mysql&gt; explain SELECT posts.* FROM posts force index (idx_post_date) INNER JOIN follow ON follow.followuserid = posts.userid WHERE follow.userid=&#039;61585&#039; ORDER BY date DESC LIMIT 0, 20;
+----+-------------+--------+--------+---------------+---------------+---------+-------------------------------+------+-------------+
&#124; id &#124; select_type &#124; table  &#124; type   &#124; possible_keys &#124; key           &#124; key_len &#124; ref                           &#124; rows &#124; Extra       &#124;
+----+-------------+--------+--------+---------------+---------------+---------+-------------------------------+------+-------------+
&#124;  1 &#124; SIMPLE      &#124; posts  &#124; index  &#124; NULL          &#124; idx_post_date &#124; 4       &#124; NULL                          &#124;   20 &#124;             &#124; 
&#124;  1 &#124; SIMPLE      &#124; follow &#124; eq_ref &#124; PRIMARY       &#124; PRIMARY       &#124; 8       &#124; const,wwwproject.posts.userid &#124;    1 &#124; Using index &#124; 
+----+-------------+--------+--------+---------------+---------------+---------+-------------------------------+------+-------------+


Is there a way to optimize this query i have now been trying for quite some time but could not find a solution any help would be very much appreciated??</description>
		<content:encoded><![CDATA[<p>I have a query that takes 14 seconds to execute:</p>
<p>SELECT posts.* FROM posts force index (idx_post_date) INNER JOIN follow ON follow.followuserid = posts.userid WHERE follow.userid=&#8217;61585&#8242; ORDER BY date DESC LIMIT 0, 20;</p>
<p>The table posts has ~1000000 records and the number of records in the posts table belonging to userid 61585 is 0.</p>
<p>The table follow has 63537 records.</p>
<p>This is how my indexes are set:-</p>
<p>mysql&gt; show index from follow;<br />
+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
| Table  | Non_unique | Key_name | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |<br />
+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
| follow |          0 | PRIMARY  |            1 | userid       | A         |        NULL |     NULL | NULL   |      | BTREE      |         |<br />
| follow |          0 | PRIMARY  |            2 | followuserid | A         |       63537 |     NULL | NULL   |      | BTREE      |         |<br />
+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>mysql&gt; show index from posts;<br />
+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |<br />
+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
| posts |          0 | PRIMARY          |            1 | id          | A         |     1047467 |     NULL | NULL   |      | BTREE      |         |<br />
| posts |          1 | idx_post_date    |            1 | date        | A         |     1047467 |     NULL | NULL   |      | BTREE      |         |<br />
| posts |          1 | idx_posts_userid |            1 | userid      | A         |       22771 |     NULL | NULL   |      | BTREE      |         |<br />
+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>mysql&gt; explain SELECT posts.* FROM posts force index (idx_post_date) INNER JOIN follow ON follow.followuserid = posts.userid WHERE follow.userid=&#8217;61585&#8242; ORDER BY date DESC LIMIT 0, 20;<br />
+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br />
| id | select_type | table  | type   | possible_keys | key           | key_len | ref                           | rows | Extra       |<br />
+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br />
|  1 | SIMPLE      | posts  | index  | NULL          | idx_post_date | 4       | NULL                          |   20 |             |<br />
|  1 | SIMPLE      | follow | eq_ref | PRIMARY       | PRIMARY       | 8       | const,wwwproject.posts.userid |    1 | Using index |<br />
+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+</p>
<p>Is there a way to optimize this query i have now been trying for quite some time but could not find a solution any help would be very much appreciated??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pirmo N ierakstu atlase &#171; Datubāzu resurss latviski</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-664341</link>
		<dc:creator>Pirmo N ierakstu atlase &#171; Datubāzu resurss latviski</dc:creator>
		<pubDate>Tue, 13 Oct 2009 05:02:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-664341</guid>
		<description>[...] Protams nevajadzētu aizmirst, ka, lai iegūtu pirmos N ierakstus arī MySQLā, kārtošanas problēma nekur nav pazudusi. Ieraksti ir jākārto tik un tā. Tikai, kā jau parasti to var darīt dažādos veidos. Var kārtot visus ierakstus vaicājuma izpildes laikā, var ierakstus sakārtot pirms tam izveidojot indeksu uz kārtošanas kolonu. Ja datu un vaicājumu daudzums pārsniedz spēlēšanās robežu, tad bez indeksa(-iem) iztikt būs ļoti nepatīkami. Sīkāk par Order by un Limit klauzas veiktspējas uzlabošanas iespējām var lasīt MySQL Performance Blogā ORDER BY &#8230; LIMIT Performance Optimization.  [...]</description>
		<content:encoded><![CDATA[<p>[...] Protams nevajadzētu aizmirst, ka, lai iegūtu pirmos N ierakstus arī MySQLā, kārtošanas problēma nekur nav pazudusi. Ieraksti ir jākārto tik un tā. Tikai, kā jau parasti to var darīt dažādos veidos. Var kārtot visus ierakstus vaicājuma izpildes laikā, var ierakstus sakārtot pirms tam izveidojot indeksu uz kārtošanas kolonu. Ja datu un vaicājumu daudzums pārsniedz spēlēšanās robežu, tad bez indeksa(-iem) iztikt būs ļoti nepatīkami. Sīkāk par Order by un Limit klauzas veiktspējas uzlabošanas iespējām var lasīt MySQL Performance Blogā ORDER BY &#8230; LIMIT Performance Optimization.  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikhail panchenko / blog &#187; Blog Archive &#187; Pull random data out of MySQL without making it cry &#8211; using and optimizing ORDER BY, LIMIT, OFFSET etc.</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-656404</link>
		<dc:creator>mikhail panchenko / blog &#187; Blog Archive &#187; Pull random data out of MySQL without making it cry &#8211; using and optimizing ORDER BY, LIMIT, OFFSET etc.</dc:creator>
		<pubDate>Wed, 23 Sep 2009 07:57:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-656404</guid>
		<description>[...] have something to tide you over. Among other places, I&#8217;ve seen this workaround suggested in this MySQL Performance Blog Post. If you have a sequential column you can use (i.e., that is included in an index you can use for [...]</description>
		<content:encoded><![CDATA[<p>[...] have something to tide you over. Among other places, I&#8217;ve seen this workaround suggested in this MySQL Performance Blog Post. If you have a sequential column you can use (i.e., that is included in an index you can use for [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Rose</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-560510</link>
		<dc:creator>Mark Rose</dc:creator>
		<pubDate>Wed, 13 May 2009 18:05:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-560510</guid>
		<description>Sorry, that should be &quot;Add an index on&quot; not &quot;Add a query on&quot;.</description>
		<content:encoded><![CDATA[<p>Sorry, that should be &#8220;Add an index on&#8221; not &#8220;Add a query on&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Rose</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-560509</link>
		<dc:creator>Mark Rose</dc:creator>
		<pubDate>Wed, 13 May 2009 18:04:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-560509</guid>
		<description>Add a query on (ID, Time) and re-order the WHERE clause to &quot;ID &gt; 2050702 and Time &gt;= 2009-03-24 12:53:00&quot;.

MySQL can only use one index in a query (basically), so you need an index that contains all the columns you are filtering and ordering by, in the same order.</description>
		<content:encoded><![CDATA[<p>Add a query on (ID, Time) and re-order the WHERE clause to &#8220;ID &gt; 2050702 and Time &gt;= 2009-03-24 12:53:00&#8243;.</p>
<p>MySQL can only use one index in a query (basically), so you need an index that contains all the columns you are filtering and ordering by, in the same order.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kalyani</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-560504</link>
		<dc:creator>Kalyani</dc:creator>
		<pubDate>Wed, 13 May 2009 17:33:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-560504</guid>
		<description>My query performance is very slow for large database.Table A has 2050702 records in it. ID is primary key and index on time.
SELECT ID, Name,  Time from TableA WHERE Time &gt;= &#039;2009-03-24 12:53:00&#039;  and ID &gt; 2050702
Order By ID DESC LIMIT 100

I would like to know ways to optimize the query above. SO retrieve last 100 records from TAble A matching the criteria in where clause</description>
		<content:encoded><![CDATA[<p>My query performance is very slow for large database.Table A has 2050702 records in it. ID is primary key and index on time.<br />
SELECT ID, Name,  Time from TableA WHERE Time &gt;= &#8216;2009-03-24 12:53:00&#8242;  and ID &gt; 2050702<br />
Order By ID DESC LIMIT 100</p>
<p>I would like to know ways to optimize the query above. SO retrieve last 100 records from TAble A matching the criteria in where clause</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yang Yang</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-536436</link>
		<dc:creator>Yang Yang</dc:creator>
		<pubDate>Sun, 12 Apr 2009 03:10:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-536436</guid>
		<description>Thanks dude that is rather helpful!</description>
		<content:encoded><![CDATA[<p>Thanks dude that is rather helpful!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Rose</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-469235</link>
		<dc:creator>Mark Rose</dc:creator>
		<pubDate>Sun, 08 Feb 2009 09:03:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-469235</guid>
		<description>Is there anyway to accomplish the same thing as this query, avoiding a temporary table?

select ID_MEMBER_STARTED, COUNT(*) as hits from smf_topics group by ID_MEMBER_STARTED order by hits desc limit 20;

There is an index on ID_MEMBER_STARTED and the table type is InnoDB.</description>
		<content:encoded><![CDATA[<p>Is there anyway to accomplish the same thing as this query, avoiding a temporary table?</p>
<p>select ID_MEMBER_STARTED, COUNT(*) as hits from smf_topics group by ID_MEMBER_STARTED order by hits desc limit 20;</p>
<p>There is an index on ID_MEMBER_STARTED and the table type is InnoDB.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DIA</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-448727</link>
		<dc:creator>DIA</dc:creator>
		<pubDate>Mon, 19 Jan 2009 18:51:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-448727</guid>
		<description>As you have somthing like this SELECT * FROM table LIMIT 10
It working with this script SELECT * FROM table LIMIT 0,10</description>
		<content:encoded><![CDATA[<p>As you have somthing like this SELECT * FROM table LIMIT 10<br />
It working with this script SELECT * FROM table LIMIT 0,10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: radek</title>
		<link>http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/comment-page-2/#comment-429686</link>
		<dc:creator>radek</dc:creator>
		<pubDate>Tue, 30 Dec 2008 19:56:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-429686</guid>
		<description>bozek, is it possible that your key_buffer size is set too small and therefore the whole index for your 30 million records is not large enough and therefore not caching the index?  Not on my application this is set larger than the total index sizes for ALL tables and not just this one.</description>
		<content:encoded><![CDATA[<p>bozek, is it possible that your key_buffer size is set too small and therefore the whole index for your 30 million records is not large enough and therefore not caching the index?  Not on my application this is set larger than the total index sizes for ALL tables and not just this one.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
