gpt4 book ai didi

ruby-on-rails - 如果凌乱的 Controller 更快,为什么 Rails 范围更可取?

转载 作者:行者123 更新时间:2023-12-04 03:43:36 26 4
gpt4 key购买 nike

我一直在尝试使用范围链接 Arel 查询,而不是仅使用我在 Controller 中编写的一些冗长的逻辑。但是范围比获取所有记录然后用一些逻辑筛选它们要慢。那么,我想知道为什么范围更好。

这是我在做什么:

  • 一个问题有很多答案
  • 一个答案属于一个问题
  • 一个问题有一个“question_type”列,我用它来排序

  • 一、scopes方式...

    问题.rb:
    scope :answered, joins(:answers).order('answers.created_at desc')
    scope :dogs, where(:question_type => "dogs")
    scope :cats, where(:question_type => "cats")
    scope :mermaids, where(:question_type => "mermaids")

    在questions_controller.rb 中:
    @dogs_recently_answered = Question.answered.dogs.uniq[0..9]
    @cats_recently_answered = Question.answered.cats.uniq[0..9]
    @mermaids_recently_answered = Question.answered.mermaids.uniq[0..9]

    然后在 View 中,我循环浏览那些实例变量(现在是最多包含 10 个元素的数组)并显示结果。

    以下是加载页面所需的时间(五个不同的时间):

    在 535 毫秒内完成 200 个 OK(查看次数:189.6 毫秒 | ActiveRecord:46.2 毫秒)

    在 573 毫秒内完成 200 个 OK(查看次数:186.0 毫秒 | ActiveRecord:46.3 毫秒)

    在 577ms 内完成 200 OK(查看:189.0ms | ActiveRecord:45.6ms)

    在 532ms 内完成 200 OK(查看:182.9ms | ActiveRecord:46.1ms)

    在 577ms 内完成 200 OK(查看:186.7ms | ActiveRecord:46.9ms)

    现在,凌乱的 Controller 方式...
    @answers = Answer.order("created_at desc")
    @all_answered = []
    @answers.each {|answer| @all_answered << answer.question}
    @recently_answered = @all_answered.uniq
    @dogs_all_answered = []
    @cats_all_answered = []
    @mermaids_all_answered = []
    @recently_answered.each do |q|
    if q.question_type == "dogs"
    @dogs_all_answered << q
    @dogs_recently_answered = @dogs_all_answered[0..9]
    elsif q.question_type == "cats"
    @cats_all_answered << q
    @cats_recently_answered = @cats_all_answered[0..9]
    elsif q.question_type == "mermaids"
    @mermaids_all_answered << q
    @mermaids_recently_answered = @mermaids_all_answered[0..9]
    end
    end

    以下是现在加载页面所需的时间(五个不同的时间):

    在 475ms 内完成 200 OK(查看:196.5ms | ActiveRecord:34.5ms)

    在 480 毫秒内完成 200 次 OK(查看次数:200.4 毫秒 | ActiveRecord:36.4 毫秒)

    在 434ms 内完成 200 OK(查看:198.2ms | ActiveRecord:35.8ms)

    在 475ms 内完成 200 OK(查看:194.2ms | ActiveRecord:36.4ms)

    在 475ms 内完成 200 OK(查看:195.0ms | ActiveRecord:35.4ms)

    所以...

    除了可读性之外,用范围磨练查询还有什么好处?当有更多记录时,它最终会变得更快吗?

    最佳答案

    首先,我不确定我是否理解一个问题除了独特之外还有什么不同,所以我会考虑尝试删除它。我不知道您数据的逻辑,因此这可能不适用,但这是您可以避免的额外步骤。

    这是我将如何处理它:

    scope :answered, joins(:answers).order('answers.created_at desc')
    scope :recent, take(10)
    scope :dogs, where(:question_type => "dogs")
    scope :cats, where(:question_type => "cats")
    scope :mermaids, where(:question_type => "mermaids")

    @dogs_recently_answered = Question.answered.dogs.recent
    @cats_recently_answered = Question.answered.dogs.recent
    @mermaids_recently_answered = Question.answered.dogs.recent

    这改变了 TOP查询的一部分到它所属的数据库,而不是获取所有行,然后丢弃除 10 之外的所有行。根据您的唯一条件,您还可以使用类似的范围
    scope :unique, select('DISTINCT column_name')

    然后您可以使用 Question.cats.unique.recent 并在一个利用数据库系统设计的关系代数的快速查询中获得所有信息。

    关于ruby-on-rails - 如果凌乱的 Controller 更快,为什么 Rails 范围更可取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4314273/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com