用sql查询tarantool中不区分大小写的列

omtl5h9j  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(369)

我们知道,通过指定排序规则选项,字符串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']
...

但问题是-它使用索引吗?如果没有索引,它也可以工作。。。

anauzrmj

anauzrmj1#

可以检查查询计划以确定是否使用了特定索引。要获取查询计划,只需在原始查询中添加“explain query plan”前缀。例如:

tarantool>  box.execute("explain query plan select * from \"test\" where \"col1\" = 'AAA' collate \"unicode_ci\"")
---
- metadata:
  - name: selectid
    type: integer
  - name: order
    type: integer
  - name: from
    type: integer
  - name: detail
    type: text
  rows:
  - [0, 0, 0, 'SEARCH TABLE test USING COVERING INDEX col1_idx (col1=?) (~1 row)']
...

所以答案是“是”,在这个例子中使用了索引。
再举一个例子:

box.execute("select * from \"test\" indexed by \"col1_idx\" where \"col1\" = 'AAA'")

不幸的是,这个比较中的排序规则是二进制的,因为索引的排序规则被忽略了。在sql中,比较期间只考虑使用列的排序规则。此限制将在相应问题解决后立即解决。

相关问题