Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Como implementar ora_hash (hash semeável que divide qualquer tipo de dados sql em n buckets)


Eu acredito que você está falando de uma função de hash perfeita. A função ORA_HASH do Oracle não é uma função de hash perfeita.

http://en.wikipedia.org/wiki/Perfect_hash_function

O mais próximo possível do que você parece querer é uma matriz associativa. A Oracle tem esses. Comece a brincar com este exemplo:
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;
/  

Nota:um array associativo é construído em uma tabela de hash, o exemplo acima usa fld1 como a chave de hash. Portanto, o acima só funcionará se, como você descreve, hashing perfeito, se e somente se fld1 for um campo exclusivo. Isso é o que o distinto está lá para fazer. Nunca é sempre necessário.