什么是在Tarantool数据库中使用SQL LIKE关键字进行查询的正确方法?
例如:
我能搜索部分索引值吗,还是需要编写自己的LUA脚本实现这样的功能?
SELECT * FROM space where smth LIKE '%some_value%';
我能搜索部分索引值吗,还是需要编写自己的LUA脚本实现这样的功能?
SELECT * FROM space where smth LIKE '%some_value%';
是的,你需要编写Lua脚本来迭代空间并在元组的'smth'
字段上使用Lua函数gsub
。目前还没有搜索字符串部分的方法。
如果您使用tarantool 2.x版本,则您的查询没有任何问题。
SELECT * FROM "your_space" WHERE "smth" LIKE '%some_value%';
优化前缀搜索可使用存储过程。 例如,此代码片段也适用于 Cyrillic 文本:
box.schema.create_space('address')
box.space.address:create_index('prefix', { type = 'tree', parts = { { 1, 'str', collation = 'unicode_ci' } }, unique = true })
select_by_prefix = function(prefix)
local result = {}
for _, addr in box.space.address.index.prefix:pairs(prefix, { iterator = 'GT' }) do
if utf.casecmp(utf.sub(addr[1], 1, utf.len(prefix)), prefix) == 0 then
table.insert(result, addr)
else
break
end
end
return result
end