1) Consistency Read user-defined function
1@@@@ Read consistency and user-defined functions
Note: two advises
1.. SELECT ... FOR UPDATE [NOWAIT];
2.. SET TRANSACTION READ ONLY; (need undo tbs)
The read consistency model of the Oracle database is simple and clear: once I start a
query, that query will only see data as it existed (was committed in the database) at the
time the query was started. So if my query starts at 9:00 a.m. and runs for an hour, then
even if another user comes along and changes data, my query will not see those changes.
Yet unless you take special precautions with user-defined functions in your queries, it
is quite possible that your query will violate (or, at least, appear to violate) the read
consistency model of the Oracle database. To understand this issue, consider the following
function and the query that calls it:
FUNCTION total_sales (id_in IN account.account_id%TYPE)
RETURN NUMBER
IS
CURSOR tot_cur
IS
SELECT SUM (sales) total
FROM orders
WHERE account_id = id_in
AND TO_CHAR (ordered_on, 'YYYY') = TO_CHAR (SYSDATE, 'YYYY');
--for update nowait
tot_rec tot_cur%ROWTYPE;
BEGIN
OPEN tot_cur;
FETCH tot_cur INTO tot_rec;
RETURN tot_rec.total;
END;
SELECT name, total_sales (account_id)
FROM account
WHERE status = 'ACTIVE';
The account table has 5 million active rows in it (a very successful enterprise!). The
orders table has 20 million rows. I start the query at 10:00 a.m.; it takes about an hour
to complete. At 10:45 a.m., somebody with the proper authority comes along, deletes
all rows from the orders table, and performs a commit. According to the read consistency
model of Oracle, the session running the query should not see all those deleted
rows until the query completes. But the next time the total_sales function executes
from within the query, it finds no order rows and returns NULL????nd will do so until
the query completes.
So if you are executing queries inside functions that are called inside SQL, you need to
be acutely aware of read-consistency issues. If these functions are called in long-running
queries or transactions, you will probably need to issue the following command to
enforce read-consistency between SQL statements in the current transaction:
SET TRANSACTION READ ONLY
In this case, for read consistency to be possible, you need to ensure that you have
sufficient undo tablespace.
referencing "Oracle PL/SQL Program Design 5th Edition"
Consistency Read user-defined function
原创emperor_majesty 博主文章分类:PLSQL ©著作权
文章标签 for update for update nowait 文章分类 数据库
-
hive UDF(User-Defined-Function) 实战
问题 hive无法按照5分钟对日志分组
hive 源码 udf hive java jar