ORACLE:WITH CLAUSE&为了更新NOWAIT

时间:2016-10-06 08:41:01

标签: sql oracle join common-table-expression

在简单的连接中,我想限制第一个表的结果。所以我想这样做:

WITH events  AS (SELECT event FROM risk_event WHERE status = 'ABC' AND rownum <= 20) 
SELECT event_id
        FROM events ev, attributes att
        WHERE ev.event_id = att.risk_event_id
        FOR UPDATE NOWAIT

问题在于,由于ORA-02014: cannot select FOR UPDATE from viewrownum<=20',我收到FOR UPDATE NOWAIT个例外。

我知道我也可以用内在条款来做,但我想知道是否有更好的方法?

1 个答案:

答案 0 :(得分:0)

首先尝试选择rowid,然后使用从中选择此rowid的表进行查询

DDL:

create table risk_event as select level as event, mod(level,20) as status from dual connect by level <=10000;

begin
    dbms_stats.gather_table_stats(user,
                                  'risk_event',
                                  cascade          => true,
                                  estimate_percent => null,
                                  method_opt       => 'for all columns size 1');
  end;
/

create table attributes as select * from risk_event;

begin
    dbms_stats.gather_table_stats(user,
                                  'attributes',
                                  cascade          => true,
                                  estimate_percent => null,
                                  method_opt       => 'for all columns size 1');
  end;
/

代码

WITH events AS (SELECT rowid as rd from risk_event WHERE status = 19 AND rownum <= 20)
SELECT ev.*
        FROM risk_event ev, attributes att
        WHERE ev.event = att.event and ev.rowid in(select rd from events) 
        FOR UPDATE NOWAIT

计划

-----------------------------------------------------------------------------------------
| Id  | Operation                        | Name       | Rows  | Bytes | Cost | Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |            |     1 |    23 |   11 | 00:00:01 |
|   1 |   FOR UPDATE                     |            |       |       |      |          |
|   2 |    BUFFER SORT                   |            |       |       |      |          |
| * 3 |     HASH JOIN                    |            |     1 |    23 |   11 | 00:00:01 |
|   4 |      NESTED LOOPS                |            |     1 |    19 |    4 | 00:00:01 |
|   5 |       VIEW                       | VW_NSO_1   |    20 |   240 |    2 | 00:00:01 |
|   6 |        SORT UNIQUE               |            |     1 |   240 |      |          |
|   7 |         VIEW                     |            |    20 |   240 |    2 | 00:00:01 |
| * 8 |          COUNT STOPKEY           |            |       |       |      |          |
| * 9 |           TABLE ACCESS FULL      | RISK_EVENT |    20 |   140 |    2 | 00:00:01 |
|  10 |       TABLE ACCESS BY USER ROWID | RISK_EVENT |     1 |     7 |    1 | 00:00:01 |
|  11 |      TABLE ACCESS FULL           | ATTRIBUTES | 10000 | 40000 |    7 | 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
------------------------------------------
* 3 - access("EV"."EVENT"="ATT"."EVENT")
* 8 - filter(ROWNUM<=20)
* 9 - filter("STATUS"=19)