2018-11-20 02:01:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-04 18:13:19 +00:00
|
|
|
module Gitlab
|
|
|
|
module Sherlock
|
|
|
|
class Query
|
|
|
|
attr_reader :id, :query, :started_at, :finished_at, :backtrace
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# SQL identifiers that should be prefixed with newlines.
|
2018-01-27 05:35:53 +00:00
|
|
|
PREFIX_NEWLINE = %r{
|
2015-11-04 18:13:19 +00:00
|
|
|
\s+(FROM
|
|
|
|
|(LEFT|RIGHT)?INNER\s+JOIN
|
|
|
|
|(LEFT|RIGHT)?OUTER\s+JOIN
|
|
|
|
|WHERE
|
|
|
|
|AND
|
|
|
|
|GROUP\s+BY
|
|
|
|
|ORDER\s+BY
|
|
|
|
|LIMIT
|
2019-05-05 10:19:14 +00:00
|
|
|
|OFFSET)\s+}ix.freeze # Vim indent breaks when this is on a newline :<
|
2015-11-05 17:01:05 +00:00
|
|
|
|
|
|
|
# Creates a new Query using a String and a separate Array of bindings.
|
|
|
|
#
|
|
|
|
# query - A String containing a SQL query, optionally with numeric
|
|
|
|
# placeholders (`$1`, `$2`, etc).
|
|
|
|
#
|
|
|
|
# bindings - An Array of ActiveRecord columns and their values.
|
|
|
|
# started_at - The start time of the query as a Time-like object.
|
|
|
|
# finished_at - The completion time of the query as a Time-like object.
|
|
|
|
#
|
|
|
|
# Returns a new Query object.
|
2015-11-04 18:13:19 +00:00
|
|
|
def self.new_with_bindings(query, bindings, started_at, finished_at)
|
2015-11-05 17:01:05 +00:00
|
|
|
bindings.each_with_index do |(_, value), index|
|
2015-11-04 18:13:19 +00:00
|
|
|
quoted_value = ActiveRecord::Base.connection.quote(value)
|
|
|
|
|
|
|
|
query = query.gsub("$#{index + 1}", quoted_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
new(query, started_at, finished_at)
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# query - The SQL query as a String (without placeholders).
|
|
|
|
# started_at - The start time of the query as a Time-like object.
|
|
|
|
# finished_at - The completion time of the query as a Time-like object.
|
2015-11-04 18:13:19 +00:00
|
|
|
def initialize(query, started_at, finished_at)
|
|
|
|
@id = SecureRandom.uuid
|
|
|
|
@query = query
|
|
|
|
@started_at = started_at
|
|
|
|
@finished_at = finished_at
|
|
|
|
@backtrace = caller_locations.map do |loc|
|
|
|
|
Location.from_ruby_location(loc)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @query.end_with?(';')
|
2018-11-20 02:01:13 +00:00
|
|
|
@query = "#{@query};"
|
2015-11-04 18:13:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# Returns the query duration in milliseconds.
|
2015-11-04 18:13:19 +00:00
|
|
|
def duration
|
|
|
|
@duration ||= (@finished_at - @started_at) * 1000.0
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
@id
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# Returns a human readable version of the query.
|
2015-11-04 18:13:19 +00:00
|
|
|
def formatted_query
|
|
|
|
@formatted_query ||= format_sql(@query)
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# Returns the last application frame of the backtrace.
|
2015-11-04 18:13:19 +00:00
|
|
|
def last_application_frame
|
|
|
|
@last_application_frame ||= @backtrace.find(&:application?)
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# Returns an Array of application frames (excluding Gems and the likes).
|
2015-11-04 18:13:19 +00:00
|
|
|
def application_backtrace
|
|
|
|
@application_backtrace ||= @backtrace.select(&:application?)
|
|
|
|
end
|
|
|
|
|
2015-11-05 17:01:05 +00:00
|
|
|
# Returns the query plan as a String.
|
2015-11-04 18:13:19 +00:00
|
|
|
def explain
|
|
|
|
unless @explain
|
|
|
|
ActiveRecord::Base.connection.transaction do
|
|
|
|
@explain = raw_explain(@query).values.flatten.join("\n")
|
|
|
|
|
|
|
|
# Roll back any queries that mutate data so we don't mess up
|
|
|
|
# anything when running explain on an INSERT, UPDATE, DELETE, etc.
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@explain
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def raw_explain(query)
|
2019-07-24 13:59:55 +00:00
|
|
|
explain = "EXPLAIN ANALYZE #{query};"
|
2015-11-04 18:13:19 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.connection.execute(explain)
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_sql(query)
|
2017-06-21 13:48:12 +00:00
|
|
|
query.each_line
|
|
|
|
.map { |line| line.strip }
|
|
|
|
.join("\n")
|
|
|
|
.gsub(PREFIX_NEWLINE) { "\n#{$1} " }
|
2015-11-04 18:13:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|