https://www.cnblogs.com/kingbase/p/14798059.html

Postgresql 常用的字符数据类型的有char、varchar和text,其中 char 固定长度类型, varchar 和 text 是可变长度类型。这三种类型在进行比较时,会进行隐含的类型转换。这种转换会导致索引可能无法使用,影响SQL的执行计划。以下以例子的形式展示Postgresql 不同字符数据类型间的转换规则。

一、创建测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create table test_char(id char(9),desc_info text);
create table test_varchar(id varchar(9),desc_info text);
create table test_text(id text,desc_info text);
insert into test_char select generate_series(100001,200000),repeat('a',100);
insert into test_varchar select generate_series(100001,200000),repeat('a',100);
insert into test_text select generate_series(100001,200000),repeat('a',100);
create index ind_test_char on test_char(id);
create index ind_test_varchar on test_varchar(id);
create index ind_test_text on test_text(id);
analyze test_char;
analyze test_varchar;
analyze test_text;

二、创建SQL游标

1
2
3
4
5
6
7
8
prepare test_char_bind_varchar(varcharas select from test_char where id=$1;
prepare test_char_bind_text(text) as select from test_char where id=$1;
prepare test_varchar_bind_char(charas select from test_varchar where id=$1;
prepare test_text_bind_char(charas select from test_text where id=$1;
prepare test_varchar_bind_text(text) as select from test_varchar where id=$1;
prepare test_text_bind_varchar(varcharas select from test_text where id=$1;

三、Postgresql字符类型的隐含转换规则

1、对于 varchar 与 char 比较,默认是 varchar 转成 char。

例子2,由于等式左边发生了类型转换,无法使用索引。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例子1:
testdb=# explain execute test_char_bind_varchar('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_char on test_char  (cost=0.42..8.44 rows=1 width=111)
   Index Cond: (id = '123456'::bpchar)
(2 rows)
例子2:等式左边发生类型转换,无法使用索引
testdb=# explain execute test_varchar_bind_char('123456');
                           QUERY PLAN                           
-----------------------------------------------------------------
 Seq Scan on test_varchar  (cost=0.00..2975.00 rows=1 width=108)
   Filter: ((id)::bpchar = '123456'::bpchar)
(2 rows)

2、对于 text 与 char 比较,默认是 char 转成 text 。

例子3,由于等式左边发生了类型转换,无法使用索引。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例子3:等式左边发生类型转换,无法使用索引。
testdb=# explain execute test_char_bind_text('123456');
                           QUERY PLAN                          
----------------------------------------------------------------
 Seq Scan on test_char  (cost=0.00..3225.00 rows=500 width=111)
   Filter: ((id)::text = '123456'::text)
(2 rows)
例子4:
testdb=# explain execute test_text_bind_char('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_text on test_text  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: (id = '123456'::text)
(2 rows)

3、对于 varchar 与 text 比较,默认是 varchar 转成 text ,但二者的转换不影响索引的使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
testdb=# explain execute test_varchar_bind_text('123456');
                                      QUERY PLAN                                      
---------------------------------------------------------------------------------------
 Index Scan using ind_test_varchar on test_varchar  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: ((id)::text = '123456'::text)
(2 rows)
testdb=# explain execute test_text_bind_varchar('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_text on test_text  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: (id = '123456'::text)
(2 rows)

PG 字符类型数据转换规则:varchar -> char -> text

四、KingbaseES 类型转换及优化

用过Oracle的人都知道,char与varchar 之间的比较不会因为类型不同而无法使用索引,Kingbase在特性上向Oracle靠拢,为用户从Oracle向KingbaseES迁移提供便利。KingbaseES 继承Postgresql 的特性,同时通过代码的优化,避免了char与varchar和text之间比较导致的转换而无法使用索引的情况。以下的例子在KingbaseES V8R6 版本进行过实际验证。

1、对于 varchar 与 char 比较,同样是 varchar 转成 char。

kingbase 针对这个问题,进行了特殊的优化处理,即使等式左边的varchar发生了类型转换,也不影响索引的使用,如:例子6。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例子5:
testdb=# explain execute test_char_bind_varchar('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_char on test_char  (cost=0.42..8.44 rows=1 width=111)
   Index Cond: (id = '123456'::bpchar)
(2 rows)
例子6:不会因为等式左边发生类型转换而无法使用索引。
testdb=# explain execute test_varchar_bind_char('123456');
                                      QUERY PLAN                                      
---------------------------------------------------------------------------------------
 Index Scan using ind_test_varchar on test_varchar  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: ((id)::text = '123456'::text)
(2 rows)

2、对于 text 与 char 比较,kingbase 进行了特殊的优化处理,使得转换发生在等式的右边,不影响索引的使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例子7:
testdb=# explain execute test_char_bind_text('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_char on test_char  (cost=0.42..8.44 rows=1 width=111)
   Index Cond: (id = '123456'::bpchar)
(2 rows)
例子8:
testdb=# explain execute test_text_bind_char('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_text on test_text  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: (id = '123456'::text)
(2 rows)

3、对于 varchar 与 text 比较,默认是 varchar 转成 text 。与PG一样,二者的转换不影响索引的使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
test=# explain execute test_varchar_bind_text('123456');
                                      QUERY PLAN                                      
---------------------------------------------------------------------------------------
 Index Scan using ind_test_varchar on test_varchar  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: ((id)::text = '123456'::text)
(2 rows)
test=# explain execute test_text_bind_varchar('123456');
                                   QUERY PLAN                                   
---------------------------------------------------------------------------------
 Index Scan using ind_test_text on test_text  (cost=0.29..8.31 rows=1 width=108)
   Index Cond: (id = '123456'::text)
(2 rows)

Tips:以上例子是基于Postgresql 12.3 和 KingbaseES V8R6版本测试的结果。

[转帖]KingbaseES不同字符类型比较转换规则的更多相关文章

  1. KingbaseES不同字符类型比较转换规则

    Postgresql 常用的字符数据类型的有char.varchar和text,其中 char 固定长度类型, varchar 和 text 是可变长度类型.这三种类型在进行比较时,会进行隐含的类型转 ...

  2. 字符类数据类型和oracle字符类型的区别

    为兼容Oracle的数据类型,KingbaseES扩展了Oracle的NUMBER.VARCHAR2.CHAR(n)和DATE类型.该措施使得移植Oracle的Create Table等DDL语句时, ...

  3. kingbase字符类数据类型和oracle字符类型的区别

    为兼容Oracle的数据类型,KingbaseES扩展了Oracle的NUMBER.VARCHAR2.CHAR(n)和DATE类型.该措施使得移植Oracle的Create Table等DDL语句时, ...

  4. 交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致

    在做交叉报表列头的排序时,遇到这三个问题,下面具体来说一下. 设计的数据库的表结构如图1所示: 图1 要处出来student_name_,s.grade_,s.subject_name_,这三个属性, ...

  5. 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语

    数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...

  6. ABAP 使用的字符类型

    1.ABAP基本数据类型 类型        描述                属性 C            字符类型           默认长度1,最大长度不限N            数字类 ...

  7. 【笨嘴拙舌WINDOWS】字符类型与字符串

    "我将用C语言作为工具,开始WINDOWS API的使用" windows NT 从底层开始支持unicode. 1.字符类型 WINDOWS的字符类型在WINNT.H和CTYPE ...

  8. js密码的校验(判断字符类型、统计字符类型个数)

    /** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...

  9. 返璞归真vc++之字符类型

    在今天,大量使用java与.net的程序员已经很少去真实了解字符的底层表达,但是使用VC++编程,对字符的处理却非常慎重,刚学习vc++肯定会为其中的字符类型给晕头转向,今天本人学习第一节,从字符开始 ...

  10. Python 基础-python环境变量、模块初识及字符类型

    (1).模块内置模块.第三方模块.自定义模块初识模块:sys \ os一般标准库存放路径 C:\Users\Administrator\AppData\Local\Programs\Python\Py ...

随机推荐

  1. 工作中常用的一些Linux指令,简单易记还实用(三)

    成功路上最大的困难就是坚持,每天坚持看一道算法,每周坚持看一本好书! 工作中,离不开Linux系统,很多刚步入职场的小白,往往对于Linux操作系统的使用都显得生疏,最主要的就是对一些常用的指令记忆不 ...

  2. Java 并发编程(七)线程池

    任务的创建与执行 在多线程的编程环境中,理想的情况是每个任务之间都存在理想的边界,各个任务之间相互独立,这样才能够享受到并发带来的明显的性能提升,并且,由于每个任务之间如果都是独立的,对于并发的处理也 ...

  3. flutter中去除导航栏与状态栏

    方法一 SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]); // ...

  4. 知道ThreadLocal吗?一起聊聊到底有啥用

    摘要:ThreadLocal是 java 提供的一个方便对象在本线程内不同方法中传递和获取的类.用它定义的变量,仅在本线程中可见和维护,不受其他线程的影响,与其他线程相互隔离. 本文分享自华为云社区& ...

  5. 梦幻联动!金蝶&华为云面向大企业发布数据库联合解决方案

    摘要:近日,金蝶软件(中国)有限公司(以下简称"金蝶")携手华为云共同发布了金蝶云·星瀚.金蝶云·苍穹和GaussDB(for openGauss)数据库联合解决方案. 本文分享自 ...

  6. 鸿蒙轻内核源码分析:Newlib C

    摘要:本文介绍了LiteOS-M内核Newlib C的实现,特别是文件系统和内存分配释放部分,最后介绍了Newlib钩子函数. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列二十 Newlib ...

  7. RandomAccessFile 读写文件

    将目录下的N个日志文件读写到一个文件中. @Test void verification() throws Exception { File f = new File("D:\\Logs&q ...

  8. AliAGC 自动增益控制算法:解决复杂场景下的音量问题

    音视频会议,直播连麦以及短视频已经成为人们工作.教学以及娱乐的一部分,其背后都离不开音视频实时通信等关键技术的广泛应用.音频方面,可预见的是客户业务形式的多样性,环境的复杂性,以及接入设备的差异性会带 ...

  9. 国内加速访问Github的办法

    说明 自从GitHub私有库免费后,又涌入了一大批开发爱好者. 但国内访问GitHub的速度实在是慢得一匹,在clone仓库时甚至只有10k以下的速度,大大影响了程序员的交友效率. 国内加速访问Git ...

  10. 遇到 Request header is too large,你是如何解决的?

    看到群里有小伙伴问,这个异常要怎么解决: java.lang.IllegalArgumentException: Request header is too large 异常原因 根据Exceptio ...