>>XXX is a random value ranking from 0 (or 1) to the maximum id value. You
>>generate it outside the database.
>Maximum id is not good solution, beacuse some of ids between 0 (or 1)
>could be missing for some reasons. You should:
>select id from table;
SEQ SCAN. Avoid for big tables.
>Then chose random one outside SQL, and then:
>select * from table where table.id = '$random_id';
>This would be slower but will work for sure.
Probably even slower than original. Not sure, though.
>Another approach -- select random id outside SQL from 0 (or 1) to
>maximum_id and then trying to select with 'where table.id =
>'$random_id'' until you've got > 0 rows in result.
The only drawback that I see to this approach is that it may not find a
result in a finite time.
Better: "Compress" your table so that it holds all continuous IDs. This
may be painful and long, but it will pay off in the end, if your table
is not updated often, so this operation only needs to be done once.
I guess The Bible is not updated very often. ;-)
In response to
pgsql-general by date
|Next:||From: Nevermind||Date: 2001-12-21 20:17:47|
|Subject: Re: Database tuning|
|Previous:||From: Helge Bahmann||Date: 2001-12-21 20:09:24|
|Subject: udp port|