内存和数据库调用之间的区别

时间:2012-05-30 08:48:32

标签: ruby-on-rails-3 performance has-many-through rails-console

好的,我的第一个项目的主要问题是速度。我不知道哪个是内存和数据库之间的调用。我有两个问题:

  1. 如何区分数据库调用和内存调用(如果调用它的方式)?
  2. 我遇到了一个指向

    的未定义方法错误
    `r.status_messages.last.name`
    

    所以我在rails控制台上测试了它(我有一种感觉 与我的第一个问题有关。)

    简而言之,

    • r.status_messages.last.name不起作用(读作2个命令,r.status_messages.last& .name)
    • 但是,h = r.status_messages.last那么h.name,WORKS
    • 但是当我这样做时,r.status_messages.last.name,现在就可以了。
      这里发生了什么?

  3. 细节
    request_form has_many status_messages:通过request_statuses
    Rails 3.2.3,Ruby193,Windows 7教授,NetBeans IDE 6.9.1
    Rails控制台

    r = RequestForm.find(25) 
    r = RequestForm.find(25)
      [1m[35mRequestForm Load (1.0ms)[0m  SELECT `request_forms`.* FROM `request_forms` WHERE `request_forms`.`id` = ? LIMIT 1  [["id", 25]]
    #<RequestForm id: 25, control_no: 1, requested: true, cds_requested: nil, cds_returned: nil, approval: false, cds_released: nil, cds_recycled: nil, defective_cds: 0, request_date: "2012-05-30", released_date: nil, user_id: 3, admin_id: nil, remarks: nil, created_at: "2012-05-30 07:13:50", updated_at: "2012-05-30 07:13:50">
    r.status_messages
    r.status_messages
    [#<StatusMessage id: 1, description: "can be edited until sent", user_id: nil, created_at: "2012-05-30 07:31:02", updated_at: "2012-05-30 07:31:02", name: "Created">, #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">]
      [1m[36mStatusMessage Load (1.0ms)[0m  [1mSELECT `status_messages`.* FROM `status_messages` INNER JOIN `request_statuses` ON `status_messages`.`id` = `request_statuses`.`status_message_id` WHERE `request_statuses`.`request_form_id` = 25[0m
    r.status_messages.last
    r.status_messages.last
    #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
    r.status_messages.last
    .name
    r.status_messages.last
    #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
    .name
    SyntaxError: (irb):6: syntax error, unexpected '.'
    .name
     ^
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
            from script/rails:6:in `require'
            from script/rails:6:in `<main>'
    r.status_messages.last
    .description
    r.status_messages.last
    #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
    .description
    SyntaxError: (irb):10: syntax error, unexpected '.'
    .description
     ^
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
            from script/rails:6:in `require'
            from script/rails:6:in `<main>'
    h = r.status_messages.last
    
    h = r.status_messages.last
    #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
    
    h.name
    h.name
    "Sent"
    r.status_messages.last.name
    r.status_messages.last.name
    "Sent"
    r.status_messages.last.description
    r.status_messages.last.description
    "visible to admin"
    
    编辑:关于两个查询的划分,我已经说过了这个问题。很抱歉不清楚。

    我输入r.status_messages.last.name 然后按下输入

    但是rails控制台已将其剪切为r.status_messages.last.name。这导致语法错误。

    namedescription是状态消息的属性。

    但是,当我将r.status_messages.last保存到变量h

    h.nameh.description工作。

    令人惊讶的是:r.status_messages.last.name之后也工作得很好。

    1. 如何区分数据库调用和内存调用(如果调用它的话)? 我想要一个很好的参考,其中有一个表格或比较列表,例如.size .length
    2. 导致这种划分的原因是什么?

1 个答案:

答案 0 :(得分:0)

  1. 懒惰的加载让我很困惑。这就是为什么我的应用有意外的数据库调用。不小心调用:count将使用sql COUNT查询数据库。您可能想要使用to_a.size
  2. 这是一个nilClass异常。 r.status_messages.last.name中没有上次状态消息。因此,:name对nil对象引发了未定义的方法错误。
  3. Netbeans终端似乎是这里的问题,正如@DaveNewton评论。