使用不同迭代器从 Tarantool 中选择组合索引

我有一个名为 peoples 的 space:

  1. id
  2. name
  3. age

带有两个索引:

peoples = box.schema.space.create('peoples', {engine = 'memtx', field_count = 3, if_not_exists = true})
peoples:create_index('primary', {type = 'TREE', unique = true, parts = {1, 'unsigned'}, if_not_exists = true})
peoples:create_index('by_name_age', {type = 'TREE', unique = false, parts = {2, 'STR', 3, 'unsigned'}, if_not_exists = true})

还有一些数据:

peoples:auto_increment{ 'name_1', 10 }
peoples:auto_increment{ 'name_1', 11 }
peoples:auto_increment{ 'name_1', 12 }

我需要根据姓名和年龄选择 peoples 多于某个值。 例如,我想选择所有年龄大于 10 岁的 'Alex',等待下一个结果:

[1, 'Alex', 11]
[2, 'Alex', 12]

我尝试执行以下查询:

peoples.index.by_name_age:select({'Alex', 10}, {{iterator = box.index.EQ},{iterator = box.index.GT}})

但结果为

[1, 'Alex', 10]

我该如何为姓名和年龄索引部分使用不同的迭代器?

点赞
用户6098274
用户6098274

你有一个按第一部分排序的索引。这意味着它无法按你所期望的方式进行选择。

为了看到预期的行为,你必须使用for loop [1]和GE迭代器,你必须在此循环内使用if条件测试区间。

[1] 一个简单的代码示例:

local test_val = 'Alex'
for _, v in peoples.index.by_name_age:select({test_val, 10}, {iterator = box.index.GE})
   if v[1] ~= test_val then
   -- do exit
   end
   -- do store result
end
2018-03-22 08:14:09
用户1135874
用户1135874

**说明**:此处开始遍历所有 `name >= 'Alex' and age >= 10` 的记录。使用 `take_while` 将记录限制为仅匹配 `name == 'Alex'` 的记录。:totable()``` 是可选的,如果没有它,这个表达式可以在类似于 pairs 的 for 循环中使用。

2019-09-03 07:10:59