2

我需要从空间中获取一些记录users。这个空间有一个二级索引category_status_rating。我需要选择具有category=1, status=1, 的 用户rating<=123456789

for _, user in box.space.users.index.category_status_rating:pairs({ 1, 1, 123456789 }, { limit = 20, offset = 5, iterator = box.index.LE }) do
    if user[categoryIdx] ~= 1 or user[statusIdx] ~= 1 then break end
    table.insert(users, user)
end

据我所知,迭代indexName:pairs不支持limit,我可以使用我自己的计数器。但是呢offset?我可以使用这个参数并从我需要的“页面”开始吗?还是我会在没有任何内容的情况下进行迭代offset并传递无用的记录(大约 100000 条)并开始到table.insert(users, user)我的“页面”开始时?谢谢!

4

1 回答 1

3

如果你真的需要它,你可以保存你的位置(这将是最后检查的元组),而不是使用偏移量。例如:

local last = 123456789
for i = 1, 2 do
    local count = 0
    for _, user in box.space.users.index.category_status_rating:pairs({1, 1, last}, { iterator = box.index.LE }) do
        if user[categoryIdx] ~= 1 or user[statusIdx] ~= 1 or count > 20 then
            break
        end
        table.insert(users, user)
        last = user[LAST_INDEX_FIELD]
        count = count + 1
    end
    -- process your tuples
end

或者,使用 luafun (其中drop_n是限制的模拟,保存到last是偏移的模拟):

local last = 123456789
for i = 1, 2 do
    local users = box.space.users.index.category_status_rating:pairs({1, 1, last}, { iterator = box.index.LE }):take_n(20):map(function(user)
        last = user[LAST_INDEX_FIELD]
        return user
    end):totable()
    -- process your tuples
end

LuaFun 的文档嵌入到 Tarantool中。

于 2016-03-30T13:40:06.080 回答