mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
dialect/sql/sqlgraph: avoid creating tx blocks for single DELETE statements
In PostgreSQL, every statement is executed within a transaction. Therefore, we can avoid creating transaction blocks manually (group of statements surrounded by BEGIN and COMMIT) for DeleteNodes operation as it's implemented with a single statement. Benchmark for 4000 operations was improved from: 14.54s 7270455 ns/op 3702 B/op 81 allocs/op To: 5.36s 2679935 ns/op 2864 B/op 59 allocs/op --- MySQL and SQLite share the same behavior. Please see https://github.com/ent/ent/pull/1858 for more info.
This commit is contained in:
committed by
Ariel Mashraki
parent
ee606f4e86
commit
b23a0e8554
@@ -472,10 +472,6 @@ type DeleteSpec struct {
|
||||
|
||||
// DeleteNodes applies the DeleteSpec on the graph.
|
||||
func DeleteNodes(ctx context.Context, drv dialect.Driver, spec *DeleteSpec) (int, error) {
|
||||
tx, err := drv.Tx(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var (
|
||||
res sql.Result
|
||||
builder = sql.Dialect(drv.Dialect())
|
||||
@@ -487,14 +483,14 @@ func DeleteNodes(ctx context.Context, drv dialect.Driver, spec *DeleteSpec) (int
|
||||
pred(selector)
|
||||
}
|
||||
query, args := builder.Delete(spec.Node.Table).Schema(spec.Node.Schema).FromSelect(selector).Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return 0, rollback(tx, err)
|
||||
if err := drv.Exec(ctx, query, args, &res); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
affected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, rollback(tx, err)
|
||||
return 0, err
|
||||
}
|
||||
return int(affected), tx.Commit()
|
||||
return int(affected), nil
|
||||
}
|
||||
|
||||
// QuerySpec holds the information for querying
|
||||
|
||||
Reference in New Issue
Block a user