注册

text字段使用to_char() 修改类型后 ,使用like ‘xxx%’前缀匹配加上escape转义后不走索引

DM_213954 2023/10/11 766 3

【DM版本】:V8
【操作系统】:CentOS 7
【CPU】:
【问题描述】*:

建表语句:

CREATE TABLE "test"."test1"
(
"id" TEXT,
"id1" CHAR(2000)) STORAGE(ON "MAIN", CLUSTERBTR) ;

CREATE  INDEX "test1_id" ON "test"."test1"(TO_CHAR("id")) STORAGE(ON "MAIN", CLUSTERBTR) ;
CREATE  INDEX "test1_id1" ON "test"."test1"("id1" ASC) STORAGE(ON "MAIN", CLUSTERBTR) ;

建了一个测试表,id为text字段。创建了一个to_char(id)索引

插入了十万条随机数据。

执行以下sql,走了索引。

explain select count(*) from "test"."test1" where to_char("id") like '123%'

image.png

执行以下sql,加了escape对下划线进行转义,却是全表扫描

explain select count(*) from "test"."test1" where to_char("id") like '123\_%' escape '\'

image.png

在varchar字段上就不会出现这种情况,加了escape也能走索引。

如何让to_char(text)字段使用转义符的时候,like前缀匹配也能走索引呢?

回答 0
暂无回答
扫一扫
联系客服