You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于复合索引使用不同迭代器在Tarantool中执行查询

Hey there! Let's walk through how to use all the common iterator types with your by_name_age composite index in Tarantool. I'll use your peoples space structure and sample data to make examples concrete.

Using Iterators with the by_name_age Composite Index

First, a quick reminder: your by_name_age index is ordered by name (string) first, then age (unsigned integer). This means you can query by just name (prefix matching) or by both name and age for more precise results. Below are each iterator type with practical examples:

1. EQ (Exact Match)

This is the default iterator—perfect for finding tuples that exactly match your query values. You can match on just name (which returns all tuples with that name) or both name and age for an exact tuple match.

-- Get all tuples where name = 'name_1' (EQ is default, so no need to specify iterator)
local all_name_1 = peoples.index.by_name_age:select({'name_1'})

-- Get the exact tuple where name = 'name_1' AND age = 10
local exact_match = peoples.index.by_name_age:select({'name_1', 10}, {iterator = 'EQ'})

-- Print results
for _, tuple in ipairs(exact_match) do
    print(string.format("ID: %d, Name: %s, Age: %d", tuple[1], tuple[2], tuple[3]))
end

2. GE (Greater Than or Equal)

Use this to fetch tuples where the index values are greater than or equal to your query. For composite indexes, this works by first matching the name prefix, then filtering age values.

-- Get all tuples where name >= 'name_1' (includes all 'name_1' entries plus any names starting after 'name_1')
local ge_name = peoples.index.by_name_age:select({'name_1'}, {iterator = 'GE'})

-- Get tuples where name = 'name_1' AND age >= 11
local ge_age = peoples.index.by_name_age:select({'name_1', 11}, {iterator = 'GE'})

3. GT (Greater Than)

Similar to GE, but only returns tuples with strictly greater index values.

-- Get tuples where name > 'name_1' (excludes all 'name_1' entries)
local gt_name = peoples.index.by_name_age:select({'name_1'}, {iterator = 'GT'})

-- Get tuples where name = 'name_1' AND age > 10
local gt_age = peoples.index.by_name_age:select({'name_1', 10}, {iterator = 'GT'})

4. LE (Less Than or Equal)

Fetches tuples where index values are less than or equal to your query.

-- Get all tuples where name <= 'name_1' (includes 'name_1' and any names that come before it)
local le_name = peoples.index.by_name_age:select({'name_1'}, {iterator = 'LE'})

-- Get tuples where name = 'name_1' AND age <= 11
local le_age = peoples.index.by_name_age:select({'name_1', 11}, {iterator = 'LE'})

5. LT (Less Than)

Strictly less than the query values.

-- Get tuples where name < 'name_1' (excludes all 'name_1' entries)
local lt_name = peoples.index.by_name_age:select({'name_1'}, {iterator = 'LT'})

-- Get tuples where name = 'name_1' AND age < 11
local lt_age = peoples.index.by_name_age:select({'name_1', 11}, {iterator = 'LT'})

Reverse Iterators

Tarantool also supports reverse versions of the above iterators, which traverse the index from highest to lowest values instead of lowest to highest.

REQ (Reverse Exact Match)

Exact match, but returns results in reverse order of the index.

local reverse_exact = peoples.index.by_name_age:select({'name_1', 10}, {iterator = 'REQ'})

RGE (Reverse Greater Than or Equal)

Reverse version of GE—starts from the largest matching tuple and goes down.

-- Get tuples where name >= 'name_1', starting from the last entry in the index
local reverse_ge = peoples.index.by_name_age:select({'name_1'}, {iterator = 'RGE'})

RGT (Reverse Greater Than)

Reverse version of GT.

local reverse_gt = peoples.index.by_name_age:select({'name_1'}, {iterator = 'RGT'})

RLE (Reverse Less Than or Equal)

Reverse version of LE.

-- Get tuples where name <= 'name_1', starting from the last matching entry
local reverse_le = peoples.index.by_name_age:select({'name_1'}, {iterator = 'RLE'})

RLT (Reverse Less Than)

Reverse version of LT.

local reverse_lt = peoples.index.by_name_age:select({'name_1'}, {iterator = 'RLT'})

Quick Tip

When using composite indexes, you can leverage prefix matching: if you only specify the first field (name), Tarantool will match all entries with that name regardless of age. If you specify both fields, it narrows down to exact matches on both.

内容的提问来源于stack exchange,提问作者andrey_ustinov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:40:28