Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Feature: Adding Random option to query #254

Open
2 of 7 tasks
byawitz opened this issue Mar 31, 2023 · 7 comments
Open
2 of 7 tasks

🚀 Feature: Adding Random option to query #254

byawitz opened this issue Mar 31, 2023 · 7 comments
Assignees

Comments

@byawitz
Copy link
Member

byawitz commented Mar 31, 2023

🔖 Feature description

Adding the option to select n items as random.

The implementation would look something like this

$documents = static::getDatabase()->find('movies', [
    Query::random(),
    Query::limit(5),
]);

Or with limit as the random parameter.

$documents = static::getDatabase()->find('movies', [
    Query::random(5),
]);

Adapter are required for:

  • MariaDB
  • MySQL
  • SQLite
  • Postgres
  • MongoDB.

🎤 Pitch

I first intend to add it as a feature for Appwrite database endpoints.
Also, It can be helpful in games, for example.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@joeyouss
Copy link

Hi
Thank you for sharing this. How do you plan to implement this in MongoDB and MariaDB. Having a clear path about implementation might help our engineers understand more and then review PR. That being said, this feature would be a really cool one if implemented!

@byawitz
Copy link
Member Author

byawitz commented Mar 31, 2023

Thanks for your replay
Sure thing, I'm attaching basic query (of course I will adapt it).

Postgres

SELECT * FROM users ORDER BY random() LIMIT 5;

MySQL / MariaDB / SQLite

SELECT * FROM users ORDER BY RAND() LIMIT 5;

MongoDB

db.users.aggregate(
   [ { $sample: { size: 3 } } ]
)

@joeyouss joeyouss self-assigned this Apr 3, 2023
@joeyouss
Copy link

joeyouss commented Apr 3, 2023

Thanks for sharing, adding @fogelito for review/feedback on this idea.

@fogelito
Copy link
Contributor

fogelito commented Apr 4, 2023

Hello, @byawitz Thanks for sharing new ideas!
So this is possible to do as more community members will need to vote for this feature.
random on big tables is always a table scan so better use on small collections.
So in the meanwhile, with the coming new release you can query with a relatively big limit, and use your client code to random the results fetched.

@byawitz
Copy link
Member Author

byawitz commented Apr 4, 2023

Hey, @fogelito. Of course, that can be a slow query in RDBMS, I agree, Though there is a way to mitigate it by using to know the number of rows and adding it as a pre-defined var.

That means that for every insert to the database, a counter of it will be added (and decreased any delete), this can be another great feature for any case.

@fogelito
Copy link
Contributor

fogelito commented Apr 4, 2023

@byawitz Correct, As to count all rows in the collection, but in the case of filtering queries, it will be hard to count all options.

@byawitz
Copy link
Member Author

byawitz commented Apr 4, 2023

Thanks @fogelito,
Agree. Let's wait for (if any) votes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants