使用 EXPLAIN PLAN 命令,Oracle 可以估计索引创建时间和索引大小:
示例模式
--Create a table with 1 million rows.
drop table table1;
create table table1(a number);
insert into table1 select level from dual connect by level <= 1000000;
--Gather statistics.
begin
dbms_stats.gather_table_stats(user, 'table1');
end;
/
--Estimate index creation and size.
explain plan for create index table1_idx on table1(a);
select * from table(dbms_xplan.display);
结果
Plan hash value: 290895522
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | CREATE INDEX STATEMENT | | 1000K| 4882K| 683 (2)| 00:00:10 |
| 1 | INDEX BUILD NON UNIQUE| TABLE1_IDX | | | | |
| 2 | SORT CREATE INDEX | | 1000K| 4882K| | |
| 3 | TABLE ACCESS FULL | TABLE1 | 1000K| 4882K| 254 (5)| 00:00:04 |
-------------------------------------------------------------------------------------
Note
-----
- automatic DOP: skipped because of IO calibrate statistics are missing
- estimated index size: 24M bytes
注意事项
在我的系统上,实际创建时间为2.5秒,与估计值10秒相比有所提升。但如果您只需要一个数量级的估计,那仍然足够好了。准确性取决于准确的表统计信息以及良好的系统统计信息。(但在收集系统统计信息之前要小心,它可能会影响许多执行计划!)您可以通过手动修改sys.aux_stats$来进一步调整设置。这是少数几个允许修改的SYS表之一,尽管您仍然需要小心。