Oracle:如何估计视图的大小?

Oracle: How to estimate the size of a view?

在 Oracle 中我可以 get the size of a table。我想估计视图的大小(非物化)。可能吗?

我知道视图本身没有任何数据,但我们正在将数据移动到我们的数据湖并想对其进行估算。知道大小我们将能够优化我们的资源并加快流程

您可以使用 EXPLAIN PLAN 来估计通过读取整个视图将 return 编辑的字节数和行数。但请记住,这些数字只是估计值,它们取决于当前的统计数据,对于更复杂的查询,它们的准确性会降低。

例如,在我的系统上,EXPLAIN PLAN 估计稍微复杂的元数据视图将 return 34 MB 和 75,590 行。而实际值大约为 14 MB 和 85,402 行。

命令:

explain plan for select * from dba_objects;

select * from table(dbms_xplan.display);

结果:

Plan hash value: 3423780594
 
------------------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |                   | 75590 |    34M|   134K  (1)| 00:00:06 |
|   1 |  VIEW                                  | DBA_OBJECTS       | 75590 |    34M|   134K  (1)| 00:00:06 |
|   2 |   UNION-ALL                            |                   |       |       |            |          |
...

估计单个查询中的多个视图

通过一些技巧,您可以在单个查询中为多个视图创建估算值。此解决方案需要 Oracle 12.1 或更高版本。 WITH FUNCTION 语法有点奇怪,一些 IDE 难以使用它,因此您可能不得不在末尾使用分号和斜杠。

--Create sample views from data dictionary views. 
create or replace view view1 as select * from all_tables;
create or replace view view2 as select * from all_tab_privs;
create or replace view view3 as select * from all_objects;

--Get the estimated size of each query. The actual values will differ for your database.
with function get_bytes(p_view_name varchar2) return number is
    v_bytes number;
    --(Needed because "explain plan" is technically DML, which normally shouldn't be executed inside a query.)
    pragma autonomous_transaction;
begin
    --Create an explain plan for reading everything from the view.
    execute immediate replace(q'[explain plan set statement_id = '#VIEW_NAME#' for select * from #VIEW_NAME#]', '#VIEW_NAME#', p_view_name);

    --Get the size in bytes.
    --Latest plan information. (In case the explain plan was generated multiple times.)
    select max(bytes)
    into v_bytes
    from
    (
        --Plan information.
        select bytes, timestamp, max(timestamp) over () latest_timestamp
        from plan_table
        where statement_id = p_view_name and id = 0
    )
    where latest_timestamp = timestamp;

    --As part of the AUTONOMOUS_TRANSACTION feature, the function must either commit or rollback.
    rollback;

    return v_bytes;
end;
select view_name, round(get_bytes(view_name) / 1024 / 1024, 1) mb
from user_views
order by mb desc, view_name;
/

结果:

VIEW_NAME            MB
------------ ----------
VIEW3               2.4
VIEW1                .8
VIEW2                .7