From fdf6ac4ac763fd14c4f944eb716fc689acd41f4e Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 5 Dec 2019 12:42:03 -0800 Subject: [PATCH] Couple performance fixes: 1) cache the SQL schema for a table, and 2) only update output every 500 lines. Together these give us a 60% speedup on a large import. Signed-off-by: Zach Musgrave --- go/cmd/dolt/commands/sql.go | 13 ++++++++++-- go/libraries/doltcore/sqle/tables.go | 30 +++++++++++++++------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/go/cmd/dolt/commands/sql.go b/go/cmd/dolt/commands/sql.go index a8ebec1060..5295fc9f4e 100755 --- a/go/cmd/dolt/commands/sql.go +++ b/go/cmd/dolt/commands/sql.go @@ -201,6 +201,8 @@ func runBatchMode(ctx context.Context, se *sqlEngine) error { query = "" } + updateBatchInsertOutput() + if err := scanner.Err(); err != nil { cli.Println(err.Error()) } @@ -472,6 +474,7 @@ var batchEditStats stats var displayStrLen int const maxBatchSize = 50000 +const updateInterval = 500 // Processes a single query in batch mode. The Root of the sqlEngine may or may not be changed. func processBatchQuery(ctx context.Context, query string, se *sqlEngine) error { @@ -499,8 +502,9 @@ func processBatchQuery(ctx context.Context, query string, se *sqlEngine) error { } } - displayStr := fmt.Sprintf("Rows inserted: %d", batchEditStats.numRowsInserted) - displayStrLen = cli.DeleteAndPrint(displayStrLen, displayStr) + if batchEditStats.numRowsInserted%updateInterval == 0 { + updateBatchInsertOutput() + } return nil default: @@ -520,6 +524,11 @@ func processBatchQuery(ctx context.Context, query string, se *sqlEngine) error { } } +func updateBatchInsertOutput() { + displayStr := fmt.Sprintf("Rows inserted: %d", batchEditStats.numRowsInserted) + displayStrLen = cli.DeleteAndPrint(displayStrLen, displayStr) +} + // Updates the batch insert stats with the results of an insert operation. func mergeInsertResultIntoStats(rowIter sql.RowIter, s *stats) error { for { diff --git a/go/libraries/doltcore/sqle/tables.go b/go/libraries/doltcore/sqle/tables.go index f68bb676e2..b4f013baad 100644 --- a/go/libraries/doltcore/sqle/tables.go +++ b/go/libraries/doltcore/sqle/tables.go @@ -30,11 +30,12 @@ import ( // DoltTable implements the sql.Table interface and gives access to dolt table rows and schema. type DoltTable struct { - name string - table *doltdb.Table - sch schema.Schema - db *Database - ed *tableEditor + name string + table *doltdb.Table + sch schema.Schema + sqlSch sql.Schema + db *Database + ed *tableEditor } var _ sql.Table = (*DoltTable)(nil) @@ -78,20 +79,21 @@ func (t *DoltTable) String() string { // Schema returns the schema for this table. func (t *DoltTable) Schema() sql.Schema { - // TODO: fix panics - sch, err := t.table.GetSchema(context.TODO()) - - if err != nil { - panic(err) - } - - // TODO: fix panics - sqlSch, err := doltSchemaToSqlSchema(t.name, sch) + return t.sqlSchema() +} +func (t *DoltTable) sqlSchema() sql.Schema { + if t.sqlSch != nil { + return t.sqlSch + } + + // TODO: fix panics + sqlSch, err := doltSchemaToSqlSchema(t.name, t.sch) if err != nil { panic(err) } + t.sqlSch = sqlSch return sqlSch }