<?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: Selecting a random row from a table in mysql</title>
	<atom:link href="http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/</link>
	<description>( to ) ? be : ! be;</description>
	<pubDate>Tue, 06 Jan 2009 07:37:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: deepak</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-7377</link>
		<dc:creator>deepak</dc:creator>
		<pubDate>Fri, 19 Sep 2008 05:50:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-7377</guid>
		<description>Hi,

I'm getting the last result row in the table which is ordered by that row.
What I want is specific row selection from the table.

Thanks,
Deepak E.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;m getting the last result row in the table which is ordered by that row.<br />
What I want is specific row selection from the table.</p>
<p>Thanks,<br />
Deepak E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl J</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-7360</link>
		<dc:creator>Carl J</dc:creator>
		<pubDate>Thu, 18 Sep 2008 23:36:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-7360</guid>
		<description>That should return the last row in the db, assuming that cnum is your primary key field. What is it returning to you?

Carl Js last blog post..&lt;a href="http://feeds.feedburner.com/~r/CarlJ/~3/386075624/" rel="nofollow"&gt;In Case You’re Wondering, I’m Doing Alright - Part Duh!&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>That should return the last row in the db, assuming that cnum is your primary key field. What is it returning to you?</p>
<p>Carl Js last blog post..<a href="http://feeds.feedburner.com/~r/CarlJ/~3/386075624/" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/feeds.feedburner.com');">In Case You’re Wondering, I’m Doing Alright - Part Duh!</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: deepak</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-7309</link>
		<dc:creator>deepak</dc:creator>
		<pubDate>Thu, 18 Sep 2008 05:54:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-7309</guid>
		<description>Hi People!

I need to fetch the last but one row from the table.
I used below queries it is not giving me the desired result. I think a mysql function is needed to fetch.

