Path: redb/redb.Tests.Integration/Tests/Sqlite/SqlitePlannerStatisticsTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Sqlite/SqlitePlannerStatisticsTests.cs ---- using AwesomeAssertions; using Microsoft.Data.Sqlite; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Core; using redb.Core.Extensions; using redb.Core.Models.Configuration; using redb.Core.Models.Entities; using redb.SQLite.Data; using redb.SQLite.Extensions; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Models; using Xunit; namespace redb.Tests.Integration.Tests.Sqlite; /// /// Planner statistics of a SQLite file (KeepSqlitePlannerStatistics): a file an application filled has no sqlite_stat1, /// and SQLite then costs every index lookup at a few rows. Start-up analyzes a file with no statistics once it holds /// 10 000 value rows, and leaves a smaller one alone. /// public class SqlitePlannerStatisticsTests { [Fact] public async Task Start_AnalyzesAFileAboveTheThreshold_AndLeavesASmallerOneAlone() { var dbPath = Path.Combine(Path.GetTempPath(), $"redb_stats_{Guid.NewGuid():N}.db"); try { // A new file: below the threshold, no statistics after the start. await WithServiceAsync(dbPath, keep: true, async redb => { await redb.SyncSchemeAsync(); await redb.SaveAsync(new RedbObject { name = "stats-seed", Props = new EmployeeProps { FirstName = "S", Skills = Enumerable.Range(0, 12_000).Select(i => $"skill-{i}").ToArray() } }); }); StatisticsRows(dbPath).Should().Be(0, "the first start met an empty file, below the threshold"); // The next start meets 12 000 element rows and no statistics: it analyzes. await WithServiceAsync(dbPath, keep: true, _ => Task.CompletedTask); StatisticsRows(dbPath).Should().BeGreaterThan(0, "a file above the threshold without statistics is analyzed at start-up"); } finally { DeleteFile(dbPath); } } [Fact] public async Task Start_WithTheSwitchOff_GathersNothing() { var dbPath = Path.Combine(Path.GetTempPath(), $"redb_stats_off_{Guid.NewGuid():N}.db"); try { await WithServiceAsync(dbPath, keep: false, async redb => { await redb.SyncSchemeAsync(); await redb.SaveAsync(new RedbObject { name = "stats-off-seed", Props = new EmployeeProps { FirstName = "S", Skills = Enumerable.Range(0, 12_000).Select(i => $"skill-{i}").ToArray() } }); }); await WithServiceAsync(dbPath, keep: false, _ => Task.CompletedTask); StatisticsRows(dbPath).Should().Be(0, "KeepSqlitePlannerStatistics = false leaves the file as it is"); } finally { DeleteFile(dbPath); } } private static async Task WithServiceAsync(string dbPath, bool keep, Func body) { SqliteDataSource.NativeExtensionPath ??= SqliteTestSupport.ResolveNativeExtension(); var services = new ServiceCollection(); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning)); services.AddRedb(o => o .UseSqlite($"Data Source={dbPath}") .Configure(c => { c.EnablePropsCache = false; c.KeepSqlitePlannerStatistics = keep; })); var sp = services.BuildServiceProvider(); try { var redb = sp.GetRequiredService(); await redb.InitializeAsync(ensureCreated: true); await body(redb); } finally { await sp.DisposeAsync(); } } private static long StatisticsRows(string dbPath) { using var connection = new SqliteConnection($"Data Source={dbPath};Mode=ReadOnly;Pooling=False"); connection.Open(); using var exists = connection.CreateCommand(); exists.CommandText = "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'sqlite_stat1'"; if ((long)exists.ExecuteScalar()! == 0) return 0; using var rows = connection.CreateCommand(); rows.CommandText = "SELECT count(*) FROM sqlite_stat1 WHERE tbl IN ('_objects', '_values')"; return (long)rows.ExecuteScalar()!; } private static void DeleteFile(string dbPath) { // Best effort: in a full run another pool of the process may still hold the file for a moment; a temp file left // behind is not what these tests check. SqliteConnection.ClearAllPools(); foreach (var path in new[] { dbPath, dbPath + "-wal", dbPath + "-shm" }) { try { if (File.Exists(path)) File.Delete(path); } catch (IOException) { } } } }