Git Product home page Git Product logo

Comments (3)

spilliton avatar spilliton commented on September 28, 2024

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.

pascalbetz avatar pascalbetz commented on September 28, 2024

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.

spilliton avatar spilliton commented on September 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.