【发布时间】:2020-10-09 20:53:09
【问题描述】:
我们知道字符串 Tarantool 索引可以通过指定排序选项设置为不区分大小写:collation = "unicode_ci"。例如:
t = box.schema.create_space("test")
t:format({{name = "id", type = "number"}, {name = "col1", type = "string"}})
t:create_index('primary')
t:create_index("col1_idx", {parts = {{field = "col1", type = "string", collation = "unicode_ci"}}})
t:insert{1, "aaa"}
t:insert{2, "bbb"}
t:insert{3, "ccc"}
现在我们可以进行不区分大小写的查询:
tarantool> t.index.col1_idx:select("AAA")
---
- - [1, 'aaa']
...
但是如何使用 SQL 来实现呢?这不起作用:
tarantool> box.execute("select * from \"test\" where \"col1\" = 'AAA'")
---
- metadata:
- name: id
type: number
- name: col1
type: string
rows: []
...
这个也不行:
tarantool> box.execute("select * from \"test\" indexed by \"col1_idx\" where \"col1\" = 'AAA'")
---
- metadata:
- name: id
type: number
- name: col1
type: string
rows: []
...
有一个性能不佳的肮脏技巧(完整扫描)。我们不想要它,是吗?
tarantool> box.execute("select * from \"test\" indexed by \"col1_idx\" where upper(\"col1\") = 'AAA'")
---
- metadata:
- name: id
type: number
- name: col1
type: string
rows:
- [1, 'aaa']
...
最后,我们还有一个解决方法:
tarantool> box.execute("select * from \"test\" where \"col1\" = 'AAA' collate \"unicode_ci\"")
---
- metadata:
- name: id
type: number
- name: col1
type: string
rows:
- [1, 'aaa']
...
但问题是 - 它是否使用索引?没有索引它也可以工作......
【问题讨论】:
标签: sql performance tarantool