We have a very high parallel application in which some processed keys are constantly written to the Oracle 11g table along with the priority of their processing. In this table there is a primary key (ID field) coming from the sequence. There is a UNIQUE constraint for the KEY field.
ID KEY PRIORITY
-------------------------
1 ABC 0
2 XYZ 5
3 AZW 0
...
100 CNS 7
The table above is inserted at a very high rate, say about ten thousand records per minute. We also have about a hundred parallel consumers who constantly combine the above table in search of work. One of these consumers only needs a key for processing at a time, but it is crucial if not two have the same key, which will have more than one consumer at a time. Processing should occur in PRIORITYfollowed by IDorder.
To satisfy this, the consumer ends up calling a function similar to the one below:
FUNCTION select_key RETURN VARCHAR2
IS
v_key VARCHAR2(64) := NULL;
CURSOR keys IS
SELECT key
FROM my_table
ORDER BY priority, id
FOR UPDATE SKIP LOCKED;
BEGIN
OPEN keys
LOOP
FETCH keys INTO v_key;
EXIT WHEN keys%NOTFOUND;
DELETE FROM my_table WHERE key = v_key;
EXIT WHEN SQL%ROWCOUNT > 0;
END LOOP;
CLOSE keys;
RETURN v_key;
END;
, . SELECT . , .
, ORACLE , , , PRIORITY, ID. , .
, SQL- , , .
, , , , . , .
, , .
.