GORM optimizes many things to improve the performance, the default performance should be good for most applications, but there are still some tips for how to improve it for your application.

Disable Default Transaction

GORM performs write (create/update/delete) operations inside a transaction to ensure data consistency, which is bad for performance, you can disable it during initialization

  1. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  2. SkipDefaultTransaction: true,
  3. })

Caches Prepared Statement

Creates a prepared statement when executing any SQL and caches them to speed up future calls

  1. // Globally mode
  2. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  3. PrepareStmt: true,
  4. })
  5. // Session mode
  6. tx := db.Session(&Session{PrepareStmt: true})
  7. tx.First(&user, 1)
  8. tx.Find(&users)
  9. tx.Model(&user).Update("Age", 18)

NOTE Also refer how to enable interpolateparams for MySQL to reduce roundtrip https://github.com/go-sql-driver/mysql#interpolateparams

SQL Builder with PreparedStmt

Prepared Statement works with RAW SQL also, for example:

  1. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  2. PrepareStmt: true,
  3. })
  4. db.Raw("select sum(age) from users where role = ?", "admin").Scan(&age)

You can also use GORM API to prepare SQL with DryRun Mode, and execute it with prepared statement later, checkout Session Mode for details

Select Fields

By default GORM select all fields when querying, you can use Select to specify fields you want

  1. db.Select("Name", "Age").Find(&Users{})

Or define a smaller API struct to use the smart select fields feature

  1. type User struct {
  2. ID uint
  3. Name string
  4. Age int
  5. Gender string
  6. // hundreds of fields
  7. }
  8. type APIUser struct {
  9. ID uint
  10. Name string
  11. }
  12. // Select `id`, `name` automatically when query
  13. db.Model(&User{}).Limit(10).Find(&APIUser{})
  14. // SELECT `id`, `name` FROM `users` LIMIT 10

Iteration / FindInBatches

Query and process records with iteration or in batches

Index Hints

Index is used to speed up data search and SQL query performance. Index Hints gives the optimizer information about how to choose indexes during query processing, which gives the flexibility to choose a more efficient execution plan than the optimizer

  1. import "gorm.io/hints"
  2. db.Clauses(hints.UseIndex("idx_user_name")).Find(&User{})
  3. // SELECT * FROM `users` USE INDEX (`idx_user_name`)
  4. db.Clauses(hints.ForceIndex("idx_user_name", "idx_user_id").ForJoin()).Find(&User{})
  5. // SELECT * FROM `users` FORCE INDEX FOR JOIN (`idx_user_name`,`idx_user_id`)"
  6. db.Clauses(
  7. hints.ForceIndex("idx_user_name", "idx_user_id").ForOrderBy(),
  8. hints.IgnoreIndex("idx_user_name").ForGroupBy(),
  9. ).Find(&User{})
  10. // SELECT * FROM `users` FORCE INDEX FOR ORDER BY (`idx_user_name`,`idx_user_id`) IGNORE INDEX FOR GROUP BY (`idx_user_name`)"

Read/Write Splitting

Increase data throughput through read/write splitting, check out Database Resolver