Browse files

Merge pull request #7078 from kennyj/logging_query_plan

Log query plan when we use count_by_sql method.
  • Loading branch information...
2 parents 2897bf1 + 1b25283 commit fa3c84fabee01847c3468cd1e8e32003693bc629 @rafaelfranca rafaelfranca committed Jul 19, 2012
Showing with 14 additions and 2 deletions.
  1. +4 −2 activerecord/lib/active_record/querying.rb
  2. +10 −0 activerecord/test/cases/explain_test.rb
View
6 activerecord/lib/active_record/querying.rb
@@ -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"
def count_by_sql(sql)
- sql = sanitize_conditions(sql)
- connection.select_value(sql, "#{name} Count").to_i
+ logging_query_plan do
+ sql = sanitize_conditions(sql)
+ connection.select_value(sql, "#{name} Count").to_i
+ end
end
end
end
View
10 activerecord/test/cases/explain_test.rb
@@ -68,6 +68,16 @@ def test_collecting_queries_for_explain
assert_equal [cars(:honda)], result
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
sqls = %w(foo bar)
binds = [[], []]

0 comments on commit fa3c84f

Please sign in to comment.