Skip to content

Commit

Permalink
Merge pull request #4199 from uklotzde/analyze-and-optimize-database
Browse files Browse the repository at this point in the history
Update query planner statistics of database after library scan finished
  • Loading branch information
Holzhaus authored Aug 16, 2021
2 parents 53572a0 + 7f16142 commit 86613c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
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;
-- 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;

0 comments on commit 86613c3

Please sign in to comment.