question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support query for random row(s)

See original GitHub issue

Problem

In Prisma 1 there was conversation around potentially including a way to query for a random row(s) in the database. See https://github.com/prisma/prisma1/issues/114. I think this should still be considered in Prisma 2 (and don’t want the feature request to get lost in the transition to this new repo).

Without this feature, in order to do random lookup of the table you would first need make a call for the count of the number of rows in a table. Then create a random number bounded by that count. And make another call to find that row. I think prisma has the potential to make this feature usable and performant.

Suggested solution

In the linked issue (https://github.com/prisma/prisma1/issues/114) the discussion lead to utilizing the orderBy with a random option seems to make the most sense. But, maybe that random option should only be available to id fields as to not make it confusing.

Also, how this is solved in the Engine I’m less sure. As each database can solve this differently and probably some queries are more/less performant than others.

Alternatives

Would love some more conversation around this.

Additional context

Again think prisma is well poised to make this a usable performant feature. Because doing it in sql is very database dependent and very non-performant if you aren’t careful. See this discussion: https://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:25
  • Comments:6

github_iconTop GitHub Comments

1reaction
naothomachidacommented, Jul 23, 2021

+1

0reactions
deepslamcommented, Nov 18, 2021

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to request a random row in SQL? - Stack Overflow
The first one is WRONG in SQL Server. The RAND() function is invoked only once per query not once per row. So it...
Read more >
SQL to Select a random row from a database table - Pete Freitag
Another great solution to selecting a random row: count number of rows in the table, pick a random value from 0 to that...
Read more >
SQL SELECT RANDOM - Javatpoint
SQL SELECT RANDOM. The SQL SELECT RANDOM() function returns the random row. It can be used in online exam to display the random...
Read more >
How to randomly select rows in SQL? - Intellipaat Community
Following queries are used to select a random row in SQL: Select a random row with MySQL like this: QUERY. SELECT column FROM...
Read more >
Data Sampling: Techniques for Efficiently Finding a Random ...
None of the queries presented here need a full table (or index) scan. Each has a time proportional to the number of rows...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found