Re: Select x random rows from DB
On Feb 20, 2011, at 4:30 PM, Christophe Pettus wrote:
>
> On Feb 20, 2011, at 2:19 PM, galago wrote:
>
>> What is the best way, to select X random rows from DB? I know that method: .all().order_by('?')[:X] is not good idea.
>
> The best way is to push it onto the DB, using a raw query:
>
> random_results = Table.objects.raw("SELECT * FROM table ORDER BY random() LIMIT X")
If you have lots of rows, this query is really slow as the db must build a new table for the ORDER BY.
--
Eric Chamberlain, Founder
RF.com - http://RF.com/
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home