-
Hello everyone! I'm trying to switch my prisma schema to mysql with A) Does the order of the fields in the index attribute matter? B) Why vscode does not complain about second relation? I have two relations to the same model, but vscode doesn't show a warning for the first relation if it doesn't have an index. And if I need to set up an additional index, how should I do it? @@index([followerUserId, followingUserId]) or @@index([followerUserId])
@@index([followingUserId]) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hello @smashboy 👋
The order in which you pass the column names in the Ideally, the first column in the index should have high selectivity i.e. if the first column has a large number of unique values relative to the total number of rows in the table, then it will help narrow down the search more efficiently, leading to better query performance.
Do you get the warning for the second relation? Also, if you can share the schema in text format instead of message it would be much easier in trying it locally.
This would depend on how you are querying. If your find query is filtering on await prisma.userfollower.findMany({
where: {
followerUserId: '__id__',
},
}); @@index([followerUserId])
@@index([followingUserId]) But if your find query has filtering on both columns in the same query await prisma.userfollower.findMany({
where: {
followerUserId: '__id__',
followeingUserId: '__id__',
},
}); then you should define it as @@index([followerUserId, followingUserId]) |
Beta Was this translation helpful? Give feedback.
-
No, I get a warning only for one relation. Here is a schema: generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
relationMode = "prisma"
}
model User {
id String @id @default(cuid())
createdAt DateTime @default(value: now())
updatedAt DateTime @updatedAt
name String?
followers UserFollower[] @relation("following")
following UserFollower[] @relation("followers")
}
model UserFollower {
createdAt DateTime @default(value: now())
updatedAt DateTime @updatedAt
following User @relation("following", fields: [followingUserId], references: [id])
followingUserId String
follower User @relation("followers", fields: [followerUserId], references: [id]) // only this relation has a warning
followerUserId String
@@unique([followingUserId, followerUserId])
}
|
Beta Was this translation helpful? Give feedback.
Hello @smashboy 👋
The order in which you pass the column names in the
@relation
attribute would be the same for creating the underlying composite index. The order of columns determines which types of queries can use the index efficiently.Ideally, the first column in the index should have high selectivity i.e. if the first column has a large number of unique values relative to the total number of rows in the table, then it will help narrow down the search more efficiently, leading to better query performance.
Do you get the warning for the second relation? Also, if you can sh…