如何通过 sqlplus 假脱机解释来自 Oracle 的结果?

How to spool explain results from Oracle via sqlplus?

我在从查询中提取结果时遇到问题,它或多或少总是显示没有实际成本,将行显示为一个并且没有成本或添加任何内容,而不是实际查询的统计信息。

我正在使用此设置:

current_date=$(date +%Y-%m-%d)
sqlplus -S "Username/password@mydatabase.bag" <<EOF >/output/Testoutput_$current_date.log
set verify off;
set colsep ,
SET AUTOTRACE ON
set headsep off
set pagesize 0
set trimspool on
set termout off
col v_spool noprint new_value v_spool
select 'Spoolfile'||
to_char(sysdate,'yyyy_mm_dd_hh24_mi_ss')||'.csv' v_spool from dual;
set termout on
spool /folder/subfolder/&&v_spool
set lines 12345 pages 12345;
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
SET TIMING OFF
spool off
EXIT
EOF

我的愿望是以某种方式将解释结果假脱机到一个文件中,我需要修改什么来实现这个或者有不同的方法?

如果你在 SQL*Plus:

中尝试 运行
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';

你会注意到它只是输出:

Explained.

您实际上必须 select 使用 DBMS_XPLAN 的 EXPLAIN PLAN 结果 - 这将得到输出并假脱机到您的文件。

-- generate the explain plan
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
  select * from dbms.database1_1 where numberline like '%4214%';
-- actually display the results
select plan_table_output 
  from table(dbms_xplan.display('plan_table',null,'typical'));