通过具有不同迭代器的复合索引从Tarantool中选择 [英] Select from Tarantool by composite index with different iterators
问题描述
我有空间人:
- id
- 名称
- 年龄
有两个索引:
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 }
我需要按姓名和年龄选择人,而不是某些值.例如,我要选择年龄大于10的所有"Alex",然后等待下一个结果:
I need select peoples by name and by age more than some value. For example, I want to select all 'Alex' with age > 10, waiting for the next result:
[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]
如何为名称和年龄索引部分使用不同的迭代器?
How can I use different iterators for name and age index parts?
推荐答案
您有一个索引,并按第一部分对其进行了排序.意味着它无法按照您的期望进行选择.
You have an index and its sorted by the first part. Means it can't do the selection as you are expecting.
您必须做什么才能看到预期的行为?您必须使用带有GE迭代器的for循环[1],并且必须使用此循环内的if条件来测试范围.
What you have to do to see the expected behavior? You have to use the for loop[1] with GE iterator, and you have to test ranges using the if condition inside this loop.
[1] 一个简单的代码示例:
[1] A simple code example:
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
这篇关于通过具有不同迭代器的复合索引从Tarantool中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!