Thursday, April 7, 2016
ORA_HASH by Example
What is ORA_HASH
Per Oracle Documentation:
ORA_HASH is a function that computes a hash value for a given expression.
This function is useful for operations such as analyzing a subset of data and generating a random sample.
When to use ORA_HASH
Consider following scenario.
There is a table of 80,000,000 rows, and you need to return a result, which is say an AVG() of some column.
This would be a huge overhead to scan the whole 80,000,000 rows.
The solution would be to divide the table into buckets, say 20 buckets, each one stands for 5% on the rows, and run AVG() only on one of the buckets.
ORA_HASH Usage
ORA_HASH(expression, max_bucket, seed_value) = 0;
Where:
expression Is any column, value, etc.
max_bucket Is the number of buckets.
Numbering starts at zero.
So for example 99 - would mean 100 buckets.
seed_value: optional.
Default value is zero.
By providing a different seed values to same ORA_HASH function,
the result returned by ORA_HASH would be different each time.
ORA_HASH Example
SELECT AVG(salary)
FROM SALARY_HISTORY
WHERE ORA_HASH(SALARY_HISTORY.seq_id,19)=0;
The ORA_HASH table works on SALARY_HISTORY.seq_id values.
This SQL returns AVG(salary) for entries in the first bucket (=0), out of total 20 buckets (,19).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment