diff options
| author | Jonathan Amsterdam <jba@google.com> | 2020-06-09 07:17:17 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2020-06-09 15:02:07 +0000 |
| commit | 7e391c3fb7c3fc5ff8ff3366dfba0012fade1bcf (patch) | |
| tree | c639e7277e0ff7a74c7d61362385b7fca540d658 /internal/database/database.go | |
| parent | 465b4fd31d6406fa613cc74a27c4194927b77ea0 (diff) | |
| download | go-x-pkgsite-7e391c3fb7c3fc5ff8ff3366dfba0012fade1bcf.tar.xz | |
internal/database: BulkInsert: only prepare when necessary
We were preparing the full statement (of size stride) even if we never used
it, because the number of values to insert was less than stride.
Also, tweak TestLargeBulkInsert:
- the logging is no longer voluminous, so we don't have to turn it off;
- make the number of values not divisible by 1000, so we test the case
where rightBound > len(values).
Change-Id: I169130ab370b192a629025886173b1e33f5d158b
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/766240
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal/database/database.go')
| -rw-r--r-- | internal/database/database.go | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/internal/database/database.go b/internal/database/database.go index 0499a199..25c90b96 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -258,17 +258,23 @@ func (db *DB) bulkInsert(ctx context.Context, table string, columns, returningCo // handle it cautiously. return fmt.Errorf("too many columns to insert: %d", len(columns)) } - fullQuery := buildInsertQuery(table, columns, returningColumns, stride, conflictAction) - stmt, err := db.Prepare(ctx, fullQuery) - if err != nil { - return err + + prepare := func(n int) (*sql.Stmt, error) { + return db.Prepare(ctx, buildInsertQuery(table, columns, returningColumns, n, conflictAction)) } - defer stmt.Close() + + var stmt *sql.Stmt for leftBound := 0; leftBound < len(values); leftBound += stride { rightBound := leftBound + stride - if rightBound > len(values) { + if rightBound <= len(values) && stmt == nil { + stmt, err = prepare(stride) + if err != nil { + return err + } + defer stmt.Close() + } else if rightBound > len(values) { rightBound = len(values) - stmt, err = db.Prepare(ctx, buildInsertQuery(table, columns, returningColumns, rightBound-leftBound, conflictAction)) + stmt, err = prepare(rightBound - leftBound) if err != nil { return err } |
