From 4cbc38430918e9ed995fe2fbed41ff475d87683a Mon Sep 17 00:00:00 2001 From: Chris Gwilliams <517923+encima@users.noreply.github.com> Date: Wed, 10 Apr 2024 10:44:14 +0300 Subject: [PATCH] add explanation for cache hit and if action is needed --- internal/inspect/cache/cache.go | 18 ++++++++++++++++-- .../long_running_queries.go | 2 +- internal/inspect/queries.go | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/inspect/cache/cache.go b/internal/inspect/cache/cache.go index 416c9b049..875bdd151 100644 --- a/internal/inspect/cache/cache.go +++ b/internal/inspect/cache/cache.go @@ -33,9 +33,23 @@ func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...fu return err } // TODO: implement a markdown table marshaller - table := "|Name|Ratio|\n|-|-|\n" + table := "|Name|Ratio|OK?|Explanation|\n|-|-|-|-|\n" for _, r := range result { - table += fmt.Sprintf("|`%s`|`%.6f`|\n", r.Name, r.Ratio) + + ok := "Yup!" + if r.Ratio < 0.94 { + ok = "Maybe not..." + } + var explanation string + if r.Name == "index hit rate" { + explanation = "This is the ratio of index hits to index scans. If this ratio is low, it means that the database is not using indexes effectively. Check the `index-usage` command for more info." + } else if r.Name == "table hit rate" { + explanation = "This is the ratio of table hits to table scans. If this ratio is low, it means that your queries are not finding the data effectively. Check your query performance and it might be worth increasing your compute." + + } + + table += fmt.Sprintf("|`%s`|`%.6f`|`%s`|`%s`|\n", r.Name, r.Ratio, ok, explanation) + } return list.RenderTable(table) } diff --git a/internal/inspect/long_running_queries/long_running_queries.go b/internal/inspect/long_running_queries/long_running_queries.go index 53b97d558..fc1a56f90 100644 --- a/internal/inspect/long_running_queries/long_running_queries.go +++ b/internal/inspect/long_running_queries/long_running_queries.go @@ -25,7 +25,7 @@ func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...fu if err != nil { return err } - rows, err := conn.Query(ctx, inspect.LONG_RUNNIN_QUERY) + rows, err := conn.Query(ctx, inspect.LONG_RUNNING_QUERY) if err != nil { return errors.Errorf("failed to query rows: %w", err) } diff --git a/internal/inspect/queries.go b/internal/inspect/queries.go index c3ded66dd..f4e947329 100644 --- a/internal/inspect/queries.go +++ b/internal/inspect/queries.go @@ -142,7 +142,7 @@ AND pg_locks.pid=pg_stat_activity.pid AND pg_locks.mode = 'ExclusiveLock' ORDER BY query_start` -const LONG_RUNNIN_QUERY = `SELECT +const LONG_RUNNING_QUERY = `SELECT pid, now() - pg_stat_activity.query_start AS duration, query AS query