Lua 通过使用不同迭代器的复合索引从Tarantool中选择

Lua 通过使用不同迭代器的复合索引从Tarantool中选择,lua,tarantool,Lua,Tarantool,我有空间人: 身份证 名字 年龄 有两个索引: 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:cre

我有空间

  • 身份证
  • 名字
  • 年龄
  • 有两个索引:

    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”,等待下一个结果:

    [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]
    

    如何对名称和年龄索引部分使用不同的迭代器?

    您有一个索引,它按第一部分排序。意味着它无法按照您的期望进行选择

    你必须做什么才能看到预期的行为?您必须将for循环[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
    

    您有一个索引,它按第一部分排序。意味着它无法按照您的期望进行选择

    你必须做什么才能看到预期的行为?您必须将for循环[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
    
    peoples.index.by_name_age:pairs({'Alex',10},'GE'):
    take_while(函数(x)返回x.name=='Alex'结束):
    totable()
    
    在这里,它开始迭代所有
    name>='Alex'和age>=10的记录
    take_,而
    用于将记录仅限于匹配的
    name==“Alex”

    :totable()
    是可选的,如果没有它,这个表达式可以在for循环中使用,类似于
    pairs

    peoples.index。by_name\u age:pairs({'Alex',10},'GE'):
    take_while(函数(x)返回x.name=='Alex'结束):
    totable()
    
    在这里,它开始迭代所有
    name>='Alex'和age>=10的记录
    take_,而
    用于将记录仅限于匹配的
    name==“Alex”


    :totable()
    是可选的,如果没有它,此表达式可以在类似于
    对的for循环中使用

    谢谢你,Vasily!谢谢你,瓦西里!