Phil Bookst
10/04/2021, 10:49 AMmodel Profile {
id String? @unique
username String @id
profile_pic_url String?
follower Int?
following Int?
images Image[]
@@map("profile")
}
model Location {
id String @unique
name String @id
images Image[]
@@map("location")
}
model Hashtag {
id String? @unique
name String @id
images Image[]
@@map("hashtag")
}
model Image {
id String @id
thumbnail String
shortcode String @unique
like_count Int
comment_count Int
profile Profile? @relation(fields: [profileUsername], references: [username])
profileUsername String?
hashtag Hashtag? @relation(fields: [hashtagName], references: [name])
hashtagName String?
location Location? @relation(fields: [locationId], references: [id])
locationId String?
// Is this ok?
@@index([id, profileUsername])
@@index([id, hashtagName])
@@index([id, locationId])
//
@@map("image")
}
Hey guys, quick question - is it ok to set indexes on my relations? I've experienced slow reads but since I added the indexes above the speed has increased a lot!
Is this safe to do or will it cause problems in the future when the tables get bigger?Ryan
10/04/2021, 10:52 AMjames tan
10/04/2021, 11:06 AMPhil Bookst
10/04/2021, 11:09 AMRyan
10/04/2021, 11:17 AMid
combined with other fields (using an AND
) then the extra indexes are not required.Phil Bookst
10/04/2021, 11:17 AMmodel Image {
id String @id
thumbnail String
shortcode String @unique
like_count Int
comment_count Int
profile Profile? @relation(fields: [profileUsername], references: [username])
profileUsername String?
hashtag Hashtag? @relation(fields: [hashtagName], references: [name])
hashtagName String?
location Location? @relation(fields: [locationId], references: [id])
locationId String?
@@index([id, profileUsername]) <= index here
@@index([id, hashtagName])
@@index([id, locationId])
@@map("image")
}
and this
model Profile {
id String? @unique
username String @id
profile_pic_url String?
follower Int?
following Int?
images Image[]
// index here
@@index([username, images])
@@map("profile")
}
Phil Bookst
10/04/2021, 11:18 AMRyan
10/04/2021, 11:20 AMfindUnique
on which of the fields? username
and id
?Phil Bookst
10/04/2021, 11:21 AMPhil Bookst
10/04/2021, 11:21 AMPhil Bookst
10/04/2021, 11:22 AMlet user = await prisma.profile.findUnique({
where: {
username,
},
include: {
images: {
orderBy: {
id: "desc",
},
},
},
});
Ryan
10/04/2021, 11:23 AMusername
.Ryan
10/04/2021, 11:24 AMusername
is already an @id
so an index will automatically be added.Phil Bookst
10/04/2021, 11:25 AMRyan
10/04/2021, 11:36 AMprofileUsername
should work then as the relation would be fetched from there.Phil Bookst
10/04/2021, 11:40 AM@@index([id, profileUsername])
in the images table?
or like this @@index([profileUsername])
?
Thank you very much for your help, I appreciate it big time!Ryan
10/04/2021, 11:42 AM@@index([profileUsername])
should work.