2

我想在一次调用中从 Tarantool 中选择多条记录,但看不到如何将多个键传递给space:getspace:select

4

2 回答 2

4

您可以使用 Lua 和 SQL 来完成。

1) 在 Lua 中使用存储过程,如下所示:

function select_several(space_name, index_name, keys)
    local obj = index_name == nil and box.space[space_name] or box.space[space_name].index[index_name]
    local result = {}
    for _, key in pairs(keys) do
        table.insert(result, obj:get(key))
    end
    return result
end
...
select_several('test', nil, {1, 2})

2)从 Tarantool 2.0 开始,可以使用 SQL(前提是你有空格格式):

box.execute('select * from "test" where "id" in (1, 3);')
于 2019-06-14T13:54:16.257 回答
3

select * from "test" where "id" in (1, 3)另一种等效于使用LuaFun的 SQL 查询的变体:

tarantool> box.space.test:pairs():filter(function (tuple) return tuple.id == 1 or tuple.id == 3 end):totable()

如果空间中没有“id”索引,则它是一个通用变体,它意味着执行全扫描。

如果存在名为“id”的唯一索引,则可以使用更有效的变体:

tarantool> fun.iter({1, 3}):map(function (value) return box.space.test.id:get(value) end):totable()

否则,如果索引不是唯一的,那么它看起来像

tarantool> fun.iter({1, 3}):map(function (value) return box.space.test.id:select(value) end):reduce(function (result, data) for _, rec in ipairs(data) do table.insert(result, rec) end return result end, {})
于 2019-06-14T14:22:32.027 回答