ging / social_stream

A framework for building distributed social network websites
http://social-stream.dit.upm.es/
MIT License
1.13k stars 307 forks source link

sctor suggestions method is very very slow with candidates.delete_at rand(candidates.size) #482

Open leopoldchen opened 10 years ago

leopoldchen commented 10 years ago

When there were thousands of users for suggestion, this method seems like cost 2-3 second. Then I google how to get random records by sql in rails, here is the result I got:

  Model.first(:order => "RANDOM()") # postgres example

In this way, the solution could be

 candidates = candidates.order("RANDOM()").limit(size)

This will more faster then the original code.

But different database get different rand function. See detail in this url: http://www.petefreitag.com/item/466.cfm I don't know how many kinds of database SocialStream need to support. With a support list, it might could optimize it with a method to get current random func for current database.

Anyway, for somebody who got this problem, could just use the decorator to rewrite this method.