How do I select a limited number of records from tarantool, like with SELECT LIMIT in SQL?

橙三吉。 提交于 2021-02-10 06:00:13

问题


I want to perform a selection on a Tarantool space, using filtering and limiting the results, as I can do with a simple SQL query like "SELECT * FROM users WHERE age > 33 LIMIT 1". How can I achieve that?


回答1:


It can be done using Lua as well as SQL.

1) Here is an example in Lua. Assume we have a space named "users" with fields "name", "surname", "age". First, let's create and populate the space:

$ tarantool
Tarantool 2.1.1-465-gbd1a43fbc
type 'help' for interactive help
tarantool> box.cfg({})
...
2019-06-10 14:51:33.827 [47393] main/102/interactive I> ready to accept requests
...
2019-06-10 14:51:33.827 [47393] main/104/checkpoint_daemon I> scheduled next checkpoint for Mon Jun 10 16:14:44 2019
---
...

tarantool> s = box.schema.space.create('users', {temporary=true})
---
...

tarantool> box.space.users:format({{'id','unsigned'},{'name','string'},{'surname','string'},{'age','unsigned'}})
---
...

tarantool> s:create_index('primary', {unique = true, parts = {1, 'unsigned'}})
---
- unique: true
  parts:
  - type: unsigned
    is_nullable: false
    fieldno: 1
  id: 0
  space_id: 512
  name: primary
  type: TREE
...

tarantool> s:insert({1,'Pasha','Yudin',33})
---
- [1, 'Pasha', 'Yudin', 33]
...

tarantool> s:insert({3,'Kostya','Nazarov',34})
---
- [2, 'Kostya', 'Nazarov', 34]
...

tarantool> s:insert({2,'Oleg','Babin',23})
---
- [3, 'Oleg', 'Babin', 23]
...

tarantool> s:insert({4,'Roma','Babaev',34})
---
- [4, 'Roma', 'Babaev', 34]
...

Let's select all records from the space:

tarantool> s:select()
---
- - [1, 'Pasha', 'Yudin', 33]
  - [2, 'Kostya', 'Nazarov', 23]
  - [3, 'Oleg', 'Babin', 34]
  - [4, 'Roma', 'Babaev', 34]
...

Next, let's select all users older than 33 years. The LuaFun library may be used:

tarantool> fun = require('fun')
---
...

tarantool> fun.iter(s:select()):filter(function (tuple) return tuple.age > 33 end):totable()
---
- - [3, 'Oleg', 'Babin', 34]
  - [4, 'Roma', 'Babaev', 34]
...

But as @AlexanderTurenko mentioned below, it's better using the pairs iterator for not loading extra tuples into memory:

tarantool> s:pairs():filter(function (tuple) return tuple.age > 33 end):totable()
---
- - [3, 'Oleg', 'Babin', 34]
  - [4, 'Roma', 'Babaev', 34]
...

Also, this variant is shorter and more readable.

Finally, let's select just one user that meets our criteria, which is equivalent to the SQL query "SELECT * FROM users WHERE age > 33 LIMIT 1":

tarantool> s:pairs():filter(function (tuple) return tuple.age > 33 end):take_n(1):totable()
---
- - [3, 'Oleg', 'Babin', 34]
...

2) Starting with Tarantool 2.0, it can be done using SQL (provided that you have the space format):

box.execute('select * from users where age > 33 limit 1;')


来源:https://stackoverflow.com/questions/56584050/how-do-i-select-a-limited-number-of-records-from-tarantool-like-with-select-lim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!