Class: SqlGenius::Core::Analysis::QueryStats

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_genius/core/analysis/query_stats.rb

Overview

Top statements by a given sort dimension, sourced from MySQL’s performance_schema.events_statements_summary_by_digest or PostgreSQL’s pg_stat_statements (whichever the connected server provides). Returns an array of per-digest hashes with call counts, timing percentiles, row examine/sent ratios, and temp-table metadata.

If the underlying stats source is not enabled, the SQL exec will raise — the caller decides how to render that.

Constant Summary collapse

VALID_SORTS =
["total_time", "avg_time", "calls", "rows_examined"].freeze
MAX_LIMIT =
50

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ QueryStats

Returns a new instance of QueryStats.



18
19
20
21
# File 'lib/sql_genius/core/analysis/query_stats.rb', line 18

def initialize(connection)
  @connection = connection
  @builder = QueryBuilders.for(connection)
end

Instance Method Details

#call(sort: "total_time", limit: MAX_LIMIT) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sql_genius/core/analysis/query_stats.rb', line 23

def call(sort: "total_time", limit: MAX_LIMIT)
  order_clause = @builder.query_stats_order_clause(sort)
  effective_limit = limit.to_i.clamp(1, MAX_LIMIT)

  sql = @builder.query_stats(
    @connection,
    order_clause: order_clause,
    limit: effective_limit,
    include_digest: digest_column_available?,
  )
  result = @connection.exec_query(sql)
  result.to_hashes.map { |row| transform(row) }
end