Re: Select x random rows from DB

2011-02-21 Thread Phlip
On Feb 20, 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. > What methods do you use? order_by('RAND()') That might use the same seed each time. To create, for examp

Re: Select x random rows from DB

2011-02-21 Thread Christophe Pettus
If you know this is going to be an important part of the application, it might make sense to have a random primary key, such as a UUID. -- -- Christophe Pettus x...@thebuild.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this

Re: Select x random rows from DB

2011-02-21 Thread Cal Leeming [Simplicity Media Ltd]
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 wrote: > This is the function for getting 1 item that works even if some rows > were deleted that works times faster than order_by('

Re: Select x random rows from DB

2011-02-21 Thread Mikhail Korobov
Stackoverflow: http://stackoverflow.com/questions/962619/how-to-pull-a-random-record-using-djangos-orm/971671#971671 On 22 фев, 03:06, Mikhail Korobov 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

Re: Select x random rows from DB

2011-02-21 Thread Mikhail Korobov
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()

Re: Select x random rows from DB

2011-02-21 Thread galago
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 m

Re: Select x random rows from DB

2011-02-21 Thread Christophe Pettus
On Feb 21, 2011, at 10:34 AM, Eric Chamberlain wrote: > If you have lots of rows, this query is really slow as the db must build a > new table for the ORDER BY. You can do better if you have a guaranteed ordinal on the rows; otherwise, it has to do a full table scan no matter what. -- -- Chris

Re: Select x random rows from DB

2011-02-21 Thread Eric Chamberlain
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 p

Re: Select x random rows from DB

2011-02-21 Thread Cal Leeming [Simplicity Media Ltd]
eb 20, 2011, at 2:19 PM, galago wrote: > > > What is the best way, to select X random rows from DB... > > 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") &g

Re: Select x random rows from DB

2011-02-20 Thread Christophe Pettus
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("SELE

Re: Select x random rows from DB

2011-02-20 Thread Cal Leeming [Simplicity Media Ltd]
om MTN >> -- >> *From: * galago >> *Sender: * django-users@googlegroups.com >> *Date: *Sun, 20 Feb 2011 14:24:37 -0800 (PST) >> *To: * >> *ReplyTo: * django-users@googlegroups.com >> *Subject: *Re: Select x random rows from D

Re: Select x random rows from DB

2011-02-20 Thread Cal Leeming [Simplicity Media Ltd]
ireless device from MTN > -- > *From: * galago > *Sender: * django-users@googlegroups.com > *Date: *Sun, 20 Feb 2011 14:24:37 -0800 (PST) > *To: * > *ReplyTo: * django-users@googlegroups.com > *Subject: *Re: Select x random rows from DB > > If i knew what is a good idea, I

Re: Select x random rows from DB

2011-02-20 Thread delegbede
@googlegroups.com Date: Sun, 20 Feb 2011 14:24:37 To: Reply-To: django-users@googlegroups.com Subject: Re: Select x random rows from DB If i knew what is a good idea, I wouldn't aks here:D I only know - that described method is DB killer :) -- You received this message because you are subscrib

Re: Select x random rows from DB

2011-02-20 Thread Cal Leeming [Simplicity Media Ltd]
10: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. > What methods do you use? > > -- > You received this message because you are subscribed to the Google Groups > &q

Re: Select x random rows from DB

2011-02-20 Thread delegbede
ly-To: django-users@googlegroups.com Subject: Select x random rows from DB What is the best way, to select X random rows from DB? I know that method: .all().order_by('?')[:X] is not good idea. What methods do you use? -- You received this message because you are subscribed to the Google

Re: Select x random rows from DB

2011-02-20 Thread galago
If i knew what is a good idea, I wouldn't aks here:D I only know - that described method is DB killer :) -- 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

Select x random rows from DB

2011-02-20 Thread galago
What is the best way, to select X random rows from DB? I know that method: .all().order_by('?')[:X] is not good idea. What methods do you use? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send e