Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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/4/postgresql/10.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_Postgresql - Fatal编程技术网

Ruby on rails 使用带有限制和偏移的范围和顺序

Ruby on rails 使用带有限制和偏移的范围和顺序,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,使用postgresql和RubyonRails,我想批量处理一个大型数据库。我似乎无法使用find_each,因为它使用最小的id向上。但是我需要处理最新的数据,它具有最大的id 我目前的尝试是 score_ok.order(cmp_id: :desc).limit(X).offset(Y).pluck(:id) where score_ok是带有where子句的范围。 如果我在一个小的测试数据库上尝试这个,那么没有限制和偏移,即 score_ok.order(cmp_id: :desc).

使用postgresql和RubyonRails,我想批量处理一个大型数据库。我似乎无法使用find_each,因为它使用最小的id向上。但是我需要处理最新的数据,它具有最大的id

我目前的尝试是

score_ok.order(cmp_id: :desc).limit(X).offset(Y).pluck(:id)
where score_ok是带有where子句的范围。 如果我在一个小的测试数据库上尝试这个,那么没有限制和偏移,即

score_ok.order(cmp_id: :desc).pluck(:id)
我明白了

[372、362、363、361、366、367、368、369、370、371、364]

现在如果我这样做了

score_ok.order(cmp_id: :desc).limit(2).offset(0).pluck(:id)
我明白了

如果我这样做了

score_ok.order(cmp_id: :desc).limit(2).offset(2).pluck(:id)
我明白了


但我想要的是[372362],然后是[363361]。我该怎么做?我已尝试将
限制(2).偏移量(2)
移到查询的开头,但没有帮助。

注释解释了该行为。cmp_id有重复的值,显然数据库不需要每次以相同的方式对相等的值进行排序。解决这个问题的一种方法是添加一个辅助键,以一致的方式断开连接。

这个由
创建的排序在一开始就有点误导。您确定没有使用它而不是
comp\u id
?从开发日志中,您可以粘贴到正在运行的实际SQL中吗?我猜想生成的SQL正在做一些意想不到的事情,这将帮助您了解引擎盖下实际发生的事情。当我遇到类似的问题时,我通常使用原始SQL.potashin,我使用的是cmp_id(我已经编辑了这个问题),但是如果我替换为created_at,查询将按预期工作。cmp_id的类型为日期。stef,明天我将查看原始sql。
cmp\u id
是一个
date
(粒度有限的类型)和您看到的行为(包括在
行为处创建的
表明您有重复的
cmp\u id
值。数据库不需要每次都以相同的方式对相等的值进行排序,您可能需要添加第二个排序键以一致的方式断开关系。包括更多的数据(例如每个
id
cmp\u id
值)将清除此问题。
score_ok.order(cmp_id: :desc).limit(2).offset(2).pluck(:id)
[362, 366]