我应该使用 Oracle 的 sys_guid() 来生成 guids 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11864575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:01:51  来源:igfitidea点击:

Should I use Oracle's sys_guid() to generate guids?

oracleprimary-keyguid

提问by cmdematos

I have some inherited code that calls SELECT SYS_GUID() FROM DUALeach time an entity is created. This means that for each insertion there are two calls to Oracle, one to get the Guid, and another to insert the data.

我有一些继承的代码,SELECT SYS_GUID() FROM DUAL每次创建实体时都会调用这些代码。这意味着对于每次插入,都会对 Oracle 进行两次调用,一次是获取Guid,另一次是插入数据。

I suppose that there may be a good reason for this, for example - Oracle's Guids may be optimized for high-volume insertions by being sequential and thus they maybe are trying to avoid excessive index tree re-balancing.

我想这可能有一个很好的理由,例如 - Oracle 的 Guids 可以通过顺序进行大容量插入优化,因此他们可能试图避免过度的索引树重新平衡。

Is there a reason to use SYS_GUIDas opposed to building your own Guidon the client?

是否有理由使用SYS_GUID而不是Guid在客户端上构建自己的?

采纳答案by cmdematos

I have found no reason to generate a Guid from Oracle. The round trip between Oracle and the client for every Guid is likely slower than the occasional index rebalancing that occurs is random value inserts.

我发现没有理由从 Oracle 生成 Guid。对于每个 Guid,Oracle 和客户端之间的往返行程可能比随机值插入时偶尔发生的索引重新平衡要慢。

回答by tbone

Why roll your own if you already have it provided to you. Also, you don't need to grab it first and then insert, you can just insert:

如果你已经提供给你,为什么要自己动手。此外,您不需要先抓取它然后插入,您只需插入:

create table my_tab
(
val1 raw(16),
val2 varchar2(100)
);

insert into my_tab(val1, val2) values (sys_guid(), 'Some data');
commit;

You can also use it as a default value for a primary key:

您还可以将其用作主键的默认值:

drop table my_tab;
create table my_tab
(
val1 raw(16) default sys_guid(),
val2 varchar2(100),
primary key(val1)
);

Here there's no need for setting up a before insert trigger to use a sequence (or in most cases even caring about val1 or how its populated in the code).

这里不需要设置 before insert 触发器来使用序列(或者在大多数情况下甚至关心 val1 或其在代码中的填充方式)。

More maintenance for sequences also. Not to mention the portability issues when moving data between systems.

也对序列进行更多维护。更不用说在系统之间移动数据时的可移植性问题了。

But, sequences are more human friendly imo (looking at and using a number is better than a 32 hex version of a raw value, by far). There may be other benefits to sequences, I haven't done any extensive comparisons, you may wish to run some performance tests first.

但是,序列对人类更友好(到目前为止,查看和使用数字比原始值的 32 十六进制版本更好)。序列可能还有其他好处,我没有做过任何广泛的比较,您可能希望先运行一些性能测试。

回答by Adam Hawkes

If your concern is two database calls, you should be able to call SYS_GUID()within your INSERTstatement. You could even use a RETURNINGclause to get the value that Oracle generated, so that you have it in your application for further use.

如果您关心的是两个数据库调用,您应该能够SYS_GUID()在您的INSERT语句中调用。您甚至可以使用RETURNING子句来获取 Oracle 生成的值,以便在您的应用程序中使用它以供进一步使用。

回答by Smart003

SYS_GUID can be used as a default value for a primary key column, which is often more convenient than using a sequence, but note that the values will be more or less random and not sequential.On the plus side, that may reduce contention for hot blocks, but on the minus side your index inserts will be all over the place as well. We generally recommend against this practice.

SYS_GUID 可以用作主键列的默认值,这通常比使用序列更方便,但请注意,这些值或多或少是随机的,而不是顺序的。从好的方面来说,这可能会减少对热块的争用,但从不利的方面来说,您的索引插入也会到处都是。我们通常不推荐这种做法。

for reference click here

参考 点击这里