[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这样的数据库软件,必须注意这些细节,导致编码发热复杂性.
--//我自己花了一个晚上,才从中总结出规律,又有点钻牛角尖了。^_^。