背景

thinkphp中批量查询数据时使用 chunk 方法, golang中也有相应的方法:
使用 FindInBatches 方法即可。需要使用 gorm v2版本。切记!

go mod 中相关引用可以参考:

    gorm.io/driver/mysql v1.1.3
    gorm.io/gorm v1.22.2

FindInBatches 方法使用:

// batch size 100
result := db.Where("processed = ?", false).FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error {
  for _, result := range results {
    // batch processing found records
  }

  tx.Save(&results)

  tx.RowsAffected // number of records in this batch

  batch // Batch 1, 2, 3

  // returns error will stop future batches
  return nil
})

result.Error // returned error
result.RowsAffected // processed records count in all batches

gorm Change Log

v2.0 – 2020.08

GORM 2.0 is a rewrite from scratch, it introduces some incompatible-API change and many improvements

  • Performance Improvements
  • Modularity
  • Context, Batch Insert, Prepared Statement Mode, DryRun Mode, Join Preload, Find To Map, Create From Map, FindInBatches supports
  • Nested Transaction/SavePoint/RollbackTo SavePoint supports
  • Named Argument, Group Conditions, Upsert, Locking, Optimizer/Index/Comment Hints supports, SubQuery improvements
  • Full self-reference relationships supports, Join Table improvements, Association Mode for batch data
  • Multiple fields support for tracking create/update time, which adds support for UNIX (milli/nano) seconds
  • Field permissions support: read-only, write-only, create-only, update-only, ignored
  • New plugin system: multiple databases, read/write splitting support with plugin Database Resolver, prometheus integrations…
  • New Hooks API: unified interface with plugins
  • New Migrator: allows to create database foreign keys for relationships, constraints/checker support, enhanced index support
  • New Logger: context support, improved extensibility
  • Unified Naming strategy: table name, field name, join table name, foreign key, checker, index name rules
  • Better customized data type support (e.g: JSON)
  • GORM 2.0 Release Note

v1.0 – 2016.04

Breaking Changes:

  • gorm.Open returns *gorm.DB instead of gorm.DB
  • Updating will only update changed fields
  • Soft Delete’s will only check deleted_at IS NULL
  • New ToDBName logic
  • Common initialisms from golint like HTTP, URI was converted to lowercase, so HTTP‘s db name is http, but not h_t_t_p, but for some other initialisms not in the list, like SKU, it’s db name was s_k_u, this change fixed it to sku
  • Error RecordNotFound has been renamed to ErrRecordNotFound
  • mssql dialect has been renamed to github.com/jinzhu/gorm/dialects/mssql
  • Hstore has been moved to package github.com/jinzhu/gorm/dialects/postgres