Home > Software engineering >  Does gorm use indexes by default?
Does gorm use indexes by default?

Time:07-07

I would like to utilize indexes in db

Does gorm use index by default on it's queries or I should specify manually?

type User struct {
ID uint
Name string `gorm:"uniqueIndex:idx_user_name"
}

Option A

var user User
db.Find(&user)

from terminal I don't see if option A is using index

Option B

import "gorm.io/hints"

db.Clauses(hints.UseIndex("idx_user_name")).Find(&User{})
// SELECT * FROM `users` USE INDEX (`idx_user_name`)

CodePudding user response:

Postgres does not support USE INDEX hinting syntax. It will automatically choose the appropriate index. Using gorm won't change that.

  • Related