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

Get random records from findMany() and also with pagination #5894

Open
hyochan opened this issue Feb 28, 2021 · 14 comments
Open

Get random records from findMany() and also with pagination #5894

hyochan opened this issue Feb 28, 2021 · 14 comments

Comments

@hyochan
Copy link
Contributor

hyochan commented Feb 28, 2021

Problem

Comming from #5886.

As discussed in Prisma 1 forum, looks like there is no way to find random records with findMany. Indeed, this is such a challenge to use with pagination.

Suggested solution

Providing a random function from prisma would be an easy solution for users therefore we can expect results with orderBy: random(). Although this results in a bad performance in pure sql, I think Prisma can do better internally without just exposing the random inside the sql.

Alternatives

If the below pagination query and raw query could aggregate, it would scope down many limitations.

prisma.item.findMany({
  ...relayToPrismaPagination({
    after,
    before,
    first,
    last,
  }),
  where: {
    ...filters,
  },
  orderBy: {id: 'desc'},
});
select * from "Item" limit 20
offset floor(random() * (select count(*) from "Item"));
@pantharshit00 pantharshit00 added kind/feature A request for a new feature. team/client Issue for team Client. topic: client api labels Mar 1, 2021
@naothomachida

This comment was marked as off-topic.

@DraftProducts
Copy link

Is this feature planned?
There is still no way today to retrieve a row random in the database with Prisma.

@ryancwalsh
Copy link

I've been checking back on this issue and these docs since June 2020 or earlier, and I see that others have wanted this functionality since Feb 2017 or earlier.

@schickling I'm curious whether you think anyone will work on this soon?

Also, I propose an optional seed: number argument (in addition to the first: number argument proposed here).

Sometimes it's helpful to let the client generate and store a seed (such as from a random number generator) that it can then pass to this backend function, which will then return the results sorted in a way that will always be the same (given that the full array of results hasn't changed in the meantime and the same seed was provided).

See the following example of some shuffling code I've used (inspired by Mike Bostock's implementation of the Fisher–Yates algorithm):

/**
 *
 * @param {array} array
 * @param {number} seed
 * @returns {array}
 */
export function shuffle(array: any, seed: number) {
  // https://stackoverflow.com/a/53758827/

  const result = [...array];
  let a = result.length;
  let b;
  let elementToMove;
  let currentSeed = seed;

  // While there remain elements to shuffle…
  while (a) {
    // Pick a remaining element…
    a -= 1;
    b = Math.floor(random(currentSeed) * a);

    // And swap it with the current element.
    elementToMove = result[a];
    result[a] = result[b];
    result[b] = elementToMove;
    currentSeed += 1;
  }

  return result;
}

@deemeetree
Copy link

Yes, we absolutely need this feature. Please, consider adding it into the next upgrades.

@janpio janpio changed the title Get random records from findMany and also with pagination Get random records from findMany and also with pagination Nov 5, 2022
@ansh

This comment was marked as off-topic.

@kaloyanBozhkov

This comment was marked as off-topic.

@Zenoo

This comment was marked as off-topic.

@hjKangIB

This comment was marked as off-topic.

@kblizeck

This comment was marked as off-topic.

@najibghadri

This comment was marked as off-topic.

@iamthe-Wraith

This comment was marked as off-topic.

@EliasTouil
Copy link

I will find a workaround but I am also very interested in this feature.

@bhupesh98
Copy link

bhupesh98 commented Dec 14, 2023

I required this feature too, please tag me whenever there's update to this issue. Absence of advanced SQL functions is making me dismiss the usage of prisma in further projects.

There must be a totally raw query option which can bypass prisma engine and send query directly to db without any syntax checking

@GauravBR
Copy link

GauravBR commented Jan 1, 2024

Is there any way to get only one random value after adding initial filter like where:{ }?

@janpio janpio changed the title Get random records from findMany and also with pagination Get random records from findMany() and also with pagination Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests