Avoid sorting twice in Browse Data tab

When browsing a table and changing the sort order, then switching to
another table, and then switching back, we would sort the table twice:
once using the default sort order and then again using the previously
used sort order. This results in four instead of two queries for those
tables (including the COUNT queries). This commit fixes this so that
only two queries are executed which should cut the run time for these
cases in half.

See issue #1007.
This commit is contained in:
Martin Kleusberg
2017-05-04 19:54:03 +02:00
parent a436901d12
commit 9a171e0621
3 changed files with 18 additions and 7 deletions

View File

@@ -446,7 +446,7 @@ void MainWindow::populateTable()
// No stored settings found.
// Set table name and apply default display format settings
m_browseTableModel->setTable(tablename);
m_browseTableModel->setTable(tablename, 0, Qt::AscendingOrder);
// There aren't any information stored for this table yet, so use some default values
@@ -458,7 +458,6 @@ void MainWindow::populateTable()
ui->dataTable->setColumnWidth(i, ui->dataTable->horizontalHeader()->defaultSectionSize());
// Sorting
m_browseTableModel->sort(0, Qt::AscendingOrder);
ui->dataTable->filterHeader()->setSortIndicator(0, Qt::AscendingOrder);
// Encoding
@@ -488,9 +487,9 @@ void MainWindow::populateTable()
}
}
if(only_defaults)
m_browseTableModel->setTable(tablename);
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode);
else
m_browseTableModel->setTable(tablename, v);
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, v);
// There is information stored for this table, so extract it and apply it
@@ -503,7 +502,6 @@ void MainWindow::populateTable()
ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value());
// Sorting
m_browseTableModel->sort(storedData.sortOrderIndex, storedData.sortOrderMode);
ui->dataTable->filterHeader()->setSortIndicator(storedData.sortOrderIndex, storedData.sortOrderMode);
// Filters

View File

@@ -25,6 +25,7 @@ SqliteTableModel::SqliteTableModel(DBBrowserDB& db, QObject* parent, size_t chun
void SqliteTableModel::reset()
{
m_sTable.clear();
m_iSortColumn = 0;
m_sSortOrder = "ASC";
m_headers.clear();
@@ -38,15 +39,23 @@ void SqliteTableModel::setChunkSize(size_t chunksize)
m_chunkSize = chunksize;
}
void SqliteTableModel::setTable(const QString& table, const QVector<QString>& display_format)
void SqliteTableModel::setTable(const QString& table, int sortColumn, Qt::SortOrder sortOrder, const QVector<QString>& display_format)
{
// Unset all previous settings. When setting a table all information on the previously browsed data set is removed first.
reset();
// Set sort parameters. This saves the sort parameters in the class instance but doesn't execute any query yet because the table name
// isn't set yet. This is why this needs to be called before setting the table name
sort(sortColumn, sortOrder);
// Save the other parameters
m_sTable = table;
m_vDisplayFormat = display_format;
// The first column is the rowid column and therefore is always of type integer
m_vDataTypes.push_back(SQLITE_INTEGER);
// Get the data types of all other columns as well as the column names
bool allOk = false;
if(m_db.getObjectByName(table)->type() == sqlb::Object::Types::Table)
{
@@ -73,6 +82,9 @@ void SqliteTableModel::setTable(const QString& table, const QVector<QString>& di
}
}
// If for one reason or another (either it's a view or we couldn't parse the table statement) we couldn't get the field
// information we retrieve it from SQLite using an extra query.
// NOTE: It would be nice to eventually get rid of this piece here. As soon as the grammar parser is good enough...
if(!allOk)
{
QString sColumnQuery = QString::fromUtf8("SELECT * FROM %1;").arg(sqlb::escapeIdentifier(table));
@@ -80,6 +92,7 @@ void SqliteTableModel::setTable(const QString& table, const QVector<QString>& di
m_headers.append(getColumns(sColumnQuery, m_vDataTypes));
}
// Prepare actual query and fetch data
buildQuery();
}

View File

@@ -38,7 +38,7 @@ public:
void setQuery(const QString& sQuery, bool dontClearHeaders = false);
QString query() const { return m_sQuery; }
void setTable(const QString& table, const QVector<QString> &display_format = QVector<QString>());
void setTable(const QString& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QVector<QString> &display_format = QVector<QString>());
void setChunkSize(size_t chunksize);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);