Quering random row
Hello, what would be the best way to query a random row from DB via Convex? I have a game project where users should get a random image, each image should be a row in DB (ideally) and there will be more than 100 images.
15 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
I had an idea to have an unique field with int value and then have index. Each value would be 0, 1, 2 and etc. So that way I could just do Math.random and then use index in order to find the document but I'm not sure if it's the best way
If your images count stays around 100 or even 500. the Sequential ID you defined would work fine!
What if it's gonna be more than 500?
I was thinking about using a different service where I could save Convex document ID and then use SQL Random command
This question came up a few days ago: https://discord.com/channels/1019350475847499849/1283999801188421634
Your idea about using an int isn't far off from what was suggested in that thread
Thank you!🙏
Your original solution is a great one @TwendyKirn. It generally provides more "truly random" selection as well as stronger capabilities for quite complex random selection use cases (for example "give me a batch of 20 random documents, and then in the next batch, give me 20 different random documents"). However, it very easily becomes quite a lot more cumbersome and complex to maintain than the float-solution.
It sounds like your use case is quite simple: fetch one random image from the table. For this, the float number solution from the thread linked to is perfect, and the incrementing integer-approach that you mentioned would be "overkill" as well as potentially/probably necessitating some headache-inducing maintenance 🙂
Let me know if you need some help deciding on or implementing a solution 🙂 Ive learned a lot from thinking about and implementing different randomization solutions!
@djbalin Hey, thank you for your feedback! I will try to build a simple prototype and then write back)
What would be a chance to get 2 same random float numbers? I assume super small)
Yeah virtually impossible, see screenshot. But you can (and, depending on your usecase: should) handle possible duplicates in the frontend, e.g. by filtering over an array or adding to a Set or something 🙂
I would assume that if field has unique tag then Convex would return error by default in case of same float (1 in db and 1 in new create)
there is no unique tag in convex. But personally i wouldn't worry about duplicate random floats
also out of curiosity if we could provide an easy way to get a random document, would you be interested? I think that's likely to be available this week to those interested
You've got my vote!
Absolutely yes!! 😍 We've discussed why .random() isn't in Convex yet already, and concluded that you probably want to not bloat it prematurely with all sorts of specific use cases before there is too much request for it. But this would be great!!
Big yes! We are looking at using quite a lot of randomization in our app, anything helping us in this area would be suuper helpfull!
Hey, yes, that would be nice!