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方式。

  • 相关阅读:
    AFNetworking https (引用)
    UITextview 垂直居中
    通过经纬度计算两点间多距离
    iOS 状态机
    iOS 去掉table前面的空格
    ubuntu 更新系统时间
    Python 调用 C 语言 so
    转 iOS socket
    java正则表达式
    javascript小应用。
  • 原文地址:https://www.cnblogs.com/chentianwei/p/9390995.html
Copyright © 2011-2022 走看看