[转帖]KingbaseES不同字符类型比较转换规则
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( varchar ) as 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( char ) as select * from test_varchar where id=$1; prepare test_text_bind_char( char ) as 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( varchar ) as 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不同字符类型比较转换规则的更多相关文章
- KingbaseES不同字符类型比较转换规则
Postgresql 常用的字符数据类型的有char.varchar和text,其中 char 固定长度类型, varchar 和 text 是可变长度类型.这三种类型在进行比较时,会进行隐含的类型转 ...
- 字符类数据类型和oracle字符类型的区别
为兼容Oracle的数据类型,KingbaseES扩展了Oracle的NUMBER.VARCHAR2.CHAR(n)和DATE类型.该措施使得移植Oracle的Create Table等DDL语句时, ...
- kingbase字符类数据类型和oracle字符类型的区别
为兼容Oracle的数据类型,KingbaseES扩展了Oracle的NUMBER.VARCHAR2.CHAR(n)和DATE类型.该措施使得移植Oracle的Create Table等DDL语句时, ...
- 交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致
在做交叉报表列头的排序时,遇到这三个问题,下面具体来说一下. 设计的数据库的表结构如图1所示: 图1 要处出来student_name_,s.grade_,s.subject_name_,这三个属性, ...
- 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 ...
- ABAP 使用的字符类型
1.ABAP基本数据类型 类型 描述 属性 C 字符类型 默认长度1,最大长度不限N 数字类 ...
- 【笨嘴拙舌WINDOWS】字符类型与字符串
"我将用C语言作为工具,开始WINDOWS API的使用" windows NT 从底层开始支持unicode. 1.字符类型 WINDOWS的字符类型在WINNT.H和CTYPE ...
- js密码的校验(判断字符类型、统计字符类型个数)
/** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...
- 返璞归真vc++之字符类型
在今天,大量使用java与.net的程序员已经很少去真实了解字符的底层表达,但是使用VC++编程,对字符的处理却非常慎重,刚学习vc++肯定会为其中的字符类型给晕头转向,今天本人学习第一节,从字符开始 ...
- Python 基础-python环境变量、模块初识及字符类型
(1).模块内置模块.第三方模块.自定义模块初识模块:sys \ os一般标准库存放路径 C:\Users\Administrator\AppData\Local\Programs\Python\Py ...
随机推荐
- 交换机SNMP配置
配置参考v2c为例 1.华为 snmp-agent protocol source-interface vlanif 1 ##S573x以上型号交换机需要snmp-agentsnmp-agent sy ...
- MySQL优化:12种提升SQL执行效率的有效方法
在数据库管理和优化的世界里,MySQL作为一个流行的关系型数据库管理系统,其性能优化是任何数据密集型应用成功的关键.优化MySQL数据库不仅可以显著提高SQL查询的效率,还能确保数据的稳定性和可靠性. ...
- Ubuntu 之 7zip使用
1.安装 sudo apt-get install p7zip 2.压缩 7zr a xxx foldername 3.解压缩 7zr x xxx.7z 4.zip命令压缩文件夹 zip -qr xx ...
- 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 07.在C++中使用UMG
斯坦福 UE4 C++ ActionRoguelike游戏实例教程 07.在C++中使用UMG 斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 本篇文章的目标是 ...
- 谈谈muduo库的销毁连接对象——C++程序内存管理和线程安全的极致体现
前言 网络编程的连接断开一向比连接建立复杂的多,这一点在陈硕写的muduo库中体现的淋漓尽致,同时也充分体现了C++程序在对象生命周期管理上的复杂性,稍有不慎,满盘皆输. 为了纪念自己啃下muduo库 ...
- Git使用经验总结1
目录 1. 概述 2. 界面化工具 3. 远端覆盖本地 4. 设置代理 1. 概述 就不去介绍一些Git最常规的命令了,这些命令一般的教程都有,这里更多的总结自己的一些使用经验.当然作为初学者,常规的 ...
- C++篇:第六章_指针_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 六.指针 (一)指针规则 两个指针不能进行加法运算,因为指针是变量,其值是另一个 ...
- 【玩转鲲鹏 DevKit系列】如何快速迁移无源码应用?
本文分享自华为云社区<[玩转鲲鹏 DevKit系列]如何快速迁移无源码应用?>,作者: 华为云社区精选. 为了帮助广大用户和开发者快速将无源码应用从 x86 迁移到鲲鹏,鲲鹏 DevKit ...
- 理论+实践,揭秘昇腾CANN算子开发
摘要: 本文介绍了CANN自定义算子开发的几种开发方式和算子的编译运行流程.然后以开发一个DSL Add算子为例,讲解算子开发的基本流程. 本文分享自华为云社区<昇腾CANN算子开发揭秘> ...
- Could not autowire. No beans of 'RestTemplate' type found.
解决方案 @Resourceprivate RestTemplate restTemplate;