Skip to content
This repository
Browse code

Don't run explain on slow queries for database adapters that don't su…

…pport it
  • Loading branch information...
commit 50e86135287a9e573d6f7afa30550f9b3ab90137 1 parent 3ec4430
Blake Smith blakesmith authored
13 activerecord/lib/active_record/explain.rb
@@ -6,11 +6,12 @@ def self.extended(base)
6 6 base.mattr_accessor :auto_explain_threshold_in_seconds, instance_accessor: false
7 7 end
8 8
9   - # If auto explain is enabled, this method triggers EXPLAIN logging for the
10   - # queries triggered by the block if it takes more than the threshold as a
11   - # whole. That is, the threshold is not checked against each individual
12   - # query, but against the duration of the entire block. This approach is
13   - # convenient for relations.
  9 + # If the database adapter supports explain and auto explain is enabled,
  10 + # this method triggers EXPLAIN logging for the queries triggered by the
  11 + # block if it takes more than the threshold as a whole. That is, the
  12 + # threshold is not checked against each individual query, but against the
  13 + # duration of the entire block. This approach is convenient for relations.
  14 +
14 15 #
15 16 # The available_queries_for_explain thread variable collects the queries
16 17 # to be explained. If the value is nil, it means queries are not being
@@ -21,7 +22,7 @@ def logging_query_plan # :nodoc:
21 22
22 23 threshold = auto_explain_threshold_in_seconds
23 24 current = Thread.current
24   - if threshold && current[:available_queries_for_explain].nil?
  25 + if connection.supports_explain? && threshold && current[:available_queries_for_explain].nil?
25 26 begin
26 27 queries = current[:available_queries_for_explain] = []
27 28 start = Time.now
7 activerecord/lib/active_record/railtie.rb
@@ -136,6 +136,13 @@ class Railtie < Rails::Railtie # :nodoc:
136 136 end
137 137 end
138 138
  139 + initializer "active_record.validate_explain_support" do |app|
  140 + if app.config.active_record[:auto_explain_threshold_in_seconds] &&
  141 + !ActiveRecord::Base.connection.supports_explain?
  142 + warn "auto_explain_threshold_in_seconds is set but will be ignored because your adapter does not support this feature. Please unset the configuration to avoid this warning."
  143 + end
  144 + end
  145 +
139 146 # Expose database runtime to controller for logging.
140 147 initializer "active_record.log_runtime" do |app|
141 148 require "active_record/railties/controller_runtime"
10 activerecord/test/cases/explain_test.rb
@@ -108,6 +108,16 @@ def test_exec_explain_with_binds
108 108 assert_equal expected, base.exec_explain(queries)
109 109 end
110 110
  111 + def test_unsupported_connection_adapter
  112 + connection.stubs(:supports_explain?).returns(false)
  113 +
  114 + base.logger.expects(:warn).never
  115 +
  116 + with_threshold(0) do
  117 + Car.where(:name => 'honda').to_a
  118 + end
  119 + end
  120 +
111 121 def test_silence_auto_explain
112 122 base.expects(:collecting_sqls_for_explain).never
113 123 base.logger.expects(:warn).never

0 comments on commit 50e8613

Please sign in to comment.
Something went wrong with that request. Please try again.