Please see my previous post with the django snippet for more info on this
method (as it uses this same principle).

On Mon, Feb 21, 2011 at 10:06 PM, Mikhail Korobov <kmik...@googlemail.com>wrote:

> This is the function for getting 1 item that works even if some rows
> were deleted that works times faster than order_by('?') even for not-
> so-big datasets at least on mysql:
>
> def get_random_item(model, max_id=None):
>    if max_id is None:
>        max_id = model.objects.aggregate(Max('id')).values()[0]
>    min_id = math.ceil(max_id*random.random())
>    return model.objects.filter(id__gte=min_id)[0]
>
> It assumes that almost all records are still in DB and ids are more or
> less successive because otherwise the distribution won't be uniform.
> If there are a lot of items in DB then it should be almost safe to
> call this several times (and re-call if the same row is obtained).
>
> On 22 фев, 02:10, galago <prog...@gmail.com> wrote:
> > I need to do this in my tagcloud.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to