Ben Guthrie
04/19/2022, 1:23 AMplaylistId
and videoId
as composite keys, you have a unique id
on the model. If you had the `playlistId`/`videoId` pairs, but not the id
s, is there an efficient method to find all of the rows for the pairs? (schema.prisma
and data in thread)Ben Guthrie
04/19/2022, 1:24 AMgenerator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model PlaylistVideo {
id String @id
playlistId String @map("playlist_id")
videoId String @map("video_id")
@@map("playlist_video")
}
Ben Guthrie
04/19/2022, 1:24 AMconst playlistVideos = [
{ playlistId: 'playlist-one', videoId: 'video-one' },
{ playlistId: 'playlist-one', videoId: 'video-two' },
// ... (hundreds of entries)
{ playlistId: 'playlist-ninety', videoId: 'video-eighty' }
]
await prisma.playlistVideo.findMany({
where: {
// ???
}
})
Hyo
04/19/2022, 5:38 AMNurul
04/19/2022, 8:55 AMBen Guthrie
04/19/2022, 2:09 PMid
for each of those distinct pairs of `playlistId`/`videoId`
@Nurul correct, and in bulk. I'm given a list of pairs of `playlistId`/`videoId` and I need to find the corresponding id
from playlistVideo
. Can you find
by a multi-column index? Say my new schema is as follows:
model PlaylistVideo {
id String @id
playlistId String @map("playlist_id")
videoId String @map("video_id")
@@map("playlist_video")
@@index(fields: [playlist_id, video_id], name: "multi_index")
}
I know this next part isn't the correct syntax, but is something like this possible?
await prisma.playlistVideo.findMany({
where: {
multi_index: {
in: [
{ playlistId: 'playlist-one', videoId: 'video-one' },
{ playlistId: 'playlist-one', videoId: 'video-two' },
// ... (hundreds of entries)
{ playlistId: 'playlist-ninety', videoId: 'video-eighty' }
]
}
}
})
Nurul
04/20/2022, 7:03 AMconst arr: any[] = [
{ playlistId: 'playlist-one', videoId: 'video-one' },
{ playlistId: 'playlist-one', videoId: 'video-two' },
];
for (let i = 0; i < arr.length; i++) {
const videos = await prisma.playlistVideo.findMany({
where: {
playlistId: arr[i].playlistId,
videoId: arr[i].videoId,
},
});
}
Ben Guthrie
04/20/2022, 3:15 PMOR
in the where
? And this would potentially be for hundreds of records at a time. That's why we were leaning away from one findMany
per record.
const arr: any[] = [
{ playlistId: 'playlist-one', videoId: 'video-one' },
{ playlistId: 'playlist-one', videoId: 'video-two' }
];
const videos = await prisma.playlistVideo.findMany({
where: {
OR: arr
},
});
Nurul
04/21/2022, 11:45 AMOR
operator is indeed a suitable solution. I would suggest logging time taken by query in both the cases, there shouldn’t be much difference but it would help in determining which approach to take. You can use middleware to log the time taken.Ben Guthrie
04/21/2022, 2:28 PMguyw
10/27/2022, 8:47 AMthis.prismaService.protectionAction.deleteMany({
where: {
OR: protectionActionsToDelete,
},
}),
and for big queries it took very long or even filled postgres ram entirely.
when we switched to
Prisma.sql`delete from protection_actions
where (scope_id, protection_permanent_id, profile_permanent_id) in ((${Prisma.join(
protectionActionsWhereTuple,
'),(',
)}))`
the times dropped by 90% and memory use by 75% (table has index on the required fields).
do you think the in
way of doing it will be supported by prisma soon?