Skip to content

Commit 21c7f76

Browse files
committed
Speed up execution of many SQL statements by delaying the log output
When executing a large amount of SQL statements a significant amount of time is spent on logging the executed statements and their results. This time can be reduced by caching the log text and only updating the log view a single time when everything is done. See issue #3237.
1 parent a4a1969 commit 21c7f76

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/MainWindow.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,8 @@ void MainWindow::executeQuery()
11941194
}
11951195

11961196
// Prepare a lambda function for logging the results of a query
1197-
auto query_logger = [this, sqlWidget, editor](bool ok, const QString& status_message, int from_position, int to_position) {
1197+
auto logged_queries = std::make_shared<QString>();
1198+
auto query_logger = [sqlWidget, editor, logged_queries](bool ok, const QString& status_message, int from_position, int to_position) {
11981199
int execute_from_line, execute_from_index;
11991200
editor->lineIndexFromPosition(from_position, &execute_from_line, &execute_from_index);
12001201

@@ -1224,10 +1225,10 @@ void MainWindow::executeQuery()
12241225
// The query takes the last placeholder as it may itself contain the sequence '%' + number.
12251226
QString query = editor->text(from_position, to_position);
12261227
QString log_message = "-- " + tr("At line %1:").arg(execute_from_line+1) + "\n" + query.trimmed() + "\n-- " + tr("Result: %1").arg(status_message);
1227-
db.logSQL(log_message, kLogMsg_User);
1228+
logged_queries->append(log_message + "\n");
12281229

1229-
log_message = tr("Result: %2").arg(status_message) + "\n" + tr("At line %1:").arg(execute_from_line+1) + "\n" + query.trimmed();
12301230
// Update the execution area
1231+
log_message = tr("Result: %2").arg(status_message) + "\n" + tr("At line %1:").arg(execute_from_line+1) + "\n" + query.trimmed();
12311232
sqlWidget->finishExecution(log_message, ok);
12321233
};
12331234

@@ -1296,7 +1297,10 @@ void MainWindow::executeQuery()
12961297
execute_sql_worker->stop();
12971298

12981299
}, Qt::BlockingQueuedConnection);
1299-
connect(execute_sql_worker.get(), &RunSql::finished, sqlWidget, [this, current_tab, sqlWidget]() {
1300+
connect(execute_sql_worker.get(), &RunSql::finished, sqlWidget, [this, current_tab, sqlWidget, logged_queries]() {
1301+
// Update the SQL log
1302+
db.logSQL(*logged_queries, kLogMsg_User);
1303+
13001304
// We work with a pointer to the current tab here instead of its index because the user might reorder the tabs in the meantime.
13011305
// We set different icons for general tabs, which are either new or loaded from the project file, and for tabs loaded from a file.
13021306
if(sqlWidget->fileName().isEmpty())

0 commit comments

Comments
 (0)