select * from cust order by cnum desc limit 1;</description>
		<content:encoded><![CDATA[<p>Hi People!</p>
<p>I need to fetch the last but one row from the table.<br />
I used below queries it is not giving me the desired result. I think a mysql function is needed to fetch.</p>
<p>select * from cust order by cnum desc limit 1;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prashanthellina</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-3799</link>
		<dc:creator>prashanthellina</dc:creator>
		<pubDate>Sun, 04 May 2008 04:37:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-3799</guid>
		<description>Carl J, Very interesting! I had written this blog post based on my experience in randomly selecting a record from a table with around 5 million records. I don't have the timing stats for that one though. The table you tried the test on is innoDB or MyISAM? I tried mine on InnoDB. Besides I would think a larger test size (atleast a million records) will be better for this one.

Sai,
I don't think the optimization applies for dynamically generated values like RAND() (as opposed to column data). As I said above, I *have* seen that "order by rand()" is slower. I will try to post stats when possible.</description>
		<content:encoded><![CDATA[<p>Carl J, Very interesting! I had written this blog post based on my experience in randomly selecting a record from a table with around 5 million records. I don&#8217;t have the timing stats for that one though. The table you tried the test on is innoDB or MyISAM? I tried mine on InnoDB. Besides I would think a larger test size (atleast a million records) will be better for this one.</p>
<p>Sai,<br />
I don&#8217;t think the optimization applies for dynamically generated values like RAND() (as opposed to column data). As I said above, I *have* seen that &#8220;order by rand()&#8221; is slower. I will try to post stats when possible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sai Rahul</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-3677</link>
		<dc:creator>Sai Rahul</dc:creator>
		<pubDate>Mon, 28 Apr 2008 12:54:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-3677</guid>
		<description>As Carl pointed out, 

SELECT * FROM mytable ORDER BY RAND() LIMIT 1; 

is not slow because. (http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html).

&lt;i&gt;If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without the LIMIT clause must be selected, and most or all of them must be sorted, before it can be ascertained that the first row_count rows have been found. In either case, after the initial rows have been found, there is no need to sort any remainder of the result set, and MySQL does not do so.&lt;/i&gt;</description>
		<content:encoded><![CDATA[<p>As Carl pointed out, </p>
<p>SELECT * FROM mytable ORDER BY RAND() LIMIT 1; </p>
<p>is not slow because. (http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html).</p>
<p><i>If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without the LIMIT clause must be selected, and most or all of them must be sorted, before it can be ascertained that the first row_count rows have been found. In either case, after the initial rows have been found, there is no need to sort any remainder of the result set, and MySQL does not do so.</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl J</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-3676</link>
		<dc:creator>Carl J</dc:creator>
		<pubDate>Mon, 28 Apr 2008 12:30:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-3676</guid>
		<description>Hey, first off, thanks for linking to my post :)

I have a comment regarding your "simple but slow" vs. "Pure SQL" comparison.

After reading this post, I decided to try out your approach to see what kind of difference there is, so I loaded up a table in MySQL with 5000 records and ran both the "simple but slow" method and the "Pure SQL" method. You might be surprised at the results I got.

I used two simple approaches to get my results. First, I used PHPMyAdmin to select a single record, with both SQL methods. Then I used the ASP.Net code that was used in my post and ran both, selecting a single record from the list of 5000, 100 times.

Simple but Slow in PHPMyAdmin
- 0.0073 sec
- 0.0084 sec
- 0.0069 sec
- 0.0091 sec
- 0.0073 sec

Pure SQL in PHPMyAdmin
- 4.3905 sec
- 9.5469 sec
- 9.9261 sec
- 9.6034 sec
- 9.5922 sec

Simple but Slow in ASP.Net
- 00:00:01.0014400
- 00:00:00.9313392
- 00:00:01.1816992 
- 00:00:01.3619584 
- 00:00:01.0414976

Pure SQL in ASP.Net
- 00:09:35.5175536

As you can see in both sets of tests, especially with the second set, the "simple but slow" method out performs the slower Pure SQL method.

Now I have to go write a Blog post about it :)

Carl J</description>
		<content:encoded><![CDATA[<p>Hey, first off, thanks for linking to my post <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I have a comment regarding your &#8220;simple but slow&#8221; vs. &#8220;Pure SQL&#8221; comparison.</p>
<p>After reading this post, I decided to try out your approach to see what kind of difference there is, so I loaded up a table in MySQL with 5000 records and ran both the &#8220;simple but slow&#8221; method and the &#8220;Pure SQL&#8221; method. You might be surprised at the results I got.</p>
<p>I used two simple approaches to get my results. First, I used PHPMyAdmin to select a single record, with both SQL methods. Then I used the ASP.Net code that was used in my post and ran both, selecting a single record from the list of 5000, 100 times.</p>
<p>Simple but Slow in PHPMyAdmin<br />
- 0.0073 sec<br />
- 0.0084 sec<br />
- 0.0069 sec<br />
- 0.0091 sec<br />
- 0.0073 sec</p>
<p>Pure SQL in PHPMyAdmin<br />
- 4.3905 sec<br />
- 9.5469 sec<br />
- 9.9261 sec<br />
- 9.6034 sec<br />
- 9.5922 sec</p>
<p>Simple but Slow in ASP.Net<br />
- 00:00:01.0014400<br />
- 00:00:00.9313392<br />
- 00:00:01.1816992<br />
- 00:00:01.3619584<br />
- 00:00:01.0414976</p>
<p>Pure SQL in ASP.Net<br />
- 00:09:35.5175536</p>
<p>As you can see in both sets of tests, especially with the second set, the &#8220;simple but slow&#8221; method out performs the slower Pure SQL method.</p>
<p>Now I have to go write a Blog post about it <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Carl J</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prashanthellina</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-2922</link>
		<dc:creator>prashanthellina</dc:creator>
		<pubDate>Wed, 09 Apr 2008 14:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-2922</guid>
		<description>Good one! Didn't think about this!</description>
		<content:encoded><![CDATA[<p>Good one! Didn&#8217;t think about this!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sai Rahul</title>
		<link>http://blog.prashanthellina.com/2008/04/08/selecting-a-random-row-from-a-table-in-mysql/#comment-2871</link>
		<dc:creator>Sai Rahul</dc:creator>
		<pubDate>Tue, 08 Apr 2008 18:23:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=60#comment-2871</guid>
		<description>One more method is there which is far simpler :) 
x = get_random_number(ROWS)
select * from table limit x, 1</description>
		<content:encoded><![CDATA[<p>One more method is there which is far simpler <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
x = get_random_number(ROWS)<br />
select * from table limit x, 1</p>
]]></content:encoded>
	</item>
</channel>
</rss>
