Class: SqlGenius::Core::Analysis::QueryHistory
- Inherits:
-
Object
- Object
- SqlGenius::Core::Analysis::QueryHistory
- Defined in:
- lib/sql_genius/core/analysis/query_history.rb
Overview
Fetches a single query’s aggregated stats by its digest/queryid for the query detail page. Returns nil if the digest is not found in the statement-stats source for the current database.
On MySQL/MariaDB this reads performance_schema.events_statements_summary_by_digest; on PostgreSQL it reads pg_stat_statements joined with pg_database.
Instance Method Summary collapse
- #call(digest) ⇒ Object
- #digest_text_for(digest) ⇒ Object
-
#initialize(connection) ⇒ QueryHistory
constructor
A new instance of QueryHistory.
Constructor Details
#initialize(connection) ⇒ QueryHistory
Returns a new instance of QueryHistory.
13 14 15 16 |
# File 'lib/sql_genius/core/analysis/query_history.rb', line 13 def initialize(connection) @connection = connection @builder = QueryBuilders.for(connection) end |
Instance Method Details
#call(digest) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sql_genius/core/analysis/query_history.rb', line 18 def call(digest) digest_str = digest.to_s return if digest_str.empty? sql = @builder.query_history(@connection, digest: digest_str) result = @connection.exec_query(sql) row = result.to_hashes.first return unless row { sql: row["DIGEST_TEXT"] || row["digest_text"], calls: (row["calls"] || row["CALLS"] || 0).to_i, total_time_ms: (row["total_time_ms"] || 0).to_f, avg_time_ms: (row["avg_time_ms"] || 0).to_f, max_time_ms: (row["max_time_ms"] || 0).to_f, rows_examined: (row["rows_examined"] || row["ROWS_EXAMINED"] || 0).to_i, rows_sent: (row["rows_sent"] || row["ROWS_SENT"] || 0).to_i, first_seen: (row["FIRST_SEEN"] || row["first_seen"]).to_s, last_seen: (row["LAST_SEEN"] || row["last_seen"]).to_s, } end |
#digest_text_for(digest) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/sql_genius/core/analysis/query_history.rb', line 40 def digest_text_for(digest) digest_str = digest.to_s return if digest_str.empty? sql = @builder.digest_text_lookup(@connection, digest: digest_str) @connection.select_value(sql) end |