Skip to content

Commit

Permalink
avoid starting a transaction when performing only one insert operatio…
Browse files Browse the repository at this point in the history
…n in CreateInBatches function (#6174)
  • Loading branch information
chenyahui committed Mar 23, 2023
1 parent 1a7ea98 commit 5d1cdfe
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions finisher_api.go
Expand Up @@ -35,9 +35,10 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
var rowsAffected int64
tx = db.getInstance()

// the reflection length judgment of the optimized value
reflectLen := reflectValue.Len()

callFc := func(tx *DB) error {
// the reflection length judgment of the optimized value
reflectLen := reflectValue.Len()
for i := 0; i < reflectLen; i += batchSize {
ends := i + batchSize
if ends > reflectLen {
Expand All @@ -55,7 +56,7 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
return nil
}

if tx.SkipDefaultTransaction {
if tx.SkipDefaultTransaction || reflectLen <= batchSize {
tx.AddError(callFc(tx.Session(&Session{})))
} else {
tx.AddError(tx.Transaction(callFc))
Expand Down

0 comments on commit 5d1cdfe

Please sign in to comment.