FindInBatches using Offset instead of PrimaryKey iterations
This commit is contained in:
parent
79f427d862
commit
fae7229d9e
@ -167,18 +167,16 @@ func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindInBatches find records in batches
|
// FindInBatches find records in batches
|
||||||
func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, batch int) error) *DB {
|
func (db *DB) FindInBatch(dest interface{}, batchSize int, fc func(tx *DB, batch int) error) *DB {
|
||||||
var (
|
var (
|
||||||
tx = db.Order(clause.OrderByColumn{
|
tx = db.Session(&Session{})
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
|
||||||
}).Session(&Session{})
|
|
||||||
queryDB = tx
|
queryDB = tx
|
||||||
rowsAffected int64
|
rowsAffected int64
|
||||||
batch int
|
batch int
|
||||||
)
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
result := queryDB.Limit(batchSize).Find(dest)
|
result := queryDB.Limit(batchSize).Offset(batch*batchSize).Find(dest)
|
||||||
rowsAffected += result.RowsAffected
|
rowsAffected += result.RowsAffected
|
||||||
batch++
|
batch++
|
||||||
|
|
||||||
@ -190,15 +188,6 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
|
|||||||
|
|
||||||
if tx.Error != nil || int(result.RowsAffected) < batchSize {
|
if tx.Error != nil || int(result.RowsAffected) < batchSize {
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
resultsValue := reflect.Indirect(reflect.ValueOf(dest))
|
|
||||||
if result.Statement.Schema.PrioritizedPrimaryField == nil {
|
|
||||||
tx.AddError(ErrPrimaryKeyRequired)
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
primaryValue, _ := result.Statement.Schema.PrioritizedPrimaryField.ValueOf(resultsValue.Index(resultsValue.Len() - 1))
|
|
||||||
queryDB = tx.Clauses(clause.Gt{Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, Value: primaryValue})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user