Schema ```model Profile { id String...
# orm-help
p
Schema
Copy code
model 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?
r
@Phil Bookst 👋 Only add indexes on values that you would query for frequently. Also in case of indexes, the writes would be slow with more number of indexes.
j
u can enable debug to check the underlying query or just set env debug="*"
p
hey guys thanks for the help! I include the image field in all of my queries so setting these indexes would be ok right?
r
Depends on your query. If you’re querying for
id
combined with other fields (using an
AND
) then the extra indexes are not required.
p
also is there a difference in doing this
Copy code
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?

  @@index([id, profileUsername]) <= index here
  @@index([id, hashtagName])
  @@index([id, locationId])
   
  @@map("image")
}
and this
Copy code
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")
}
I mostly query for findUnique profile and include the images related to this profile for example
r
findUnique
on which of the fields?
username
and
id
?
p
just username
or is there a performance benefit of including both username and id? 😮
Copy code
let user = await prisma.profile.findUnique({
    where: {
      username,
    },
    include: {
      images: {
        orderBy: {
          id: "desc",
        },
      },
    },
  });
r
In that case, only add an index to
username
.
But
username
is already an
@id
so an index will automatically be added.
p
Yes but the images field is the one slowing down the query without the index on it
r
Adding the index on the foreign key i.e.
profileUsername
should work then as the relation would be fetched from there.
p
Ok so just like in my first example right? setting it like this
@@index([id, profileUsername])
in the images table? or like this
@@index([profileUsername])
? Thank you very much for your help, I appreciate it big time!
🙌 1
r
I don’t think you need a combined index. Just
@@index([profileUsername])
should work.
🙌 1