[转帖]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 ...
随机推荐
- 前端系列:基于 Flex 弹性布局详解
目录 基本介绍 基本概念 父项常见属性 flex-direction justify-content flex-wrap align-items align-content flex-flow 子项常 ...
- JavaScript this 绑定详解
函数内 this 绑定 函数内this的绑定和函数定义的位置没有关系,和调用的方式和调用位置有关系,函数内的this是在被调用执行时被绑定的. this的具体绑定规则 this 绑定基本包含下面4种绑 ...
- Programming abstractions in C阅读笔记:p181-p183
<Programming Abstractions In C>学习第61天,p181-p183总结. 一.技术总结 1.linear search algorithm 2.lexicogr ...
- Spring Boot 整合 Log4j2 日志并压测性能
1/ Log4j2的性能测试 从图中不难看出,在线程数为 2~16 之间,混合使用同步和异步的logger来打印日志,性能是最好的. 2/ 目标 混合 sync/async 彩色日志 分类输出到不同文 ...
- 实例讲解基于Sermant快速开发服务治理插件
本文分享自华为云社区<Sermant框架下的服务治理插件快速开发及使用指南>,作者: 华为云开源 . Sermant是基于Java字节码增强技术的云原生无代理服务网格,它具有非侵入.插件化 ...
- 创新 = 颠覆?AI创新如何做大蛋糕
本文分享自华为云社区<创新 = 颠覆?AI创新如何做大蛋糕>,作者: 华为云PaaS服务小智 . 最近随着AI的风靡,各行各业都充斥着近乎疯狂的言论,"AI必将替代一切" ...
- 论文解读丨Zero-Shot场景下的信息结构化提取
摘要:在信息结构化提取领域,前人一般需要基于人工标注的模板来完成信息结构化提取.论文提出一种zero-shot的基于图卷积网络的解决方案,可以解决训练集和测试集来自不同垂直领域的问题. 本文分享自华为 ...
- 据说有人面试栽在了Thread类的stop()方法和interrupt()方法上
摘要:今天就简单的说说Thread类的stop()方法和interrupt()方法到底有啥区别. 本文分享自华为云社区<[高并发]又一个朋友面试栽在了Thread类的stop()方法和inter ...
- 再谈P2P技术:网络拓扑结构、核心技术分析
随着P2P应用的蓬勃发展,作为P2P应用中核心问题的发现技术除了遵循技术本身的逻辑以外,也受到某些技术的发展趋势.需求趋势的深刻影响. P2P协议概述 P2P打破了传统的Client/Server ( ...
- centos8 测地卸载php5.6 与卸载php7
centos8 yum php 默认安装 php7.1.2 我想卸载php7 ,安装php5.6 yum remove php 无法彻底卸载干净.必须强制删除,使用下面命令查看全部php软件包 rpm ...