Skip to content

Commit

Permalink
#4600 #4587 Fixed very slow loading of data in tables with foreign keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsalawa committed Dec 2, 2022
1 parent 869068b commit 91552b0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 3.4.1
- CHANGE: #4585 SQLite updated to 3.40.0 (except for SQLCipher, which has latest version 3.39.2).
- BUGFIX: #4600 #4587 Fixed very slow loading of data in tables with foreign keys.
- BUGFIX: #4549 Query executor column aliases do not need wrapping anymore, because parser in 3.4.0 does not strip wrapped aliases anymore.
- BUGFIX: #4219 Removed duplicated UI config entries generated by Printing plugin.
- BUGFIX: #4563 Installer now runs the application at final step as a regular user even if running as root.
Expand Down
12 changes: 12 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/fkcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,15 @@ QString FkComboBox::getSql() const

return getSqlForFkEditor(comboModel->getDb(), columnModel, sourceValue);
}

qlonglong FkComboBox::getRowCountForFkEditor(Db* db, const QString& query, bool* isError)
{
static_qstring(tpl, "SELECT count(*) FROM (%1)");

QString sql = tpl.arg(query);
SqlQueryPtr result = db->exec(sql);
if (isError)
*isError = result->isError();

return result->getSingleCell().toLongLong();
}
1 change: 1 addition & 0 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/fkcombobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FkComboBox : public QComboBox
FkComboBox(QWidget *parent, int dropDownViewMinWidth = -1);

static QString getSqlForFkEditor(Db* db, SqlQueryModelColumn* columnModel, const QVariant& currentValue);
static qlonglong getRowCountForFkEditor(Db* db, const QString& query, bool* isError);

static const qlonglong MAX_ROWS_FOR_FK = 10000L;
static const int FK_CELL_LENGTH_LIMIT = 30;
Expand Down
14 changes: 1 addition & 13 deletions SQLiteStudio3/guiSQLiteStudio/datagrid/sqlqueryitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,12 @@ QString SqlQueryItemDelegate::getSqlForFkEditor(SqlQueryItem* item) const
);
}

qlonglong SqlQueryItemDelegate::getRowCountForFkEditor(Db* db, const QString& query, bool* isError) const
{
static_qstring(tpl, "SELECT count(*) FROM (%1)");

QString sql = tpl.arg(query);
SqlQueryPtr result = db->exec(sql);
if (isError)
*isError = result->isError();

return result->getSingleCell().toLongLong();
}

QWidget* SqlQueryItemDelegate::getFkEditor(SqlQueryItem* item, QWidget* parent, const SqlQueryModel* model) const
{
Db* db = model->getDb();
bool countingError = false;
QString sql = FkComboBox::getSqlForFkEditor(db, item->getColumn(), item->getValue());
qlonglong rowCount = getRowCountForFkEditor(db, sql, &countingError);
qlonglong rowCount = FkComboBox::getRowCountForFkEditor(db, sql, &countingError);
if (rowCount > FkComboBox::MAX_ROWS_FOR_FK)
{
notifyWarn(tr("Foreign key for column %2 has more than %1 possible values. It's too much to display in drop down list. You need to edit value manually.")
Expand Down
37 changes: 32 additions & 5 deletions SQLiteStudio3/guiSQLiteStudio/formview.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "formview.h"
#include "common/unused.h"
#include "datagrid/fkcombobox.h"
#include "datagrid/sqlquerymodel.h"
#include "datagrid/sqlqueryview.h"
#include "multieditor/multieditorfk.h"
Expand Down Expand Up @@ -64,15 +65,15 @@ void FormView::setModel(SqlQueryModel* value)

void FormView::load()
{
reloadInternal();
dataMapper->toFirst();
shouldReload = true;
indexForReload = 0;
}

void FormView::reload()
{
int idx = dataMapper->getCurrentIndex();
shouldReload = true;
indexForReload = dataMapper->getCurrentIndex();
reloadInternal();
dataMapper->setCurrentIndex(idx);
}

void FormView::focusFirstEditor()
Expand All @@ -85,6 +86,11 @@ void FormView::focusFirstEditor()

void FormView::reloadInternal()
{
if (!shouldReload)
return;

shouldReload = false;

// Cleanup
dataMapper->clearMapping();
for (QWidget*& widget : widgets)
Expand Down Expand Up @@ -131,7 +137,21 @@ MultiEditor* FormView::addColumn(int colIdx, SqlQueryModelColumn* column)

// MultiEditor editors
if (!column->getFkConstraints().isEmpty())
multiEditor->enableFk(model->getDb(), column);
{
Db* db = model->getDb();
QString sql = FkComboBox::getSqlForFkEditor(db, column, QVariant());
bool countingError = false;
qlonglong rowCount = FkComboBox::getRowCountForFkEditor(db, sql, &countingError);
if (!countingError && rowCount <= FkComboBox::MAX_ROWS_FOR_FK)
multiEditor->enableFk(db, column);
else
{
qDebug() << "FkCombo excluded from FormView for column" << column->column << "due to"
<< (countingError ?
"error with row counting query" :
"too many rows in the FK table: " + QString::number(rowCount));
}
}

multiEditor->setDataType(column->dataType);

Expand Down Expand Up @@ -275,3 +295,10 @@ QToolBar* FormView::getToolBar(int toolbar) const
UNUSED(toolbar);
return nullptr;
}

void FormView::showEvent(QShowEvent* event)
{
UNUSED(event);
reloadInternal();
dataMapper->setCurrentIndex(indexForReload);
}
3 changes: 3 additions & 0 deletions SQLiteStudio3/guiSQLiteStudio/formview.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class GUI_API_EXPORT FormView : public QScrollArea, public ExtActionContainer
void createActions();
void setupDefShortcuts();
QToolBar* getToolBar(int toolbar) const;
void showEvent(QShowEvent* event);

private:
void reloadInternal();
Expand All @@ -83,6 +84,8 @@ class GUI_API_EXPORT FormView : public QScrollArea, public ExtActionContainer
QList<bool> readOnly;
bool valueModified = false;
bool currentIndexUpdating = false;
bool shouldReload = false;
int indexForReload = 0;

private slots:
void dataLoaded(bool successful);
Expand Down

0 comments on commit 91552b0

Please sign in to comment.