zoukankan      html  css  js  c++  java
  • (GoRails)ActiveRecord --explain方法:(优化你的查询)

    https://gorails.com/episodes/activerecord-explain?autoplay=1

    比如没有加index的查询和加了index的查询,调用数据库的速度就差5倍。

    create_table "tweets" do |t|

      t.integer "user_id"

      t.text "body"

      ...时间戳

    end

    create_table "users" do |t|

    ...时间戳

    end

    在rails console上输入:

    User.where(id: 1).joins(:tweets).explain

    结果显示: 在users上用index查询(Index Scan)和在tweets上用Seq Scan查询的时间

    而:

    用add_index :tweets, :usre_id加上index后:则会使用:

    Index Only Scan using index_tweets_id on tweets.速度加快5倍。

    如果你继承了一个rails app 遗产,面对复杂的查询,可以使用explain来理解。

    Postgresql文档https://www.postgresql.org/docs/current/static/using-explain.html

    scan nodes是节点的底层,它们返回raw rows从一个表格。

    不同类型的扫描节点scan nodes对应不同的表格存取方法:

    sequential scans序列扫描

    index scans 

    bitmap scans 

    还有各种查询子句的scan方式。

  • 相关阅读:
    git
    读后感
    总结
    封装,策略,Asp换脸
    典型用户
    第四次作业
    第三次作业
    计算
    感悟
    对git的认识
  • 原文地址:https://www.cnblogs.com/chentianwei/p/9390995.html
Copyright © 2011-2022 走看看