Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 活动记录作用域的可交换性和顺序_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 活动记录作用域的可交换性和顺序

Ruby on rails 活动记录作用域的可交换性和顺序,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,在Rails的活动记录查询中,“排序”过滤器(Desc或Asc)的位置在性能和逻辑方面是否重要 例如,以下范围1将sam定义为范围2 范围1 scope :default_stream, -> { order(deal_end_date: :asc) } # this is the "sorting query" scope :scope_1, lambda { default_stream.where('deal_start_date <= ? AND deal_end

在Rails的活动记录查询中,“排序”过滤器(Desc或Asc)的位置在性能和逻辑方面是否重要

例如,以下范围1将sam定义为范围2

范围1

scope :default_stream, -> { order(deal_end_date: :asc) } # this is the "sorting query"
scope :scope_1,
      lambda { default_stream.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true) }
scope:default\u stream,->{order(deal\u end\u date::asc)}}这是“排序查询”
范围:范围1,
lambda{default_stream.where('deal_start_date=?',Time.zone.now,Time.zone.now)。where(is_cool:true)}
范围2

scope :scope_2,
      lambda { Deal.all.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true).order(deal_end_date: :asc) }
范围:范围2,
lambda{Deal.all.where('Deal\u start\u date=?',Time.zone.now,Time.zone.now)。where(is\u cool:true)。order(Deal\u end\u date::asc)}
我应该先使用“排序查询”,然后使用其他过滤器(scope1)还是相反的过滤器(scope2)


不确定它是否有影响,但让我告诉你,交易的数量可能非常重要(>100000)

没关系。直到您尝试读取结果时,才会实际计算您的作用域
order
where
子句可以按任意顺序追加到关系中,因为它们在最终SQL查询中被放置在同一位置

如果有疑问,请尝试两种方法,并在您的作用域上调用
。\u sql
。您会发现
scope1.to_sql
scope2.to_sql
相同

给定一个简单的
Post
模型,您会发现
Post.order(:name).where(active:true)
Post.where(active:true).order(:name)
product完全相同SQL:

select * from posts where active = true order by name

无论是
顺序
还是
位置
都不会“先发生”,这是不可能的。只生成了一个SQL查询,它的所有不同子句(where、order、limit、offset等)都在生成语法上有效的查询所需的位置。

因此,将非排序查询(where('deal\u..Time.zone.now.)、where(is\u cool:true))放在第一位不会更快吗?我认为这是因为它将输出有限/更少的行数,而不是排序查询(order(deal\u end\u date::asc)第二步比对整个数据库行进行排序要快……您是说,实际上在生成的sql中,active record?@Mathieu会自动对此进行优化,这根本不是它的工作原理。
.order(foo).where(bar)
.where(foo).order(bar)
将执行完全相同的操作。它们将生成相同的SQL和相同的记录。排序/筛选由数据库完成,而不是由Ruby完成。无论是
顺序
还是
其中
发生在“第一位”;Rails都会接受所有这些操作(无论您以何种顺序调用它们)并将它们转换为一个SQL查询。好的,我明白了,我会检查的。对于有疑问的SQL:)。非常感谢你为我指明了正确的方向