dialect/sql/sqlgraph: avoid creating tx blocks for single statement create operation

For example, 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 CreateNode operation with single SQL statement.

Benchmark was improved from:

	(2000 Inserts)   8.41s      4206748 ns/op    4595 B/op    115 allocs/op

To:

	(2000 Inserts)   4.66s      2330222 ns/op    4107 B/op    104 allocs/op
This commit is contained in:
Ariel Mashraki
2021-08-20 16:24:33 +03:00
committed by Ariel Mashraki
parent f326c7dfd0
commit ea67be12a4
2 changed files with 53 additions and 56 deletions

View File

@@ -851,11 +851,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `users` (`age`, `name`) VALUES (?, ?)")).
WithArgs(30, "a8m").
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -872,11 +870,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `users` (`age`, `name`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `age` = VALUES(`age`), `name` = VALUES(`name`), `id` = LAST_INSERT_ID(`users`.`id`)")).
WithArgs(30, "a8m").
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -890,11 +886,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `users` (`age`, `name`, `id`) VALUES (?, ?, ?)")).
WithArgs(30, "a8m", 1).
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -907,11 +901,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `users` (`json`) VALUES (?)")).
WithArgs([]byte("{}")).
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -927,11 +919,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `pets` (`name`, `owner_id`) VALUES (?, ?)")).
WithArgs("pedro", 2).
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -947,11 +937,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `cards` (`number`, `owner_id`) VALUES (?, ?)")).
WithArgs("0001", 2).
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
{
@@ -1156,11 +1144,9 @@ func TestCreateNode(t *testing.T) {
},
},
expect: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectExec(escape("INSERT INTO `mydb`.`users` (`age`, `name`) VALUES (?, ?)")).
WithArgs(30, "a8m").
WillReturnResult(sqlmock.NewResult(1, 1))
m.ExpectCommit()
},
},
}