Bulk insert

Overview

go-zero provides a simple bulk encapsulation that uses the scenario where, for example, there is a large number of logs that require bulk writing and can be used without attention to results.

Simple Example:

  1. var conn sqlx.SqlConn
  2. blk, err := sqlx.NewBulkInserter(conn, "insert into user (id, name) values (?, ?)")
  3. if err != nil {
  4. panic(err)
  5. }
  6. defer blk.Flush()
  7. blk.Insert(1, "test1")
  8. blk.Insert(2, "test2")

BulkInserter current logic will collect 1000 records or perform library operations at one time per sec.

The BulkInserter is based on executors.PeriodicalExecutor and will write the data when collecting enough data or when it meets a certain period of time, while his writing is asynchronous and the result can only be processed by callbacks.

Create BulkInserter

We created BulkInserter with sqlx.NewBulkInserter who needs a sqlx.SqlConn and an insert sql statement.

  1. func NewBulkInserter(sqlConn SqlConn, stmt string) (*BulkInserter, error)

Insert data

  1. func (bi *BulkInserter) Insert(args ...any) error

Be aware of args, for each parameter in insert, need and ? A counterpart.Also because Insert is an asynchronous operation, there will be no inserts back in this place.

flushes

Because insertion is an asynchronous process, if we have an immediate library or the program is about to exit, we need to manually flush it out.The framework has been added PeriodicalExecutor to pro.AddShutdown Listener.So there is no need to be interested in quitting operations and simply call flush on your own when the business needs it.

  1. blk.Flush()

Set Result Callback

If we have a business that needs to follow the results of each batch insertion, because our insertion is asynchronous, we need to set the results back manually.

  1. blk.SetResultHandler(func(result sql.Result, err error) {
  2. if err != nil {
  3. logx.Error(err)
  4. return
  5. }
  6. fmt.Println(result.RowsAffected())
  7. })