[development] Drupal 7 Entities - view random node

Larry Garfield larry at garfieldtech.com
Mon Jan 24 01:12:08 UTC 2011


I don't know that I've ever tried it with 12k nodes so I can't say from 
experience.

Randomization is a tricky subject.  The common method, which Views uses as 
does the ->orderRandom() method of DBTNG, boils down to adding a random number 
as a new column for each record, ordering by that, and then (in this case) 
discarding all but the first record.  That involves a linear-growth creation 
for the new column (a fixed additional cost per record) and then an integer 
ordering.  Assuming the sorting function in the SQL database is sane (which if 
it isn't you need a new database), that should be an n*log n algorithm.  
(That's as fast as a sorting algorithm can get.)

So yes, more records means randomization is slower.  How slow is too slow 
depends on too many factors for me to make any general statement.

There are other mechanisms for randomization that can be faster, even O(1), 
but those rely on other assumption such as a primary key that is always 1-
based and contiguous.  That would work great... if you're searching all nodes, 
not just nodes of a certain type, and you've never deleted a node.  The less 
those two caveats are true, the less well that method works.

In this case, randomizing via Views and randomizing via DBTNG will be equally 
performant since they're using the same underlying SQL (at least the same 
approach, even if not the exact same syntax).  I don't know of a faster, 
generic random-order approach in SQL.  (If someone does, please file an issue 
so we can get it into core. <g>)

--Larry Garfield

On Sunday, January 23, 2011 6:53:58 pm nan wich wrote:
> Larry, I am curious. Being that you are a database guru, this fits in with
> an issue for my Quotes module, as does the original post. [BTW, Blaine,
> rather than write you own, you might want to look at Quotes.]
> 
> One of the users who has an unusual number of quotes (~12K) says that ORDER
> RANDOM LIMIT 1 is not very efficient and gets fairly slow at that size.
> There are discussions elsewhere on the web to back up that assertion.
> 
> Do you find that to be true? Do you have a simpler way around it than
> keeping a separate index of the applicable nodes?
>  
> Nancy
>  
> Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L.
> King, Jr.
> 
> 
> 
> 
> ________________________________
> From: Larry Garfield <larry at garfieldtech.com>
> To: development at drupal.org
> Sent: Sun, January 23, 2011 7:41:34 PM
> Subject: Re: [development] Drupal 7 Entities - view random node
> 
> I did that all the time in Drupal 6 using Views.  It's dead simple.  (Order
> random limit 1, done.)  I haven't tried doing it in Drupal 7, but I'm sure
> the same method still works.
> 
> --Larry Garfield
> 
> On Sunday, January 23, 2011 1:33:34 pm Blaine Lang wrote:
> > I have a need for a block that will display a random quote. Node type =
> > 'random_quote' and only 1 random record should be displayed


More information about the development mailing list