如何实现ora_hash(将任何sql数据类型划分为n个存储桶的可种子哈希) [英] How to implement ora_hash (seedable hash that divides any sql datatype into n buckets)
问题描述
我看过有关哈希表的Wikipedia文章,但是似乎没有说什么如何实现一个哈希表,该哈希表可以将任意SQL数据类型的条目平均分配到n个存储桶中.
I've looked at the wikipedia article on Hash tables but it doesn't seem to say anything about how to implement a hash table that can distribute entries of arbitrary SQL datatype evenly into n buckets.
有人可以指出我有关该主题的文档或现有源代码的方向吗?
Can anyone point me in the direction of documentation or existing source code on this topic?
推荐答案
我相信您正在谈论的是完美的哈希函数. Oracle的ORA_HASH函数不是完美的哈希函数.
I believe you are talking about a perfect hash function. Oracle's ORA_HASH function is not a perfect hash function.
http://en.wikipedia.org/wiki/Perfect_hash_function
尽可能接近您想要的是一个关联数组.甲骨文拥有那些. 开始玩这个例子:
As close as you will get to what you seem to want is an associative array. Oracle has those. Start playing with this example:
set serverout on size 10000
DECLARE
cursor foo
is
select distinct fld1,fld2,fld9 from sometable;
type t is table of foo.%ROWTYPE
index by varchar2; -- change the index to an int if you want
myarray t; -- myarray is a table of records -- whatever foo returns
BEGIN
for x in foo
loop
-- index using the first column of the fetched row "fld1":
myarray(x.fld1)=x; -- assign the rowtype to the table of records.
end loop;
END;
/
注意:关联数组建立在哈希表上,上面的示例使用fld1作为哈希键. 因此,仅当且仅当fld1是唯一字段时,上述内容才适用于您所描述的完美哈希.这就是其中的独特之处.从来都不总是必需的.
Note: an associative array is built on a hashtable, the above example uses fld1 as the hash key. So the above will only work if as you describe, perfect hashing, if and only if fld1 is a unique field. That is what the distinct in in there to do. It is never always required.
这篇关于如何实现ora_hash(将任何sql数据类型划分为n个存储桶的可种子哈希)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!