最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

What'snewinEdgeRails:EXPLAIN

来源:懂视网 责编:小采 时间:2020-11-09 13:16:49
文档

What'snewinEdgeRails:EXPLAIN

What'snewinEdgeRails:EXPLAIN: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'snewinEdgeRails:EXPLAIN: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 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!

    声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文档

    What'snewinEdgeRails:EXPLAIN

    What'snewinEdgeRails:EXPLAIN: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
    推荐度:
    标签: EDGE new what
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top