<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments for The Rhodium Toad</title>
	<link>http://blog.rhodiumtoad.org.uk</link>
	<description>an experimental blog</description>
	<pubDate>Sun, 05 Feb 2012 15:06:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>Comment on Selecting random rows from a table by bla</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-17007</link>
		<author>bla</author>
		<pubDate>Fri, 15 Apr 2011 11:04:16 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-17007</guid>
		<description>This can be done simplier using &#62;= instead of =. You will always find some response even if initial random guess was non-existing ID. No need to generate more then needed in case we won't hit any existing.

. select * from items 
. where id &#62;= (select floor(random() * (max_id - min_id + 1))::integer
.              + min_id from (select max(id) as max_id, 
.              min(id) as min_id from items) s1 limit 1) 
. limit 1;

Rows occuring after a big gap in ids can get picked up more frequent.

Would be nice to write somewhere that JS is required to comment.</description>
		<content:encoded><![CDATA[<p>This can be done simplier using &gt;= instead of =. You will always find some response even if initial random guess was non-existing ID. No need to generate more then needed in case we won&#8217;t hit any existing.</p>
<p>. select * from items<br />
. where id &gt;= (select floor(random() * (max_id - min_id + 1))::integer<br />
.              + min_id from (select max(id) as max_id,<br />
.              min(id) as min_id from items) s1 limit 1)<br />
. limit 1;</p>
<p>Rows occuring after a big gap in ids can get picked up more frequent.</p>
<p>Would be nice to write somewhere that JS is required to comment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Selecting random rows from a table by Frank Kim</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-10272</link>
		<author>Frank Kim</author>
		<pubDate>Thu, 09 Sep 2010 19:58:22 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-10272</guid>
		<description>I needed a smart way of getting the next row from my PostgreSQL table and this was perfect!</description>
		<content:encoded><![CDATA[<p>I needed a smart way of getting the next row from my PostgreSQL table and this was perfect!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on The Rule Challenge by Andrew</title>
		<link>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8750</link>
		<author>Andrew</author>
		<pubDate>Tue, 22 Jun 2010 17:36:48 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8750</guid>
		<description>We've had more than the usual number of rules questions on IRC recently, at least that's my impression; that would probably explain the multiple recent blog posts. (And I only wrote this one because someone on IRC bugged me about it.)</description>
		<content:encoded><![CDATA[<p>We&#8217;ve had more than the usual number of rules questions on IRC recently, at least that&#8217;s my impression; that would probably explain the multiple recent blog posts. (And I only wrote this one because someone on IRC bugged me about it.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on The Rule Challenge by Robert Young</title>
		<link>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8735</link>
		<author>Robert Young</author>
		<pubDate>Tue, 22 Jun 2010 12:30:56 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8735</guid>
		<description>I'm not principally a PostgreSQL folk, DB2 is my main squeeze, but I do follow it a bit.  The whole rules thing is something only PG did, and that was Stonebraker's doing.  One would have to read up his rationale way back when.  From what I can find, View support is the justification for using rules, since that is the only way to get it.  There have been a few links from the PostgreSQL site to rule posts such as this recently.  I wonder why rules have become a topic du jour?

The challenge as written isn't provable; the counterexample is exhaustive enumeration of all non-trivial rules.

As to better, one of those linked posts asserts that triggers are better.  I would agree with that.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not principally a PostgreSQL folk, DB2 is my main squeeze, but I do follow it a bit.  The whole rules thing is something only PG did, and that was Stonebraker&#8217;s doing.  One would have to read up his rationale way back when.  From what I can find, View support is the justification for using rules, since that is the only way to get it.  There have been a few links from the PostgreSQL site to rule posts such as this recently.  I wonder why rules have become a topic du jour?</p>
<p>The challenge as written isn&#8217;t provable; the counterexample is exhaustive enumeration of all non-trivial rules.</p>
<p>As to better, one of those linked posts asserts that triggers are better.  I would agree with that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on The Rule Challenge by Andrew</title>
		<link>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8710</link>
		<author>Andrew</author>
		<pubDate>Tue, 22 Jun 2010 04:41:57 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8710</guid>
		<description>That is indeed the real question, but it's one for another blog post rather than this one :-)</description>
		<content:encoded><![CDATA[<p>That is indeed the real question, but it&#8217;s one for another blog post rather than this one :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on The Rule Challenge by Tom Lane</title>
		<link>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8706</link>
		<author>Tom Lane</author>
		<pubDate>Tue, 22 Jun 2010 03:43:46 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2010/06/21/the-rule-challenge/#comment-8706</guid>
		<description>Rules work all right for views (ie, ON SELECT DO INSTEAD SELECT cases).  There's no question that every other case sucks.  The *real* question is how to do better?</description>
		<content:encoded><![CDATA[<p>Rules work all right for views (ie, ON SELECT DO INSTEAD SELECT cases).  There&#8217;s no question that every other case sucks.  The *real* question is how to do better?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Selecting random rows from a table by Andrew</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-4221</link>
		<author>Andrew</author>
		<pubDate>Sat, 29 Aug 2009 20:13:08 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-4221</guid>
		<description>That's not unbiased; some rows are more likely than others to be returned.</description>
		<content:encoded><![CDATA[<p>That&#8217;s not unbiased; some rows are more likely than others to be returned.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Selecting random rows from a table by Andrew</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-4172</link>
		<author>Andrew</author>
		<pubDate>Thu, 27 Aug 2009 16:51:31 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-4172</guid>
		<description>If you don't really care about insert performance, and you only need one (or a few) random rows at once you can do this:

ALTER TABLE x ADD COLUMN r DOUBLE PRECISION;
ALTER TABLE x ALTER COLUMN r SET DEFAULT random();
UPDATE x SET r = random() WHERE r IS NULL; -- this will be slow
ALTER TABLE x ALTER COLUMN r SET NOT NULL;
CREATE INDEX i ON x(r); -- also slow
ANALYZE x(r);

Then take a sample row quickly by running this:
SELECT * FROM x WHERE r &#62;= (SELECT random()) ORDER BY r LIMIT 1;

I'm not sure if asking for more than one row in the LIMIT clause would be statistically sound or not.  The "random" order is fixed, so whenever you land in an overlapping spot the sequence will be the same.

If you just need a few rows, you can UNION a few of those together, and that should be as random as you could care for.</description>
		<content:encoded><![CDATA[<p>If you don&#8217;t really care about insert performance, and you only need one (or a few) random rows at once you can do this:</p>
<p>ALTER TABLE x ADD COLUMN r DOUBLE PRECISION;<br />
ALTER TABLE x ALTER COLUMN r SET DEFAULT random();<br />
UPDATE x SET r = random() WHERE r IS NULL; &#8212; this will be slow<br />
ALTER TABLE x ALTER COLUMN r SET NOT NULL;<br />
CREATE INDEX i ON x(r); &#8212; also slow<br />
ANALYZE x(r);</p>
<p>Then take a sample row quickly by running this:<br />
SELECT * FROM x WHERE r &gt;= (SELECT random()) ORDER BY r LIMIT 1;</p>
<p>I&#8217;m not sure if asking for more than one row in the LIMIT clause would be statistically sound or not.  The &#8220;random&#8221; order is fixed, so whenever you land in an overlapping spot the sequence will be the same.</p>
<p>If you just need a few rows, you can UNION a few of those together, and that should be as random as you could care for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Selecting random rows from a table by Joanmi</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-1079</link>
		<author>Joanmi</author>
		<pubDate>Fri, 17 Apr 2009 23:18:11 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-1079</guid>
		<description>Tell it to my boss.

Sorry for the noise.

Regards.</description>
		<content:encoded><![CDATA[<p>Tell it to my boss.</p>
<p>Sorry for the noise.</p>
<p>Regards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Selecting random rows from a table by Andrew</title>
		<link>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-1076</link>
		<author>Andrew</author>
		<pubDate>Fri, 17 Apr 2009 15:28:50 +0000</pubDate>
		<guid>http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/#comment-1076</guid>
		<description>I don't have much sympathy for people still using 7.4 (which is approaching EOL).

The optimization for min() and max() to use indexes was added in 8.2.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t have much sympathy for people still using 7.4 (which is approaching EOL).</p>
<p>The optimization for min() and max() to use indexes was added in 8.2.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

