2
  local orders = box.schema.space.create('orders')
  box.schema.sequence.create('orderId')
  orders:create_index('id', {sequence='orderId'})
  orders:create_index('price', {unique=false, parts={2, 'integer'}})

  local bestOrder = orders.index.price:min()

I'm searching best order (minimal price) with min() function over secondary index. How is Tarantool sorting records if they have the same price (secondary index)? I tested and look like by primary index. Is this behaviour standardized?

farwayer
  • 3,872
  • 3
  • 21
  • 23

1 Answers1

3

Tarantool has several types of indexes [1]. And some of are sorted some of are not sorted. For instance, the tree index [2] will be sorted, that means you can get bounds, and the hash index [3] is not sorted, means you can get bounds without copy and sort.

In your case, you use a tree index. So you can use min, max, bound selects etc for 'price' index.

UPDATE (after had some talks via email)

Tarantool returns first tuple by primary key for min() if several records have equal secondary index and last for max().

This behavior is normal for Tarantool, and you can count that it would not be changed in the near future.


[1] https://tarantool.org/en/doc/2.0/book/box/box_index.html?highlight=index#module-box.index

[2] https://en.wikipedia.org/wiki/B-tree

[3] https://en.wikipedia.org/wiki/Hash_table

farwayer
  • 3,872
  • 3
  • 21
  • 23