0

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`)
Luqman Jr
  • 69
  • 1
  • 5

1 Answers1

1

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

Bergi
  • 630,263
  • 148
  • 957
  • 1,375