Skip to content

Commit

Permalink
Merge pull request #6197 from blakesmith/connection_adapters_without_…
Browse files Browse the repository at this point in the history
…explain_support

Don't run explain on slow queries for database adapters that don't support it
  • Loading branch information
fxn committed Nov 26, 2012
2 parents 3ec4430 + 50e8613 commit 296412f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions activerecord/lib/active_record/explain.rb
Expand Up @@ -6,11 +6,12 @@ def self.extended(base)
base.mattr_accessor :auto_explain_threshold_in_seconds, instance_accessor: false base.mattr_accessor :auto_explain_threshold_in_seconds, instance_accessor: false
end end


# If auto explain is enabled, this method triggers EXPLAIN logging for the # If the database adapter supports explain and auto explain is enabled,
# queries triggered by the block if it takes more than the threshold as a # this method triggers EXPLAIN logging for the queries triggered by the
# whole. That is, the threshold is not checked against each individual # block if it takes more than the threshold as a whole. That is, the
# query, but against the duration of the entire block. This approach is # threshold is not checked against each individual query, but against the
# convenient for relations. # duration of the entire block. This approach is convenient for relations.

# #
# The available_queries_for_explain thread variable collects the queries # The available_queries_for_explain thread variable collects the queries
# to be explained. If the value is nil, it means queries are not being # to be explained. If the value is nil, it means queries are not being
Expand All @@ -21,7 +22,7 @@ def logging_query_plan # :nodoc:


threshold = auto_explain_threshold_in_seconds threshold = auto_explain_threshold_in_seconds
current = Thread.current current = Thread.current
if threshold && current[:available_queries_for_explain].nil? if connection.supports_explain? && threshold && current[:available_queries_for_explain].nil?
begin begin
queries = current[:available_queries_for_explain] = [] queries = current[:available_queries_for_explain] = []
start = Time.now start = Time.now
Expand Down
7 changes: 7 additions & 0 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -136,6 +136,13 @@ class Railtie < Rails::Railtie # :nodoc:
end end
end end


initializer "active_record.validate_explain_support" do |app|
if app.config.active_record[:auto_explain_threshold_in_seconds] &&
!ActiveRecord::Base.connection.supports_explain?
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."
end
end

# Expose database runtime to controller for logging. # Expose database runtime to controller for logging.
initializer "active_record.log_runtime" do |app| initializer "active_record.log_runtime" do |app|
require "active_record/railties/controller_runtime" require "active_record/railties/controller_runtime"
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/explain_test.rb
Expand Up @@ -108,6 +108,16 @@ def test_exec_explain_with_binds
assert_equal expected, base.exec_explain(queries) assert_equal expected, base.exec_explain(queries)
end end


def test_unsupported_connection_adapter
connection.stubs(:supports_explain?).returns(false)

base.logger.expects(:warn).never

with_threshold(0) do
Car.where(:name => 'honda').to_a
end
end

def test_silence_auto_explain def test_silence_auto_explain
base.expects(:collecting_sqls_for_explain).never base.expects(:collecting_sqls_for_explain).never
base.logger.expects(:warn).never base.logger.expects(:warn).never
Expand Down

0 comments on commit 296412f

Please sign in to comment.