EXPLAIN ANALYZE with ActiveRecord
ActiveRecord example:
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module DatabaseStatements
def explain_analyze(arel, binds = [])
sql = "EXPLAIN ANALYZE #{to_sql(arel, binds)}"
PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", binds))
end
end
end
end
end
puts Job.where("slow = true").explain_analyze
...
Raw SQL example:
puts ActiveRecord::Base.connection.select_all(sql).to_a.map { |x| x.fetch("QUERY PLAN") }.join("\n")