Hi there, what the most efficient way to query a r...
# orm-help
m
Hi there, what the most efficient way to query a random element of a type from prisma? Querying all elements of a type and selecting a random one seems incredably inefficient. Is there a better approach?
j
this works for me
Copy code
async randomLineItem(parent, args, ctx: Context, info) {
    const connection = await ctx.db.query.lineItemsConnection(
      {},
      `{
      aggregate {
        count
      }
    }`,
    )

    const randomIndex = getRandom({
      min: 0,
      max: connection.aggregate.count, // this is the current number or lineItems in the db
    })

    const lis = await ctx.db.query.lineItems({ skip: randomIndex, first: 1 }, info)

    return lis[0]
  }
if you need something quicker
you probably would go with a raw SQL query and get a random row
m
Hi @jaga_santagostino, this is exactly what I was looking for! Thanks a lot