Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update query planner statistics of database after library scan finished #4199

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/library/scanner/libraryscanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mixxx::Logger kLogger("LibraryScanner");
QAtomicInt s_instanceCounter(0);

// Returns the number of affected rows or -1 on error
int execCleanupQuery(FwdSqlQuery& query) {
int execRowCountQuery(FwdSqlQuery& query) {
VERIFY_OR_DEBUG_ASSERT(query.isPrepared()) {
return -1;
}
Expand All @@ -52,11 +52,12 @@ void cleanUpDatabase(const QSqlDatabase& database) {
query.bindValue(
QStringLiteral(":unequalHash"),
static_cast<mixxx::cache_key_signed_t>(mixxx::invalidCacheKey()));
auto numRows = execCleanupQuery(query);
if (numRows < 0) {
const auto numRows = execRowCountQuery(query);
VERIFY_OR_DEBUG_ASSERT(numRows >= 0) {
kLogger.warning()
<< "Failed to delete orphaned directory hashes";
} else if (numRows > 0) {
}
else if (numRows > 0) {
kLogger.info()
<< "Deleted" << numRows << "orphaned directory hashes";
}
Expand All @@ -65,6 +66,27 @@ void cleanUpDatabase(const QSqlDatabase& database) {
<< timer.elapsed().debugMillisWithUnit();
}

/// Update statistics for the query planner
/// See also: https://www.sqlite.org/lang_analyze.html
void updateQueryPlannerStatisticsForDatabase(const QSqlDatabase& database) {
kLogger.info()
<< "Updating query planner statistics for database...";
PerformanceTimer timer;
timer.start();
const auto sqlStmt = QStringLiteral("ANALYZE");
FwdSqlQuery query(database, sqlStmt);
const auto numRows = execRowCountQuery(query);
VERIFY_OR_DEBUG_ASSERT(numRows >= 0) {
kLogger.warning()
<< "Failed to update query planner statistics for database";
}
else {
kLogger.info()
<< "Finished updating query planner statistics for database:"
<< timer.elapsed().debugMillisWithUnit();
}
}

} // anonymous namespace

LibraryScanner::LibraryScanner(
Expand Down Expand Up @@ -384,6 +406,11 @@ void LibraryScanner::slotFinishUnhashedScan() {
cleanUpScan();
}

if (!m_scannerGlobal->shouldCancel() && bScanFinishedCleanly) {
const auto dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool);
updateQueryPlannerStatisticsForDatabase(dbConnection);
}

if (!m_scannerGlobal->shouldCancel() && bScanFinishedCleanly) {
kLogger.debug() << "Scan finished cleanly";
} else {
Expand Down
9 changes: 8 additions & 1 deletion tools/mixxxdb_cleanup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ DELETE FROM track_analysis WHERE track_id NOT IN (SELECT id FROM track_locations
-- Post-cleanup maintenance --
-----------------------------------------------------------------------

-- Rebuild the entire database file
-- https://www.sqlite.org/lang_vacuum.html
VACUUM;

PRAGMA optimize;
Holzhaus marked this conversation as resolved.
Show resolved Hide resolved
-- According to Richard Hipp himself executing VACUUM before ANALYZE is the
-- recommended order: https://sqlite.org/forum/forumpost/62fb63a29c5f7810?t=h

-- Update statistics for the query planner
-- https://www.sqlite.org/lang_analyze.html
ANALYZE;