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 2 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 analyzeAndOptimizeDatabase(const QSqlDatabase& database) {
Holzhaus marked this conversation as resolved.
Show resolved Hide resolved
kLogger.info()
<< "Analyzing and optimizing 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 analyze and optimize database";
}
else {
kLogger.info()
<< "Finished database analysis and optimization:"
<< 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);
analyzeAndOptimizeDatabase(dbConnection);
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the rational for Analyze before Vacuum? I would swap it intuitively.
Can you add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know which order is correct and why to choose one over the other:

https://stackoverflow.com/questions/58178062/in-what-order-should-i-call-analyze-and-vacuum-in-an-sqlite3-database

Copy link
Contributor Author

@uklotzde uklotzde Aug 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it:

https://sqlite.org/forum/forumpost/62fb63a29c5f7810?t=h

Richard Hipp himself recommends to run VACCUM before ANALYZE.


PRAGMA optimize;
Holzhaus marked this conversation as resolved.
Show resolved Hide resolved
-- Rebuild the entire database file
-- https://www.sqlite.org/lang_vacuum.html
VACUUM;