InfoSphere/GORM 中文版/ 返回书籍
Tutorials

Performance

qianmoQqianmoQ· 更新于 2026-09-15· 阅读 9 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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

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

Caches Prepared Statement

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


db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  PrepareStmt: true,
})


tx := db.Session(&Session{PrepareStmt: true})
tx.First(&user, 1)
tx.Find(&users)
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:

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  PrepareStmt: true,
})

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

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

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

type User struct {
  ID     uint
  Name   string
  Age    int
  Gender string
  
}

type APIUser struct {
  ID   uint
  Name string
}


db.Model(&User{}).Limit(10).Find(&APIUser{})

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

import "gorm.io/hints"

db.Clauses(hints.UseIndex("idx_user_name")).Find(&User{})


db.Clauses(hints.ForceIndex("idx_user_name", "idx_user_id").ForJoin()).Find(&User{})


db.Clauses(
  hints.ForceIndex("idx_user_name", "idx_user_id").ForOrderBy(),
  hints.IgnoreIndex("idx_user_name").ForGroupBy(),
).Find(&User{})

Read/Write Splitting

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

GitHub tag (latest SemVer)

评论

登录后参与评论

正在加载评论…