Comments (3)
Yes this is certainly an issue, but I'm not sure the best way around it.
I suppose in the cases when only 1 record is desired, we might first do a count of the records that match the criteria, then do the query again with limit 1 and a random offset. Then maybe keep doing this for each additional record? This might be more performant when small numbers of records are requested.
Or perhaps the more common way of doing random ( order by rand() in the DB ), might be more performant with large record sets.
In either case, I'll do some benchmarking with the above to ideas and compare them to the existing algorithm with super large record sets.
Any suggestions or thoughts?
from randumb.
I changed my DB schema to have much smaller amount of records in need random access to. So order by random() works fast enough.
Depending on the query you could also do
- m = select max(id) from table"
- select * from table where id >= rand(m + 1)
(if you have additional where conditions then this might not be evenly distributed)
There is no ideal solution to this, i guess. Perhaps just describe how the gem is doing it (and alternatives) so one does not have to read the source code.
from randumb.
After some testing, it turns out that it is almost always faster to just order by random at the database level. I just updated the gem to do this, should work for mysql, sqlite, and postgres. So no more ids in memory :)
from randumb.
Related Issues (17)
- Performance HOT 3
- undefined method 'uniq_value' HOT 7
- License HOT 2
- Not working on Rails 3.2.8? HOT 5
- Randumb doesn't work in Rails 4 HOT 1
- SQLite 3 - Statement Invalid - Missing Asterisk HOT 9
- [feature] add seed parameter HOT 6
- It is possible that this returns an activerecord_relation instead of a an array? HOT 4
- Add change log entry for 0.5.0 HOT 4
- is it garanteed that there is no duplicate records HOT 5
- wheight by created_at HOT 6
- Does not respect default scopes HOT 1
- NoMethodError - undefined method `reverse_merge' for 30:Fixnum HOT 4
- undefined method `shuffle!' HOT 4
- Random but unique/distinct? HOT 1
- order by error. HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from randumb.