How to spool explain results from Oracle via sqlplus? - oracle

I'm having issues with spooling out results from queries, more or less it shows always no real cost, shows rows as one and no cost or anything added, and not the statistics of the actual query.
I'm using this setup:
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
My wish is to somehow get the explain results spooled to a file, what do I need to modify to achieve this or there is a different approach?

If you try running this in SQL*Plus:
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
You'll notice that it just outputs:
Explained.
You have to actually select the results of the EXPLAIN PLAN using DBMS_XPLAN - that will get output and spooled to your file.
-- 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'));

Related

Export data as XSV from oracle

I want to export some data (i.e. some queries results') from an old oracle database into a file, values separated with a specified character or sequence of characters. A CSV file is an example.
Only integers, varchars (without newline included), dates, other basic values (?) into result set, so no LOBs or another fancy stuff. Just plain data, NO PADDING, NO WRAPPING, fields separated and maybe enclosed.
Practicaly, I'm looking for a simple "SELECT ... INTO OUTFILE FIELDS TERMINATED BY ... [OPTIONALLY] ENCLOSED BY 'char'" from mysql, but for Oracle.
I tried to use sqlplus, but any program should be ok, as long as it runs on linux console and it's not java based :-P.
Unfortunately I cannot use "SET SQLFORMAT csv", seems to be not supported.
So far I've got best results with something like:
printf "set echo off newpage 0 pagesize 0 arraysize 5000 feed off head off trimspool on trimout on\nselect field1 || '|' || field2 from table;" | sqlplus -S database > output_file
Set arraysize 5000 (maximum) gives the best performance, unfortunately wraps the result if lines are longer than 80 chars.
Set linesize 30000 (or even 32767) cancels wrapping, unfortunately the performance becomes horrible. I don't want to specify a maximum linesize for each query (calculating maximum size for each field in result), hoping that performance will rise.
After reading tons of answers I'm still not getting close to a solution that should be both CORRECT and PERFORMANT. My next call will be writing a php file to do the job, but it's a nonsense, IMHO using the default application sqlplus should give the best performances ...
Any ideas ?
I can't tell whether this set of SET commands will help (I use it, usually), but - try it.
set termout off
set trimspool on
set echo off
set verify off
set autoprint off
set serveroutput off
set arraysize 1000
set pagesize 0
set linesize 100
set long 10000
set numwidth 10
set feedback off
set colsep ';'
col empno format 99999
col ename format a10
col sal format 999G990
spool emp.txt
select empno, ename, sal from emp;
spool off
At the bottom, you can see some COL formattings; remove it if you don't need it.
Don't set linesize to that high number (30.000) if you don't really need it.
Also, there's the set colsep ';' which will separate columns by that separator (semi-colon in this example; can be any other character, such as | you mentioned).
In order to speed up spool:
set termout off
set trimspool on
run it on server, not on the client PC
Ok, for everyone that had this problem, here is the answer:
Make sure you have at least oracle client 12.2 installed.
If not, you can extract the files from instantclient-basic-linux.x64-12.2.0.1.0.zip (library files) and from jar archives from sqlplus related directories from linuxx64_12201_client.zip (client/stage/Components/oracle.sqlplus*). Those two zips are available to download for free (registration required) at oracle download site. I didn't want to install that version of the client because it could mixed up with local installed one. It's kind of an ugly hack, but at least no file on system was modified.
From 12.2 onwards the "set markup csv" command is supported, and the command was:
printf "set head off\nset feedback off\nset arraysize 5000\nselect field1, field2 from table;" | LD_LIBRARY_PATH=/path/to/oracle12/lib ORACLE_HOME=/path/to/oracle12 /path/to/oracle12/bin/sqlplus -M "CSV ON" -S database
adjust the arraysize variable (5000 is maximum and fastest, but pay attention to the memory), choose the "SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]" parameters and enjoy

Set heading off is not working in SQLcl when set sqlformat csv

I am generating a csv output using SQLcl.
set sqlformat csv
set heading off
select * from hr.employees where rownum < 10;
"EMPLOYEE_ID","FIRST_NAME","LAST_NAME","EMAIL","PHONE_NUMBER","HIRE_DATE","JOB_ID","SALARY","COMMISSION_PCT","MANAGER_ID","DEPARTMENT_ID"
100,"Steven","King","SKING","515.123.4567",17-JUN-03,"AD_PRES",24000,,,90
101,"Neena","Kochhar","NKOCHHAR","515.123.4568",21-SEP-05,"AD_VP",17000,,100,90
102,"Lex","De Haan","LDEHAAN","515.123.4569",13-JAN-01,"AD_VP",17000,,100,90
103,"Alexander","Hunold","AHUNOLD","590.423.4567",03-JAN-06,"IT_PROG",9000,,102,60
104,"Bruce","Ernst","BERNST","590.423.4568",21-MAY-07,"IT_PROG",6000,,103,60
But I am getting the heading which I don't want. I imagine set heading off should turn off the heading (as it does in SQLPlus) why it's not working in SQLcl. If I clear the sql formatting (set sqlformat) then heading off works. Is it a bug in SQLcl?
SET PAGESIZE 0
lets you run without pagination.
set heading off
should work, but was broken. The next release will have it fixed...like this
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
BARRY#orcl☘ >set sqlformat csv
BARRY#orcl☘ >set heading off
BARRY#orcl☘ >select * from demo;
1,"Barry"
2,"Lisa"
3,"Rebecca"
4,"Katie-Ellen"
BARRY#orcl☘ >
Using the current version of sqlcl (as of 6/30/2017), with both 11g and 12c databases, works fine:
alter session set NLS_DATE_FORMAT='DD-MON-YYYY';
set feedback off
set sqlformat csv
spool <spool loc>
SELECT <column list> FROM <table list> WHERE <where clause
ORDER BY <order by clause>;
spool off;
You don't want to see the headings in the results?
In SQLPLUS you can use
set pagesize 0

Heavy file generated on spooling in unix

I run this code from a unix file.
Please Find Below my code
set colsep ,
set heading off
set pagesize 0
set feedback off
set linesize 32767
set trimspool on
set tab off
spool My_File
select * from my_table; /* it has 45 records*/
spool off;
Once I run this code, I have my output file generated, but the columns are seperated with very big spaces and even the records are seperated with big spaces. As a result I get a very huge file.
Is there any workaround for this ... Thanks in advance..
What i do in these situations is not to use colsep and and do the concatenation myself.
set heading off
set pagesize 0
set feedback off
set linesize 32767
set trimspool on
set tab off
spool My_File
select column1||','||column2||','||column3.... from my_table;
spool off;

How to export half a million records from PL/SQL

I've a table with around 500,000 records. I need all records to be exported in excel. When I query I'm not able to get all as I was said as Out Of memory
Table doesn't have any primary key/Index.
Is there any way to extract?
it would be very easy in to write file output form sqlplus .
mycsv.sql:
SET DEFINE OFF
SET ECHO OFF
SET SERVEROUTPUT OFF
SET TERMOUT OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 10000
SET ARRAYSIZE 5000
REM SET HEAD OFF
SET LINE 500
spool /tmp/mycsvfile.csv;
select * from MY_table;
spool off;
exit;
and from Linux prompt you can run like
$> sqlplus username/password #/tmp/mycsv.sql

blanks lines in between data rows show up when using SPOOL in SQLPlus

I'm trying to spool a large query in sqlplus but for some reason, I see 2 blank lines every 558 rows to be exact.
I spool as such:
SET TERMOUT OFF
SET ECHO OFF
SET LINES 1000
SET FEEDBACK off
SET HEADING OFF
SET ARRAYSIZE 10000
SET NEWPAGE NONE
SET PAGESIZE 0
SET TRIMSPOOL ON
Spool D:\IPORT15.csv
select query
Spool OFF;
EXIT
I thought SET NEWPAGE was supposed to take care of that??
I was able to resolve this by setting: "SET PAGES 0"

Resources