Using Prisma and PostgreSQL:
model Chat {
id Int @id @default(autoincrement())
Users UserChat[] @relation(fields: [], references: [])
Keywords ChatKeyword[]
chatName String
}
model ChatKeyword {
id Int @id @default(autoincrement())
keyword String
Chat Chat? @relation(fields: [chatId], references: [id])
chatId Int?
}
model UserChat {
id Int @id @default(autoincrement())
Chat Chat @relation(fields: [chatId], references: [id])
chatId Int
User User @relation(fields: [userId], references: [id])
userId String
}
I want to query for chat records that:
- have a certain User.
- have a keyword.
const getChatSuggestions = ({ userId, keyword }) => {
db.chat.findMany({
where: {
Users: { some: { userId: { equals: userId } } },
Keywords: { some: { keyword: { contains: keyword } } },
},
})
}
How do I optimize my query for a table with filters on two columns that has a one-to-many relationship?
Does this query go through all the records of the Chat table?
I.e. if I have 1000 records in Chat, will it go through each Chat record's User and Keywords values?