| The use of SYS_GUID() for Primary Key [message #554600] |
Tue, 15 May 2012 23:14  |
 |
OraFerro
Messages: 139 Registered: July 2011
|
Senior Member |
|
|
Hi All,
I need a design advice.
Currently we implement Primary Key for our tables using the conventional way which is an integer Primary Key filled using a sequence and a trigger. Recently as we have a suggestion from a designer (MS SQL background) to replace all the primary keys with SYS_GUID(). Out Database is small/medium, space is not an issue but performance is extremely important. The designer who advised us says that using SYS_GUID() for Primary Key will have no impact on the performance, easier to implement and maintain, and is more secure.
Can you please give me your opinion as I read some articles that debate both points of view?
Thanks,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: The use of SYS_GUID() for Primary Key [message #554638 is a reply to message #554600] |
Wed, 16 May 2012 01:32   |
John Watson
Messages: 3113 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
With regard to performance, there may be advantages to sys_guid compared to a sequence. First, the values will not be sequential. This can be important, depending on your strategy for partitioning and indexing. Second, concurrent increments to a sequence can cause latch contention, particularly in a RAC. Both these issues are avoidable, but perhaps sys_guid means that you won't have to think about them.
Ask your designer why he has made the recommendation, then maybe we can evaluate the advice.
|
|
|
|
| Re: The use of SYS_GUID() for Primary Key [message #554665 is a reply to message #554638] |
Wed, 16 May 2012 05:37   |
Solomon Yakobson
Messages: 1404 Registered: January 2010
|
Senior Member |
|
|
John Watson wrote on Wed, 16 May 2012 02:32With regard to performance, there may be advantages to sys_guid compared to a sequence.
And disadvantages, if sequence is created with cache (which is default):
SQL> select *
2 from v$version
3 /
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Elapsed: 00:00:01.29
SQL> create table tbl(n number);
Table created.
Elapsed: 00:00:00.29
SQL> create sequence s;
Sequence created.
SQL> insert
2 into tbl
3 select s.nextval
4 from dual
5 connect by level <= 1000000
6 /
1000000 rows created.
Elapsed: 00:00:19.71
SQL> create table tbl_guid(n raw(16))
2 /
Table created.
Elapsed: 00:00:00.07
SQL> insert
2 into tbl_guid
3 select sys_guid()
4 from dual
5 connect by level <= 1000000
6 /
1000000 rows created.
Elapsed: 00:00:40.23
And if we increase cache to 100:
SQL> create sequence s cache 100
2 /
Sequence created.
Elapsed: 00:00:00.00
SQL> insert
2 into tbl
3 select s.nextval
4 from dual
5 connect by level <= 1000000
6 /
1000000 rows created.
Elapsed: 00:00:05.40
SQL>
SY.
|
|
|
|
|
|
|
|
| Re: The use of SYS_GUID() for Primary Key [message #554695 is a reply to message #554686] |
Wed, 16 May 2012 10:33   |
Solomon Yakobson
Messages: 1404 Registered: January 2010
|
Senior Member |
|
|
John Watson wrote on Wed, 16 May 2012 10:32So a sequence is faster - I hadn't expected that. But in a RAC you may still have dreadful problems if the sequence is created with ORDER, which a lot of developers insist on doing.
Yes, RAC is a different story. Then, especially if the sequence is created with ORDER, it is completely dependent on interconnect speed/load.
SY.
|
|
|
|
|
|
|
|
|
|
| Re: The use of SYS_GUID() for Primary Key [message #554724 is a reply to message #554703] |
Wed, 16 May 2012 20:16   |
andrew again
Messages: 2572 Registered: March 2000
|
Senior Member |
|
|
It's worth knowing about platform specific differences in GUIDs.
SQL> select * from v$version order by 1;
BANNER
----------------------------------------------------------------
CORE 10.2.0.1.0 Production
NLSRTL Version 10.2.0.1.0 - Production
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
SQL> select dbms_utility.port_string from dual;
PORT_STRING
--------------------------------------------------------------------------------
IBMPC/WIN_NT-8.1.0
SQL> BEGIN
2 FOR indx IN 1 .. 5
3 LOOP
4 DBMS_OUTPUT.put_line ( SYS_GUID );
5 END LOOP;
6 END;
7 /
8295E8290E5D4A6AAE72ACB0E0099BAE
AC073A39926C46DFB487A111859C2EB8
2D90C80B0EE7431AB07EDBBD4D0393F6
9F3B8D8BCDA249648ECFB3F6707C20AE
A395082816FE4EFB9833B9EA5E163012
PL/SQL procedure successfully completed.
SQL> SQL> select * from v$version order by 1;
BANNER
--------------------------------------------------------------------------------
CORE 11.2.0.1.0 Production
NLSRTL Version 11.2.0.1.0 - Production
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
TNS for Solaris: Version 11.2.0.1.0 - Production
SQL> select dbms_utility.port_string from dual;
PORT_STRING
--------------------------------------------------------------------------------
SVR4-be-64bit-8.1.0
SQL> BEGIN
2 FOR indx IN 1 .. 5
3 LOOP
4 DBMS_OUTPUT.put_line ( SYS_GUID );
5 END LOOP;
6 END;
7 /
C0319B33F73A4F63E0440003BA60882B
C0319B33F73B4F63E0440003BA60882B
C0319B33F73C4F63E0440003BA60882B
C0319B33F73D4F63E0440003BA60882B
C0319B33F73E4F63E0440003BA60882B
PL/SQL procedure successfully completed.
SQL>
I've never been tempted to use GUIDs due to the extra space they use, and the fact that sequence numbers are actually useful sometimes in determining transaction order. It's nice to be able to trace issues using somewhat human readable values. Even if you have tons of space, wider keys will use more space and that's likely to create a little more I/O. Maybe a neglegible consideration to you. If you've ever had to try merging data from 2 schemas into 1, you would likely have encountered duplicate keys. GUIDs would have avoided that issue...
Code snippet borrowed from ==> http://feuerthoughts.blogspot.com/2006/02/watch-out-for-sequential-oracle-guids.html
[Updated on: Wed, 16 May 2012 20:20] Report message to a moderator
|
|
|
|
|
|
| Re: The use of SYS_GUID() for Primary Key [message #554845 is a reply to message #554745] |
Thu, 17 May 2012 20:20   |
andrew again
Messages: 2572 Registered: March 2000
|
Senior Member |
|
|
|
You are right, start and end (commit) of the transaction are different. I'm not sure what you mean about timestamps, but I dont think you're suggesting them as PKs. Neither sequences (assuming for a monent that they were sequential over time) nor timestamps would correspond to the end of the transaction.
|
|
|
|
|
|