Module: ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements

Defined in:
lib/active_record_json_explain/activerecord/monkey_patches/json_explain.rb

Instance Method Summary collapse

Instance Method Details

#explain(arel, binds = [], json: false) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/active_record_json_explain/activerecord/monkey_patches/json_explain.rb', line 57

def explain(arel, binds = [], json: false) # NOTE: add arg json
  format_option = "FORMAT=JSON" if json # NOTE: get format option
  sql     = "EXPLAIN #{format_option} #{to_sql(arel, binds)}" # NOTE: set format option
  start   = Concurrent.monotonic_time
  result  = exec_query(sql, "EXPLAIN", binds)
  elapsed = Concurrent.monotonic_time - start

  MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
end