[20181124]关于降序索引问题2.txt
–//链接:blog.itpub.net/267265/viewspace-2221425/,探讨降序索引中索引的键值。
–//实际上使用函数sys_op_descend.
–//前面已经提到结尾加入ff,为了排序的需要。这样导致chr(0),以及后续的chr(1)字符出现问题。
–//一些细节继续探究:
1.环境:
scott@test01p> @ ver1
port_string version banner con_id
——————– ———- —————————————————————————- ——
ibmpc/win_nt64-9.1.0 12.2.0.1.0 oracle database 12c enterprise edition release 12.2.0.1.0 – 64bit production 0
2.测试:
scott@test01p> with x0
2 as (select ’00’ a, chr (0) b from dual
3 union all
4 select ’01’, chr (1) from dual)
5 select x1.a c10, sys_op_descend (x1.b) c20
6 from x0 x1;
c10 c20
———- ——————–
00 fefeff
01 fefaff
scott@test01p> with x0
2 as (select ’00’ a, chr (0) b from dual
3 union all
4 select ’01’, chr (1) from dual)
5 select x1.a || x2.a c10, sys_op_descend (x1.b || x2.b) c20
6 from x0 x1, x0 x2;
c10 c20
———- ——————–
0000 fefdff
0001 fefcff
0100 fef9ff
0101 fef8ff
–//注意以上编码。继续测试:
with x0
as (select ’00’ a, chr (0) b from dual
union all
select ’01’, chr (1) from dual
union all
select ’02’, chr (2) from dual
)
select x1.a || x2.a c10, sys_op_descend (x1.b || x2.b) c20
from x0 x1, x0 x2;
c10 c20
———- ——————–
0000 fefdff
0001 fefcff
0002 fefbfdff
0100 fef9ff
0101 fef8ff
0102 fef7fdff
0200 fdfefeff
0201 fdfefaff
0202 fdfdff
9 rows selected.
–//不看结尾的ff。可以看出编码的规律
–//字符串0x00,0x0000,0x0001,0x00nn(0xnn>=0x02),0x01,0x0100,0x0101,0x01nn(0xnn>=0x02) 单独编码。画一个表格:
ascii码 编码
———————————————
0x00 fefe
0x0000 fefd
0x0001 fefc
0x00nn(0xnn>=0x02) fefb
0x01 fefa
0x0100 fef9
0x0101 fef8
0x01nn(0xnn>=0x02) fef7
———————————————
–//看一个复杂一点的例子:
with x0
as (select ’00’ a, chr (0) b from dual
union all
select ’01’, chr (1) from dual
union all
select ’02’, chr (2) from dual)
select x1.a || x2.a || x3.a c10
,sys_op_descend (x1.b || x2.b || x3.b) c20
,to_char (ascii (‘z’), ‘fmxx’) || x1.a || x2.a || x3.a c10
,sys_op_descend (‘z’ || x1.b || x2.b || x3.b) c20
from x0 x1, x0 x2, x0 x3;
c10 c20 c10 c20
———- ——————– ———- ——————–
000000 fefdfefeff 7a000000 85fefdfefeff
000001 fefdfefaff 7a000001 85fefdfefaff
000002 fefdfdff 7a000002 85fefdfdff
000100 fefcfefeff 7a000100 85fefcfefeff
000101 fefcfefaff 7a000101 85fefcfefaff
000102 fefcfdff 7a000102 85fefcfdff
000200 fefbfdfefeff 7a000200 85fefbfdfefeff
000201 fefbfdfefaff 7a000201 85fefbfdfefaff
000202 fefbfdfdff 7a000202 85fefbfdfdff
010000 fef9fefeff 7a010000 85fef9fefeff
010001 fef9fefaff 7a010001 85fef9fefaff
010002 fef9fdff 7a010002 85fef9fdff
010100 fef8fefeff 7a010100 85fef8fefeff
010101 fef8fefaff 7a010101 85fef8fefaff
010102 fef8fdff 7a010102 85fef8fdff
010200 fef7fdfefeff 7a010200 85fef7fdfefeff
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
010201 fef7fdfefaff 7a010201 85fef7fdfefaff
010202 fef7fdfdff 7a010202 85fef7fdfdff
020000 fdfefdff 7a020000 85fdfefdff
020001 fdfefcff 7a020001 85fdfefcff
020002 fdfefbfdff 7a020002 85fdfefbfdff
020100 fdfef9ff 7a020100 85fdfef9ff
020101 fdfef8ff 7a020101 85fdfef8ff
020102 fdfef7fdff 7a020102 85fdfef7fdff
020200 fdfdfefeff 7a020200 85fdfdfefeff
020201 fdfdfefaff 7a020201 85fdfdfefaff
020202 fdfdfdff 7a020202 85fdfdfdff
27 rows selected.
–//我仅仅分析下划线记录.遇到00,01需要上面的特殊编码。
–//010200,变成01,02,00 ,0102 对应的就是01nn,02编码,这样就是fef7,fd.剩下的00 对应编码就是 fefe。
–//这样010200 对应编码就是 fef7 fd fefe ff 。
–//7a010200,变成 7a,01,02,00 , 7a与ff异或= 85, 01对应编码要结合后面的02,
–//0102 对应的就是01nn,02编码,这样就是fef7,fd.剩下的00 对应编码就是 fefe。
–//这样7a010200 对应编码就是 85 fef7 fd fefe ff.
3.总结:
–//总之一点就是排序的需要,跳出1个坑(结尾加ff),又进入一个更深的坑chr(0),占用chr(1)的编码,又进入一个更深的坑。
–//结果出现这样特殊的编码,实际上也给一些开发提供一些借鉴,看看oracle技术人员如何实现这些,虽然不知道该如何
–//写这些代码。
–//如果应用程序就不会考虑这样的情况,但是对于oracle这样的数据库软件,必须注意这些细节,导致编码发热复杂性.
–//我自己花了一个晚上,才从中总结出规律,又有点钻牛角尖了。^_^。