How to export half a million records from PL/SQL - oracle

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

Related

Oracle RDS Logminer ORA-01291 Missing Log File?

I have an Oracle RDS database I manage, and I need to be able to pull changes from the database for replication to another system. Once connect to the database, below is the commands I run.
EXEC rdsadmin.rdsadmin_util.create_directory(p_directory_name => 'logminerlogs');
set heading off;
set echo off;
set space 0;
set pagesize 0;
set linesize 10000;
set termout off;
set trimout on;
SET TRIMSPOOL ON;
set feedback off;
set newpage NONE;
set define on;
exec dbms_stats.gather_dictionary_stats;
EXEC rdsadmin.rdsadmin_util.alter_supplemental_logging('ADD','PRIMARY KEY');
EXEC rdsadmin.rdsadmin_util.alter_supplemental_logging('ADD','UNIQUE');
BEGIN
dbms_logmnr.add_logfile(
logfilename=> 'rdsdbdata/db/ORCL_A/arch?redolog-99999-9-9999999999.arc');
*** REPEAT for all redo logs ***
END
/
EXECUTE dbms_logmnr.start_logmnr(STARTTIME=>SYSDATE-1,ENDDATE=>SYSDATE,OPTIONS=>DBMS_LOGMNR.DICT_FROM_REDO_LOGS+DBMS_LOGMNR.COMMITTED_DATA_ONLY+DBMS_LOGMNR.PRINT_PRETTY_SQL);
Doing all of that gives me the error
ORA-01291: missing log file
ORA-06512: at "SYS.DBMS_LOGMNR", line 72
ORA-06512: at line 1
I get all of the 'arc' files by querying
SELECT name FROM v$archived_log WHERE first_time > SYSDATE-1 and first_time < systdate ORDER BY 1;
I ran all of these scripts once and I was able to query v$logmnr_contents and get the results I wanted. When I opened another SQLPlus window and ran all of the scripts again, I get the error of Missing Log File mentioned above.
I'm not exactly sure what I am doing wrong here. I have also tried
DBMS_LOGMNR_D.BUILD(dictionary_filename=>'lgmnrdict.ora',dictionary_location=>'LOGMNRLOGS');
where LOGMNRLOGS is a dictionary folder I created previously, and then instead of the redologs when calling add_logfile I point to the 4 onlinelogs .log files that exist on the database and then run command
EXECUTE DBMS_LOGMNR.START_LOGMNR(dictfilename=>'/rdsdbdata/userdirs/o1/logmnrdict.ora');
This will start Logminer, but when I make any changes and commit to the database, when I query v$logmnr_contents those changes are not being returned.
I realize I might be rambling at this point, but I feel like I am very close to the solution; I'm just missing something. Any help is appreciated.
The problem was that for each add_logfile command, I was setting options for dbms_logmnr.new for each rather than for the first and all remaining redologs using dbms_logmnr.addfile.

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

How to spool explain results from Oracle via sqlplus?

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'));

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;

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