Optimise SQL database store and IO

* Remove unnecessary ROWID from tables.
* Set JOURNAL_MODE to MEMORY, reducing disk IO.
* VACUUM databases after install/compile.
This commit is contained in:
Unnoen 2021-01-07 02:47:59 +11:00
parent d81b9026d4
commit 5d9677dbd1
No known key found for this signature in database
GPG Key ID: 8F8E42252BA20553
4 changed files with 41 additions and 7 deletions

View File

@ -127,7 +127,7 @@ namespace Wabbajack.Common
static HashCache()
{
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100;");
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100; Journal Mode=Memory;");
_conn = new SQLiteConnection(_connectionString);
_conn.Open();
@ -135,7 +135,8 @@ namespace Wabbajack.Common
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS HashCache (
Path TEXT PRIMARY KEY,
LastModified BIGINT,
Hash BIGINT)";
Hash BIGINT)
WITHOUT ROWID";
cmd.ExecuteNonQuery();
@ -182,6 +183,15 @@ namespace Wabbajack.Common
cmd.ExecuteNonQuery();
}
public static void VacuumDatabase()
{
using var cmd = new SQLiteCommand(_conn);
cmd.CommandText = @"VACUUM";
cmd.PrepareAsync();
cmd.ExecuteNonQuery();
}
public static Hash ReadHash(this BinaryReader br)
{
return new(br.ReadUInt64());

View File

@ -15,7 +15,7 @@ namespace Wabbajack.Common
static PatchCache()
{
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100;");
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100; Journal Mode=Memory;");
_conn = new SQLiteConnection(_connectionString);
_conn.Open();
@ -25,7 +25,8 @@ namespace Wabbajack.Common
ToHash BIGINT,
PatchSize BLOB,
Patch BLOB,
PRIMARY KEY (FromHash, ToHash))";
PRIMARY KEY (FromHash, ToHash))
WITHOUT ROWID";
cmd.ExecuteNonQuery();
}
@ -142,6 +143,15 @@ namespace Wabbajack.Common
}
public static void VacuumDatabase()
{
using var cmd = new SQLiteCommand(_conn);
cmd.CommandText = @"VACUUM";
cmd.PrepareAsync();
cmd.ExecuteNonQuery();
}
public static void ApplyPatch(Stream input, Func<Stream> openPatchStream, Stream output)

View File

@ -177,6 +177,11 @@ namespace Wabbajack.Lib
}
finally
{
Utils.Log("Vacuuming databases");
HashCache.VacuumDatabase();
PatchCache.VacuumDatabase();
VirtualFile.VacuumDatabase();
Utils.Log("Vacuuming completed");
_isRunning.OnNext(false);
}
});

View File

@ -22,14 +22,15 @@ namespace Wabbajack.VirtualFileSystem
static VirtualFile()
{
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100;");
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100; Journal Mode=Memory;");
_conn = new SQLiteConnection(_connectionString);
_conn.Open();
using var cmd = new SQLiteCommand(_conn);
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS VFSCache (
Hash BIGINT PRIMARY KEY ,
Contents BLOB)";
Hash BIGINT PRIMARY KEY,
Contents BLOB)
WITHOUT ROWID";
cmd.ExecuteNonQuery();
}
@ -273,6 +274,14 @@ namespace Wabbajack.VirtualFileSystem
throw;
}
}
public static void VacuumDatabase()
{
using var cmd = new SQLiteCommand(_conn);
cmd.CommandText = @"VACUUM";
cmd.PrepareAsync();
cmd.ExecuteNonQuery();
}
internal void FillFullPath()
{