Add a couple of indexes to the database to speed up searches on large libraries.

This commit is contained in:
David Sansome 2010-03-21 21:17:01 +00:00
parent eea777e74e
commit b5b61a2e0e
5 changed files with 24 additions and 11 deletions

View File

@ -71,5 +71,6 @@
<file>media-playlist-repeat-off.png</file>
<file>media-playlist-shuffle-off.png</file>
<file>schema-4.sql</file>
<file>schema-5.sql</file>
</qresource>
</RCC>

8
data/schema-5.sql Normal file
View File

@ -0,0 +1,8 @@
ALTER TABLE songs ADD COLUMN effective_compilation NOT NULL DEFAULT 0;
UPDATE songs SET effective_compilation = ((compilation OR sampler OR forced_compilation_on) AND NOT forced_compilation_off) + 0;
CREATE INDEX idx_comp_artist ON songs (effective_compilation, artist);
UPDATE schema_version SET version=5;

View File

@ -16,7 +16,7 @@
const char* LibraryBackend::kDatabaseName = "clementine.db";
const int LibraryBackend::kSchemaVersion = 4;
const int LibraryBackend::kSchemaVersion = 5;
void (*LibraryBackend::_sqlite3_create_function) (
sqlite3*, const char*, int, int, void*,
@ -535,7 +535,10 @@ void LibraryBackend::UpdateCompilations() {
}
// Now mark the songs that we think are in compilations
QSqlQuery update("UPDATE songs SET sampler = :sampler WHERE album = :album", db);
QSqlQuery update("UPDATE songs"
" SET sampler = :sampler,"
" effective_compilation = ((compilation OR :sampler OR forced_compilation_on) AND NOT forced_compilation_off) + 0"
" WHERE album = :album", db);
QSqlQuery find_songs("SELECT ROWID, " + QString(Song::kColumnSpec) + " FROM songs"
" WHERE album = :album AND sampler = :sampler", db);
@ -629,6 +632,7 @@ LibraryBackend::AlbumList LibraryBackend::GetAlbums(const QString& artist,
last_album = info.album_name;
last_artist = info.artist;
}
return ret;
}

View File

@ -41,12 +41,7 @@ void LibraryQuery::AddWhere(const QString& column, const QVariant& value) {
}
void LibraryQuery::AddCompilationRequirement(bool compilation) {
if (compilation)
where_clauses_ << "((compilation = 1 OR sampler = 1 OR forced_compilation_on = 1)"
" AND forced_compilation_off = 0)";
else
where_clauses_ << "((compilation = 0 AND sampler = 0 AND forced_compilation_on = 0)"
" OR forced_compilation_off = 1)";
where_clauses_ << QString("effective_compilation = %1").arg(compilation ? 1 : 0);
}
QSqlQuery LibraryQuery::Query(QSqlDatabase db) const {

View File

@ -40,7 +40,7 @@ const char* Song::kColumnSpec =
"length, bitrate, samplerate, directory, filename, "
"mtime, ctime, filesize, sampler, art_automatic, art_manual, "
"filetype, playcount, lastplayed, rating, forced_compilation_on, "
"forced_compilation_off";
"forced_compilation_off, effective_compilation";
const char* Song::kBindSpec =
":title, :album, :artist, :albumartist, :composer, "
@ -48,7 +48,7 @@ const char* Song::kBindSpec =
":length, :bitrate, :samplerate, :directory_id, :filename, "
":mtime, :ctime, :filesize, :sampler, :art_automatic, :art_manual, "
":filetype, :playcount, :lastplayed, :rating, :forced_compilation_on, "
":forced_compilation_off";
":forced_compilation_off, :effective_compilation";
const char* Song::kUpdateSpec =
"title = :title, album = :album, artist = :artist, "
@ -61,7 +61,8 @@ const char* Song::kUpdateSpec =
"art_automatic = :art_automatic, art_manual = :art_manual, "
"filetype = :filetype, playcount = :playcount, lastplayed = :lastplayed, "
"rating = :rating, forced_compilation_on = :forced_compilation_on, "
"forced_compilation_off = :forced_compilation_off";
"forced_compilation_off = :forced_compilation_off, "
"effective_compilation = :effective_compilation";
TagLibFileRefFactory Song::kDefaultFactory;
@ -302,6 +303,8 @@ void Song::InitFromQuery(const QSqlQuery& q) {
d->forced_compilation_on_ = q.value(28).toBool();
d->forced_compilation_off_ = q.value(29).toBool();
// effective_compilation = 30
#undef tostr
#undef toint
#undef tofloat
@ -373,6 +376,8 @@ void Song::BindToQuery(QSqlQuery *query) const {
query->bindValue(":forced_compilation_on", d->forced_compilation_on_ ? 1 : 0);
query->bindValue(":forced_compilation_off", d->forced_compilation_off_ ? 1 : 0);
query->bindValue(":effective_compilation", is_compilation() ? 1 : 0);
#undef intval
}