【发布时间】:2021-10-05 02:34:59
【问题描述】:
问题与Find entity with most relations filtered by criteria 非常相似,但略有不同。
model Player {
id String @id
name String @unique
game Game[]
}
model Game {
id String @id
isWin Boolean
playerId String
player Player @relation(fields: [playerId], references: [id])
}
我想找出胜率最高的 10 位玩家(isWin=true 的游戏数除以该玩家的游戏总数)。
直接而缓慢的方法是找到至少赢过一次的所有玩家并计算他们的胜利(第一次查询)。然后为他们每个人计算他们的游戏总数(第二个查询)。然后在应用程序端进行数学运算和排序,同时将结果保存在内存中。
有没有更简单的方法来做到这一点?我将如何使用prisma 做到这一点?如果没有 prisma “本机”方式来做到这一点,那么使用原始 SQL 最有效的方式是什么?
【问题讨论】: