From fbe4e60232a56e2eb807384407081404963f092b Mon Sep 17 00:00:00 2001 From: Daenney Date: Sat, 16 Dec 2023 12:54:53 +0100 Subject: [PATCH] [feature] Run ANALYZE after migrations on SQLite (#2428) * [feature] Run ANALYZE after migrations on SQLite This ensures that at the end of migrations, we run ANALYZE if we're using SQLite. This should be relatively quick and guarantees that the table and index statistics have been updated. This helps to ensure the query planner makes better choices when it comes to picking which indexes are used when running queries. * [chore] use ExecContext Uses ExecContext so we pass the context through, this is helpful for anyone running with tracing enabled --- internal/db/bundb/bundb.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 2559b8d58..f7417cfeb 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -44,6 +44,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/tracing" "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/dialect/sqlitedialect" "github.com/uptrace/bun/migrate" @@ -113,6 +114,14 @@ func doMigration(ctx context.Context, db *bun.DB) error { } log.Infof(ctx, "MIGRATED DATABASE TO %s", group) + + if db.Dialect().Name() == dialect.SQLite { + log.Info(ctx, "running ANALYZE to update table and index statistics") + _, err := db.ExecContext(ctx, "ANALYZE") + if err != nil { + log.Warnf(ctx, "ANALYZE failed, query planner may make poor life choices: %s", err) + } + } return nil }