From 0be5adaedf6565630bed04c6c651f3ff8d77c5b8 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 2 Dec 2011 11:16:07 -0800 Subject: implements AR::Base(.|#)silence_auto_explain --- .../guides/source/active_record_querying.textile | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'railties') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 352f23dc01..0ccaffe45e 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -1369,6 +1369,43 @@ EXPLAIN for: SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` IN (1) under MySQL. +h4. Automatic EXPLAIN + +Active Record is able to run EXPLAIN automatically on slow queries and log its +output. This feature is controlled by the configuration parameter + + +config.active_record.auto_explain_threshold_in_seconds + + +If set to a number, any query exceeding those many seconds will have its EXPLAIN +automatically triggered and logged. In the case of relations, the threshold is +compared to the total time needed to fetch records. So, a relation is seen as a +unit of work, no matter whether the implementation of eager loading involves +several queries under the hood. + +A threshold of +nil+ disables automatic EXPLAINs. + +The default threshold in development mode is 0.5 seconds, and +nil+ in test and +production modes. + +h5. Disabling Automatic EXPLAIN + +Automatic EXPLAIN can be selectively silenced with +silence_auto_explain+, which +is a class and instance method of +ActiveRecord::Base+: + + +silence_auto_explain do + # no automatic EXPLAIN is triggered here +end + + +That may be useful for queries you know are slow but fine, like a heavyweigth +report of an admin interface. + +As its name suggests, +silence_auto_explain+ only silences automatic EXPLAINs. +Explicit calls to +ActiveRecord::Relation#explain+ run. + h4. Interpreting EXPLAIN Interpretation of the output of EXPLAIN is beyond the scope of this guide. The -- cgit v1.2.3