A quick Friday post about numbers and GUIDs. Our friendly neighbourhood LLM has cautioned against storing a UUID in an Oracle NUMBER datatype, because of the following logic:

The logic seems sound, because many of us are aware of the “38 decimal digits of precision” that we learn when we are first digging through the Server Concepts guide. But dig a little deeper into the documentation and you’ll also find this
“Oracle guarantees the portability of numbers with precision of up to 20 base-100 digits, which is equivalent to 39 or 40 decimal digits depending on the position of the decimal point”
The seeming contradiction between the statements is because we need to take into account other things such whether a number of a whole number, contains decimals, its sign, all of which might “steal” a few bits from us. But the easiest way to see if we are impacted is with some test data.
SQL> create table c ( sz varchar2(10), x number);
SQL> insert into c values ( '35', rpad('9',35,'9'));
SQL> insert into c values ( '36', rpad('9',36,'9'));
SQL> insert into c values ( '37', rpad('9',37,'9'));
SQL> insert into c values ( '38', rpad('9',38,'9'));
SQL> insert into c values ( '39', rpad('9',39,'9'));
SQL> insert into c values ( '40', rpad('9',40,'9'));
SQL> insert into c values ( '41', rpad('9',41,'9'));
SQL> insert into c values ( '2^128', power(2,128));
SQL> insert into c values ( '2^128-1', power(2,128)-1);
SQL> insert into c values ( 'direct', '340282366920938463463374607431768211456');
SQL> insert into c values ( '38+1dp', rpad('9',38,'9')||'.1');
SQL> insert into c values ('39rand',987654321987654321987654321987654321453);
SQL> insert into c values ('40rand',9876543219876543219876543219876543214531);
SQL> select * from c;
SIZE X
---------- -------------------------------------------
35 99999999999999999999999999999999999 <<< ok
36 999999999999999999999999999999999999 <<< ok
37 9999999999999999999999999999999999999 <<< ok
38 99999999999999999999999999999999999999 <<< ok
39 999999999999999999999999999999999999999 <<< ok
40 9999999999999999999999999999999999999999 <<< ok
41 100000000000000000000000000000000000000000 <<<< lossy
2^128 340282366920938463463374607431768211456 <<< ok
2^128-1 340282366920938463463374607431768211455 <<< ok
direct 340282366920938463463374607431768211456 <<< ok
38+1dp 99999999999999999999999999999999999999 <<<< lossy
39rand 987654321987654321987654321987654321453 <<< ok
40rand 9876543219876543219876543219876543214531 <<< ok
You can see how precision limits vary once we start playing with the decimal place – notice the integer of size 39 was fine, but the “same” size, 38 digits plus 1 decimal place was not.
Most importantly, the largest GUI (2^128) or “xFFFFFFFFFFFFFFFF” managed to squeeze in there without any issue.
When we bring this around to the discussion of using this as a primary key – an obvious choice is to simply store your GUIDs in a RAW datatype. However, that can cause consternation with developers or even the application platform being used, which means storing it as a NUMBER.
Using big random numbers (GUIDs or otherwise) are generally inefficient in terms of storage and index density. For that reason, I’m typically “Team Sequence” in scenarios where the primary key values are never exposed to the outside world (where the predictability of a sequence number might be a security risk).
But if the guess-ability of primary keys is a concern, in 26ai we’ve now got UUID version 7, which gives benefits of excellent index density whilst retaining sufficient randomness.




Leave a Reply