explain.rb 2.8 KB
Newer Older
1
module ActiveRecord
2
  module Explain
3 4 5 6 7
    # If auto explain is enabled, this method triggers EXPLAIN logging for the
    # queries triggered by the block if it takes more than the threshold as a
    # whole. That is, the threshold is not checked against each individual
    # query, but against the duration of the entire block. This approach is
    # convenient for relations.
8 9 10 11 12 13
    #
    # The available_queries_for_explain thread variable collects the queries
    # to be explained. If the value is nil, it means queries are not being
    # currently collected. A false value indicates collecting is turned
    # off. Otherwise it is an array of queries.
    def logging_query_plan # :nodoc:
14
      threshold = auto_explain_threshold_in_seconds
15 16
      current   = Thread.current
      if threshold && current[:available_queries_for_explain].nil?
17
        begin
18
          queries = current[:available_queries_for_explain] = []
19
          start = Time.now
20 21
          result = yield
          logger.warn(exec_explain(queries)) if Time.now - start > threshold
22 23
          result
        ensure
24
          current[:available_queries_for_explain] = nil
25
        end
26 27
      else
        yield
28
      end
29
    end
30

31 32 33 34 35 36 37 38 39 40
    # Relation#explain needs to be able to collect the queries regardless of
    # whether auto explain is enabled. This method serves that purpose.
    def collecting_queries_for_explain # :nodoc:
      current = Thread.current
      original, current[:available_queries_for_explain] = current[:available_queries_for_explain], []
      return yield, current[:available_queries_for_explain]
    ensure
      # Note that the return value above does not depend on this assigment.
      current[:available_queries_for_explain] = original
    end
41

42
    # Makes the adapter execute EXPLAIN for the tuples of queries and bindings.
43
    # Returns a formatted string ready to be logged.
44 45
    def exec_explain(queries) # :nodoc:
      queries && queries.map do |sql, bind|
46 47 48 49 50 51 52 53 54
        [].tap do |msg|
          msg << "EXPLAIN for: #{sql}"
          unless bind.empty?
            bind_msg = bind.map {|col, val| [col.name, val]}.inspect
            msg.last << " #{bind_msg}"
          end
          msg << connection.explain(sql, bind)
        end.join("\n")
      end.join("\n")
55 56
    end

57 58 59 60 61 62 63 64
    # Silences automatic EXPLAIN logging for the duration of the block.
    #
    # This has high priority, no EXPLAINs will be run even if downwards
    # the threshold is set to 0.
    #
    # As the name of the method suggests this only applies to automatic
    # EXPLAINs, manual calls to +ActiveRecord::Relation#explain+ run.
    def silence_auto_explain
65 66
      current = Thread.current
      original, current[:available_queries_for_explain] = current[:available_queries_for_explain], false
67 68
      yield
    ensure
69
      current[:available_queries_for_explain] = original
70 71 72
    end
  end
end