【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%'
执行以下sql,加了escape对下划线进行转义,却是全表扫描
explain select count(*) from "test"."test1" where to_char("id") like '123\_%' escape '\'
在varchar字段上就不会出现这种情况,加了escape也能走索引。
如何让to_char(text)字段使用转义符的时候,like前缀匹配也能走索引呢?
按照这个思路,这样写应该也是等价的吧
select count(*) from test1 where to_char(id) >='123_' and to_char(id)<'123'||chr(ascii('_')+1);
改写成这样呢?
