视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
What'snewinEdgeRails:EXPLAIN
2020-11-09 13:16:49 责编:小采
文档


What's new in Edge Rails: EXPLAIN: There are some new features related to EXPLAIN in the forthcoming Ruby on Rails 3.2 we'd like to share: Running EXPLAIN manually Automatic EXPLAIN for slow queries Silencing automatic EXPLAIN As of this w

What's new in Edge Rails: EXPLAIN:
There are some new features related to EXPLAIN in the forthcoming Ruby on Rails 3.2 we'd like
to share:

  • Running EXPLAIN manually
  • Automatic EXPLAIN for slow queries
  • Silencing automatic EXPLAIN
  • As of this writing they are available for the adapters sqlite3, mysql2, and postgresql.

    Running EXPLAIN Manually

    You can now run EXPLAIN on the SQL generated by a relation this way:

    User.where(:id => 1).joins(:posts).explain
    The result of that method call is a string that carefully imitates the output of database shells.


    Please note that explain runs the query or queries and asks the database for their respective query plan afterwards. This is because due to eager loading a relation may trigger several queries to fetch the records and their associations, and in such cases some queries need the result of the previous ones.


    If the relation triggers several queries the method still returns a single string with all the query plans. This is an output meant for human consumption so we preferred to present everything as a string in a format which is familiar right away rather than a structure.


    Automatic EXPLAIN For Slow Queries

    Rails 3.2 has the ability to help in detecting slow queries.

    New applications get

    config.active_record.auto_explain_threshold_in_seconds = 0.5

    in config/environments/development.rb. Active Record monitors queries and if they take more than that threshold their query plan will be logged using warn.


    That works for anything running find_by_sql (which is almost everything, since most of Active Record ends up calling that method). In the particular case of relations, the threshold is compared against the total time needed to fetch the records, not against the time taken by each individual query. Because conceptually we are concerned with the cost of the call

    User.where(:id => 1).joins(:posts).explain

    rather than the cost of the different queries that call may trigger due to the implementation.

    By default the threshold is nil in the test and production environments, which means the feature is disabled.


    The value of that parameter is nil also if the threshold is not set, so existing applications will need to add it by hand if they migrate to 3.2 to be able to enable automatic EXPLAIN.

    Silencing Automatic EXPLAIN

    With automatic EXPLAIN enabled, it could still be the case that some queries are just slow and you know they have to be. For example, a heavyweight report in the backoffice.

    The macro silence_auto_explain allows you to avoid having EXPLAIN run repeatedly in those areas of code:


    ActiveRecord::Base.silence_auto_explain do
     # no automatic EXPLAIN here
    end

    Interpreting Query Plans

    The interpretation of the query plans is another topic, these are some pointers:
  • SQLite: EXPLAIN QUERY PLAN
  • MySQL: EXPLAIN Output Format
  • PostgreSQL: Using EXPLAIN
  • Happy debugging!

    下载本文
    显示全文
    专题