Skip to content

Commit

Permalink
Log query plan when we use count_by_sql method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Jul 17, 2012
1 parent 939f014 commit 1b25283
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/querying.rb
Expand Up @@ -62,8 +62,10 @@ def find_by_sql(sql, binds = [])
# #
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id" # Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
def count_by_sql(sql) def count_by_sql(sql)
sql = sanitize_conditions(sql) logging_query_plan do
connection.select_value(sql, "#{name} Count").to_i sql = sanitize_conditions(sql)
connection.select_value(sql, "#{name} Count").to_i
end
end end
end end
end end
10 changes: 10 additions & 0 deletions activerecord/test/cases/explain_test.rb
Expand Up @@ -68,6 +68,16 @@ def test_collecting_queries_for_explain
assert_equal [cars(:honda)], result assert_equal [cars(:honda)], result
end end


def test_logging_query_plan_when_counting_by_sql
base.logger.expects(:warn).with do |message|
message.starts_with?('EXPLAIN for:')
end

with_threshold(0) do
Car.count_by_sql "SELECT COUNT(*) FROM cars WHERE name = 'honda'"
end
end

def test_exec_explain_with_no_binds def test_exec_explain_with_no_binds
sqls = %w(foo bar) sqls = %w(foo bar)
binds = [[], []] binds = [[], []]
Expand Down

0 comments on commit 1b25283

Please sign in to comment.