Oracle SQL Material
Oracle SQL Material
Oracle SQL Material
PL-SQL Functions
Comparison Operators
Select Operators
Single Row Functions for Numbers, Chars and Dates
Conversion Functions
Miscellaneous Single Row Functions
Aggregate Functions
Analytical Functions
Object Reference Functions
Date Format Models
Date Prefixes and Suffixes
Number Format Models
Comparison Operators
BETWEEN x True if greater than or equal to x and less than or equal to y (can be reversed in
AND y meaning with NOT)
EXISTS True if the subquery returns at least one row (can be reversed in meaning with NOT)
LIKE pattern 'True if expression or subquery matches pattern. '%' matches any sequence of
[ESCAPE 'c'] characters, '_' matches any single character. If ESCAPE is used, the character 'c' causes
the character following to be taken literally (can be reversed in meaning with NOT).
IS NULL TRUE if the value is NULL (can be reversed in meaning with NOT)
Select Operators
Number Functions
Character Functions
SUBSTRB - bytes
SUBSTRC - unicode
INSTRC - unicode
5
also:
LENGTHB - bytes
LENGTHC - unicode
Date Functions
Conversion Functions
Aggregate Functions
All of the aggregate functions described below can have an analytical clause appended to them using the
OVER (analytical_clause) syntax. For space considerations, we've omitted this from the Function column.
Analytical Functions
All of the aggregate functions described above can also have analytic functionality, using the OVER
(analytical_clause) syntax. For space considerations, we've declined to list them twice. Note that you
cannot nest analytic functions.
SYEAR
Y Final one, two, or three digits of the year
YY
YYY
• Ref Cursor
• BULK COLLECT INTO
The cursor FOR loop provides an elegant, simple syntax to to iterate over a result set. To underscore the
advantages of cursor FOR loops, consider the following PL/SQL block which uses a basic loop.
SET SERVEROUTPUT ON
DECLARE
-- EMP_CURSOR will retrieve all columns and all rows from the EMP table
CURSOR emp_cursor IS
SELECT *
FROM emp;
emp_record emp_cursor%ROWTYPE;
BEGIN
17
OPEN emp_cursor;
LOOP
--Advance the pointer in the result set, assign row values to EMP_RECORD
FETCH emp_cursor INTO emp_record;
--Test to see if no more results
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' [' ||emp_record.empno||']');
END LOOP;
CLOSE emp_cursor;
END;
/
DECLARE
-- EMP_CURSOR will retrieve all columns and all rows from the EMP table
CURSOR emp_cursor IS
SELECT *
FROM emp;
BEGIN
FOR emp_record IN emp_cursor LOOP
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' ['||emp_record.empno||']');
END LOOP;
END;
/
• You can use a cursor for loop without a declared cursor by including a query in the FOR statement.
This can enable very compact code.
BEGIN
FOR emp_record IN (SELECT * FROM emp) LOOP
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' ['||emp_record.empno||']');
END LOOP;
END;
/
• While you can use EXIT statement within a FOR cursor loop, you should not use a cursor FOR loop
if you may need to exit the LOOP prematurely. Use a basic or WHILE loop instead.
• Cursors can use variables to adjust which rows they select when opened. Instead of hard-coding a
value into the WHERE clause of a query, you can use a variable as a placeholder for a literal value.
The variable placeholder will substituted with the value of the variable when the cursor is opened.
This makes a query more flexible.
DECLARE
v_deptno NUMBER;
v_job VARCHAR2(15);
v_sum_sal NUMBER;
/* Since v_deptno and v_job are declared above, they are in scope,
* and can be referenced in the cursor body. They will be used as
* placeholders until the cursor is opened, at which
18
OPEN emp_stats_cursor;
/* When the cursor is opened, the values of the PL/SQL
* variables are bound into the query.
* In this example, the cursor would return the
* result set using the following query:
SELECT SUM(sal) sum_sal
FROM emp
WHERE deptno=10 --current value of v_deptno is 10
AND job='MANAGER'; --current value of v_job is 'MANAGER'
*/
v_deptno:=30;
v_job:='SALESMAN';
OPEN emp_stats_cursor;
/* In this example, the cursor would
* return the result set using the following query:
DECLARE
v_sum_sal NUMBER;
BEGIN
OPEN emp_stats_cursor(10,'MANAGER');
/* When the cursor is opened, the values of the parameters
* are bound into the query. In other words, the actual parameters
* (values) will replace the formal parameters (placeholders).
* In this example, the cursor would return the result set using
* the following query:
OPEN emp_stats_cursor(30,'SALESMAN');
/* In this example, the cursor would return the result set
* using the following query:
DECLARE
v_sum_sal NUMBER;
CURSOR emp_stats_cursor(cp_deptno NUMBER, cp_job VARCHAR2) IS
SELECT SUM(sal) sum_sal
FROM emp
WHERE deptno=cp_deptno
AND job=cp_job;
BEGIN
FOR dept_job_rec IN (SELECT DISTINCT deptno,job FROM emp) LOOP
OPEN emp_stats_cursor(dept_job_rec.deptno, dept_job_rec.job);
FETCH emp_stats_cursor INTO v_sum_sal;
CLOSE emp_stats_cursor;
DBMS_OUTPUT.PUT_LINE(dept_job_rec.deptno ||' : '||dept_job_rec.job||' : '||v_sum_sal);
END LOOP;
END;
/
Ref Cursor
Ref Cursor is THE method to returns result sets to client applications (like C, VB, etc).
You cannot define ref cursors outside of a procedure or function in a package specification or body. Ref
cursors can only be processed in the defining procedure or returned to a client application. Also, a ref
cursor can be passed from subroutine to subroutine and a cursor cannot. To share a static cursor like that,
you would have to define it globally in a package specification or body. Because using global variables is
not a very good coding practice in general, Ref cursors can be used to share a cursor in PL/SQL
without having global variables getting into the mix.
Last, using static cursors—with static SQL (and not using a ref cursor) —is much more efficient than using
ref cursors, and the use of ref cursors should be limited to
In short, you want to use static SQL first and use a ref cursor only when you absolutely have to.An
Example of Ref cursor is here:
Cursor variables are cursors opened by a pl/sql routine and fetched from by another application or pl/sql
routine (in 7.3 pl/sql routines can fetch from cursor variables as well as open them). The cursor variables
are opened with the privelegs of the owner of the procedure and behave just like they were completely
contained within the pl/sql routine. It uses the inputs to decide what database it will run a query on. Here
is an example:
Introduced in Oracle8i, BULK COLLECT allows you to retrieve multiple rows of data directly into
PL/SQL Collections. It will raise NO_DATA_FOUND if it doesn't find any rows, but it certainly doesn't
raise TOO_MANY_ROWS if it finds more than one!
More information can be obtained HERE.
DECLARE
TYPE title_aat IS TABLE OF magazine.title%TYPE
INDEX BY BINARY_INTEGER;
l_titles title_aat;
BEGIN
SELECT title
BULK COLLECT INTO l_titles
FROM magazine;
END;
My advice regarding the kinds of cursors to use when fetching data from Oracle in PL/SQL programs it to
whenever possible, use BULK COLLECT—it offers dramatic performance gains. In Oracle9i Release 2, you
can even use BULK COLLECT to fetch multiple rows directly into a collection of records. Of course, when
you want/need to fetch just a single row, BULK COLLECT doesn't make sense.
22
• Concepts
• Working with explicit cursors
• Cursor Attributes
• Records and %ROWTYPE
Oracle uses cursors to process all SQL statements. From SQL*Plus, you issue a command and Oracle
takes care of creating a cursor and processing the command. These types of cursors are called implicit
cursors, because you (the user) cannot not name or control the cursor directly. In PL/SQL you also use
implicit cursors for DML statements and single select statements.
Implicit (static) cursor: commonly refers to the good old SELECT INTO, in which Oracle implicitly opens,
executes and closes the cursor for you, depositing any selected values of a single row INTO program data
structures.
The single select is a simple solution, but insufficient to solve the following problems.
• You may need to process more general result sets which return more or less than one row
• You may need to process rows in a specific order
• You may need to control the execution of your program depending on the result set.
Explicit cursors work the same way implicit cursors work, but you control the execution explicitly.
DECLARE
v_ename VARCHAR2(12);
v_empno NUMBER:=7839;
23
CURSOR ename_cursor IS
SELECT ename
FROM emp
WHERE empno=v_empno;
BEGIN
OPEN ename_cursor;
FETCH ename_cursor INTO v_ename;
CLOSE ename_cursor;
END;
/
DECLARE
...
Oracle allocates memory and processes the query
CURSOR ename_cursor IS
SELECT ename
FROM emp
WHERE empno=v_empno;
Oracle binds variables, and executes query identifying
BEGIN
the active set.
OPEN ename_cursor;
Cursor Attributes
Use cursor attributes to determine whether the row was found and what number the row is.
Cursor Attributes
Attribute Description
cur%ISOPEN Returns TRUE if cursor is open.
cur%NOTFOUND Returns FALSE if the last FETCH found a row.
Returns TRUE if the last FETCH found a row.. (Logical inverse of
cur%FOUND
%NOTFOUND).
cur%ROWCOUNT Returns the number of rows modified by the DML statement.
SQL%BULK_ROWCOUNT Returns the number of rows processed for each execution of the bulk DML
operation.
DECLARE
v_empno emp.empno%TYPE;
v_ename emp.ename%TYPE;
24
CURSOR emp_cursor IS
SELECT empno, ename
FROM emp;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_empno, v_ename;
EXIT WHEN emp_cursor%ROWCOUNT>10 or emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(INITCAP(v_ename)||' ['||v_empno||']');
END LOOP;
CLOSE emp_cursor;
END;
/
Instead of fetching values into a collection of variables, you could fetch the entire row into a record like so.
DECLARE
CURSOR emp_cursor IS
SELECT empno, ename, sal, job, deptno
FROM emp
WHERE deptno=30;
-- This creates a record named emp_row
-- based on the structure of the cursor emp_cur
emp_row emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO emp_row;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_row.ename||' ['
||emp_row.empno||'] makes '||TO_CHAR(emp_row.sal*12,'$99,990.00'));
END LOOP;
END;
/
You can reference the fields of a record using the syntax record_name.field_name.
In addition to basing a record on a cursor, you can also define records based on tables like so.
DECLARE
CURSOR emp_cursor IS
SELECT *
FROM emp
WHERE deptno=30;
DECIMAL 16 40 digit
INTEGER 16 40 digits
INTEGER(a) varies a digits
CHAR(a) a a=(1-255)
VARCHAR(a) varies 1 - 255
VARCHAR2(a) varies 1 - 2000
* Long datatypes are discouraged in Oracle 8. Note that are long and blob
datatypes are incompatible.
VARCHAR
VARCHAR2
Creating a table
PCTFREE = Amount of space to leave in block during insert operations. Allows room for records to grow
within the same area.
PCUSED = The threshold at which the block is placed back on the free block list.
INITIAL/NEXT = The initial disk allocated, and the next extent size.
LOGGING = Indicates whether operations are written to the redo logs.
Creating constraints
Creating triggers
The example below illustrates versioning of the EMP_RESUME table, which contains a blob field.
DECLARE
VER1 NUMBER ;
EBLOB BLOB ;
VBLOB BLOB ;
BEGIN
EBLOB := EMPTY_BLOB();
VBLOB := :NEW.RESUME ;
SELECT RESUME
INTO EBLOB
FROM VEMP_RES
WHERE EMP_ID =:NEW.EMP_ID AND
VER_NO = VER1
29
FOR UPDATE ;
UPDATE VEMP_RES
SET RESUME = VBLOB
WHERE EMP_ID =:NEW.EMP_ID AND
VER_NO = VER1 ;
END;
Renaming a table
-- Synonym Creation
-- Database Link
CREATE DATABASE LINK ARCHIVE_DATA CONNECT TO USER5 IDENTIFIED BY TIGER USING 'SERVER5' ;
/* user within this system can now reference tables using ARCHIVE_DATA.tablename */
Moving a table
Using SQL-Plus
30
SQL-Plus is a query / command line utility which has some powerful formatting capabilities.
Getting Started
; Command line terminator
/ Execute the current batch of commands
SET SERVEROUTPUT ON Allow messages from PL-SQL to be displayed
SHOW ERRORS Show errors from last batch
EDIT Run editor, and load buffer
CLEAR BUFFER Clear buffer commands
& Prompt for value
@ Run commands in @filename
BEGIN
INV_COUNT := 0;
SELECT COUNT(*)
INTO INV_COUNT
FROM INVOICE
WHERE INV_ID = INV_ID1 ;
END IF ;
END ;
Displaying output
31
All SELECT statements in PL-SQL must have an INTO clause; therefore another method is needed to
display output to the console.
DBMS_OUTPUT.PUT_LINE('TEST OUTPUT');
salary := 24000;
dbms_output.put_line(salary);
Output variables
Output variables are used to return data to another procedure, or to an external application which has
invoked the stored procedure.
...
Arrays and structures are implemented thought the use of "tables" and "records" in PL-SQL.
/* ARRAY DECLARATION */
NAME_TABLE NAME_TABLE_TYPE ;
/* ARRAY SUBSCRIPTING */
I := I + 1;
NAME_TABLE(I) := 'JSMITH';
Conditionals
32
Sample formats of conditional branching are given below:
IF <condition> THEN
<statements> ;
END IF;
Looping
I = I + 1;
END LOOP;
Cursors
The first example depicts dbase-style row processing ; the second a more traditional "fetch" approach.
CURSOR INVOICE_CUR IS
SELECT INV_ID, INV_DT FROM INVOICE ;
INVOICE_REC INVOICE_REC_TYPE ;
BEGIN
DBMS_OUTPUT.PUT_LINE('INVOICE DELETETED:');
DBMS_OUTPUT.PUT_LINE(INV_REC.INV_ID);
END
33
END LOOP;
END;
/* ======================================= */
RPT_LINE VARCHAR2(1900);
RPT_PART VARCHAR2(1900);
RPT_LEAD VARCHAR2(200);
GLIB_ID1 NUMBER ;
GLIB_ID2 VARCHAR(12);
ORIG_LOC_CD1 VARCHAR2(12);
AUTHOR_ID1 VARCHAR2(30);
CONTRIBUTORS1 VARCHAR2(80);
TOPIC1 VARCHAR2(80);
NBR_ACCESS1 NUMBER ;
NBR_ACCESS2 VARCHAR2(12);
TOT_EXPIRED1 NUMBER ;
TOT_EXPIRED2 VARCHAR2(12);
COUNT1 NUMBER ;
RPT_BODY_PART LONG ;
CURSOR CUR1 IS
SELECT GLIB_ID, ORIG_LOC_CD, AUTHOR_ID, CONTRIBUTORS, TOPIC, NBR_ACCESS
FROM GEN_DOC
WHERE EXPIRE_DT < (SYSDATE + 30)
ORDER BY ORIG_LOC_CD, GLIB_ID ;
BEGIN
SELECT COUNT(*)
INTO TOT_EXPIRED1
FROM GEN_DOC
WHERE STAT_CD='90';
TOT_EXPIRED2 := TO_CHAR(TOT_EXPIRED1);
OPEN CUR1;
RPT_BODY := UTL_RAW.CAST_TO_RAW(RPT_LINE);
RPT_LINE := '';
LOOP
COUNT1 := COUNT1 + 1;
EXIT WHEN (COUNT1 > 500);
EXIT WHEN (UTL_RAW.LENGTH(RPT_BODY) > 32000);
RPT_PART := '<TR><TD>';
NBR_ACCESS2 := TO_CHAR(NBR_ACCESS1);
RPT_PART := CONCAT(RPT_PART,NBR_ACCESS2);
RPT_PART := CONCAT(RPT_PART,'</TD><TD>');
GLIB_ID2 := TO_CHAR(GLIB_ID1);
RPT_BODY_PART := UTL_RAW.CAST_TO_RAW(RPT_LINE);
RPT_BODY := UTL_RAW.CONCAT(RPT_BODY,RPT_BODY_PART);
-- RPT_BODY := RPT_BODY || RPT_LINE;
RPT_LINE := '';
END LOOP;
CLOSE CUR1 ;
RPT_LINE := '</TABLE></BODY></HTML>';
RPT_BODY_PART := UTL_RAW.CAST_TO_RAW(RPT_LINE);
RPT_BODY := UTL_RAW.CONCAT(RPT_BODY, RPT_BODY_PART);
EXCEPTION
WHEN OTHERS THEN
BEGIN
DBMS_OUTPUT.PUT_LINE('ERROR: PROC_DOCSTAT_RPT');
GLIB_ID1 := UTL_RAW.LENGTH(RPT_BODY);
DBMS_OUTPUT.PUT_LINE(GLIB_ID1);
END;
END;
35
Packages
A package is a construct which bounds related procedures and functions together. Variables declared in
the declaration section of a package can be shared among the procedures/functions in the body of the
package.
/* package */
END INVPACK;
/* package body */
COUNT1 NUMBER;
SELECT COUNT(*)
INTO COUNT1
FROM INVOICE
WHERE SALES_REP_ID = SALESREP ;
RETURN COUNT1 ;
END COUNTINV;
END PURGEINV;
COUNT1 := 0 ;
END INVPACK;
Exception Handling
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('End of data !!);
WHEN OTHERS THEN
BEGIN
DBMS_OUTPUT.PUT_LINE('OTHER CONDITION OCCURRED !');
END;
Using Blobs
Blob variables require special handling in PL-SQL. When reading from a file to a blob, only one statement
is required. When reading from a blob field to a PL-SQL variable, only 32k blocks can be processed, thus
necessitating a loop construct.
/*---------------------------------------*/
/* Read a blob from a file, and write */
/* it to the database. */
/*---------------------------------------*/
file1 bfile ;
lblob blob ;
len int ;
e_blob blob ;
begin
file1 := bfilename('IMAGE_DIR',fname1);
e_blob := empty_blob();
dbms_lob.fileopen(file1);
len := dbms_lob.getlength(file1) ;
dbms_lob.loadfromfile(lblob,file1,len);
dbms_lob.filecloseall();
commit;
exception
when others then
begin
dbms_output.put_line(sqlerrm);
dbms_lob.filecloseall();
commit;
end;
end ;
/
call proc_imp_jpg('jada.jpg',101)
/
/*-----------------------------*/
/* determine the length of */
/* a blob field */
/* by reading it */
/*-----------------------------*/
CREATE OR REPLACE PROCEDURE PROC_BLOB_LENGTH
(PART_ID1 NUMBER)
IS
SRC_LOB BLOB;
BUFFER RAW(100);
AMT BINARY_INTEGER := 100;
POS INTEGER := 1;
COUNTER INTEGER :=0;
BEGIN
SELECT PART_PHOTO INTO SCR_LOB
FROM PARTS
WHERE PART_ID=PART_ID1 ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
38
DBMS_OUTPUT.PUT_LINE('End of data, total bytes:');
DBMS_OUTPUT.PUT_LINE(POS);
END;
X := UTL_RAW.LENGTH(RPT_BODY) ;
Y := DBMS_LOB.GETLENGTH(LONG_RAW_COL);
Managing Context is an arduous task. The best approach is to use the command line utility as much as
possible. Below is a code sample which creates a policy (a policy is a construct which informs context
which column on what table to scan during a search operation). The next example illustrates how to
perform a search, and stored the result keys in a table.
/* create a policy, on the emp_resume table */
ctx_svc.clear_all_errors;
ctx_ddl.create_policy(
POLICY_NAME => 'EMP_RES_POLICY',
COLSPEC => 'EMP_RES.RESUME',
SOURCE_POLICY => 'CTXSYS.DEFAULT_POLICY',
DESCRIPTION => 'EMP Policy',
TEXTKEY => 'EMP_ID',
DSTORE_PREF => 'CTXSYS.DEFAULT_DIRECT_DATASTORE',
FILTER_PREF => 'CTXSYS.HTML_FILTER',
LEXER_PREF => 'CTXSYS.DEFAULT_LEXER'
);
dbms_output.put_line('Indexing Policy ...');
ctx_ddl.create_index('EMP_POLICY');
POLICY1 := 'EMP_POLICY';
TABLE1 := 'EMP_CTX_RESULTS';
ID1 := 100 ;
QUERY1 := 'COBOL|FORTRAN';
Sometimes it is necessary to delay the execution of commands, for debugging, or batch runs.
/* Sleep 60 seconds */
execute dbms_lock.sleep(60);
execute dbms_lock.sleep(3600);
Date Manipulation
Version information
SELECT FILE_NAME,TABLESPACE_NAME,BYTES,AUTOEXTENSIBLE,
MAXBYTES,INCREMENT_BY
FROM DBA_DATA_FILES;
SELECT SEGMENT_NAME,TABLESPACE_NAME,EXTENTS,BLOCKS
FROM DBA_SEGMENTS;
Extent information
SELECT segment_name,segment_type,
max_extents, extents
FROM dba_segments
WHERE extents+5 > max_extents
AND segment_type<>'CACHE';
42
List segments reaching extent limits
SELECT s.segment_name,s.segment_type,s.tablespace_name,s.next_extent
FROM dba_segments s
WHERE NOT EXISTS (SELECT 1
FROM dba_free_space f
WHERE s.tablespace_name=f.tablespace_name
HAVING max(f.bytes) > s.next_extent);
List table blocks, empty blocks, extent count, and chain block count
SELECT SEGMENT_NAME,TABLESPACE_NAME,OWNER,STATUS
FROM DBA_ROLLBACK_SEGS;
SELECT index_name,table_name,uniqueness
FROM dba_indexes
WHERE index_name in
(SELECT constraint_name
FROM dba_constraints
WHERE table_name = TNAME
AND constraint_type in ('P','U')) ;
EXEC DBMS_STATS.GATHER_TABLE_STATS('SCHEMA1','COMPANY');
EXEC DBMS_STATS.GATHER_SCHEMA_STATS('SCHEMA1');
set pagesize 0;
Constraint columns
select
b.uniqueness, a.index_name, a.table_name, a.column_name
from user_ind_columns a, user_indexes b
where a.index_name=b.index_name
order by a.table_name, a.index_name, a.column_position;
Trigger listing
Glossary:
pins = # of time an item in the library cache was executed
reloads = # of library cache misses on execution
Goal:
get hitratio to be less than 1
Tuning parm:
adjust SHARED_POOL_SIZE in the initxx.ora file, increasing by small increments
Glossary:
gets = # of requests for the item
getmisses = # of requests for items in cache which missed
Goal:
get rcratio to be less than 1
Tuning parm:
adjust SHARED_POOL_SIZE in the initxx.ora file, increasing by small increments
Tuning: sorts
Goal:
Increase number of memory sorts vs disk sorts
Tuning parm:
adjust SORT_AREA_SIZE in the initxx.ora file, increasing by small increments
An informational query.
Goal:
Try to avoid increasing 'undo header' counts
Tuning method:
Create more rollback segments, try to reduce counts
SELECT CLASS,COUNT
FROM V$WAITSTAT
WHERE CLASS LIKE '%UNDO%' ;
Killing Sessions
Runaway processes can be killed on the UNIX side, or within server manager.
46
Recovering an Instance
An incomplete recovery is the only option if backups are run periodically on a cold instance. Complete
recovery is possible if archive logging is enabled, and backups are run while the database is active.
/* incomplete recovery #1 */
svrmgrl> connect;
svrmgrl> startup;
/* incomplete recovery #2 */
svrmgrl> connect;
svrmgrl> startup mount;
svrmgrl> recover database until time '2002-03-04:15:00:00' ;
svrmgrl> alter database open resetlogs;
svrmgrl> connect;
svrmgrl> startup mount;
svrmgrl> recover database ;
< or >
svrmgrl> recover datafile '/data4/ts03.dbf'
svrmgrl> startup open;
svrmgrl> connect;
svrmgrl> startup mount;
svrmgrl> set autorecovery on ;
svrmgrl> recover tablespace ts03 ;
< or >
svrmgrl> recover datafile 4 ;
svrmgrl> startup open;
These queries list the status / locations of the redo log files.
This tool loops a specified number of times, displaying memory usage along with user process counts for a
specific username.
--=================================================
--
-- proc_ora_monitor
--
-- parm1: username to count
-- parm2: number of loops, 5 sec duration
--
--
--=================================================
set serveroutput on ;
i number ;
usercount1 number ;
memory1 number ;
date1 varchar(20) ;
msg varchar(99) ;
begin
i := 0 ;
select count(1)
into usercount1
from sys.v_$session
where username = user1 ;
dbms_output.put_line(msg) ;
dbms_lock.sleep(5) ;
i := i + 1 ;
end loop ;
end;
/
show errors ;
execute proc_ora_monitor('SILVERUSER',2) ;
exit
Connection Errors
-------------------------------------------------------
ORA-01034: ORACLE not available
-------------------------------------------------------
TNS-12564: TNS:connection refused
-------------------------------------------------------
TNS-12530: Unable to start a dedicated server process
50
-------------------------------------------------------
Connection errors can crop up out of nowhere ; the error message tend to be vague, and not useful at all.
Here's a plan of attack which will solve many connection issues. Try each step, and proceed if the problem
persists.
1) Check your environment ; verify the variables depicted below are set.
( NT: check the registry )
The example below details a Solaris/CSH environment.
Note the TWO_TASK setting ...
tnsping db22
Solaris:
1) kill the process, running the tnslsnr binary
2) nohup $ORACLE_HOME/bin/tnslsnr start &
NT:
1) restart the service, in the control panel
4) SQL-Plus / ServerMgr
Try switching the oracle user to the Bourne or Csh shell ; make a script for SQL-Plus
as follow:
#!/usr/bin/csh
# OR
#sqlplus $1@$ORACLE_SID
Also verify the oracle user owns the oracle directory tree.
6) Check the pfile, verify the settings detailed below. For this example,
the machine should have at least 512mb of memory, to handle the OS and
other processes.
processes = 65
sessions = 65
8) Verify the Oracle version, SQLNet version, and patched OS are all compatible.
Below is sample syntax for explain plan ( getting output from the optimizer )
/*----------------------------*/
exit
/
Oracle 9i Articles
Oracle9i: New Features For Administrators Exam Tips - Some tips on what to expect when upgrading your
OCP to Oracle9i.
ANSI ISO SQL Support In Oracle9i - Check out the new SQL enhancements to bring Oracle in line with the
ANSI/ISO SQL: 1999 standards.
Automatic Segment Free Space Management - Reduce segment header contention and wasted space
within blocks by switching from using FreeLists to Automatic Segment Free Space Management.
Automatic Undo Management - Replace you existing rollback segments with self-tuning undo segments.
Bitmap Join Indexes - Increase the performance and reduce the size of your data warehouse.
Character Semantics And Globalization - Prevent storage problems associated with mutlibyte character
sets by using character semantics when defining table columns.
Data Guard - Oracle9i Data Guard is the new name for Oracle8i Standby Database incorporating a large
number of new features. This article discusses those relevant to the Oracle9i Database: New Features For
Administrators OCP exam.
Export And Import Enhancements In Oracle9i - See what new functionality has been incorporated into the
Export and Import utilities in Oracle9i.
Flashback Query - Get a consistent view of the your data at a previous point in time.
High Availability Enhancements in Oracle9i - Reduce downtime for users by using the new high availability
features of Oracle9i.
Index Organized Table (IOT) Enhancements in Oracle9i - Better support for secondary indexes and
improved online maintenance make IOTs nearly as flexible as heap tables.
Index Skip Scanning - See how Oracle9i uses composite indexes even when the leading edge is not
referenced.
LOB Enhancements In Oracle9i - Kickstart the conversion from LONG and RAW columns to CLOB and BLOB
columns.
Memory Management In Oracle9i - See how easy memory management has become in Oracle9i.
Metadata API - Extract DDL or XML definitions of all database objects using this simple API.
Multiple Block Sizes - Improve efficiency by having multiple block sizes that relate more closely to the
operations that are being performed.
Oracle Managed Files - Let Oracle manage operating system level file placement, naming and cleanup.
Partitioning Enhancements In Oracle9i - Check out the additions and enhancements to partitioning in
Oracle9i.
54
Performance Enhancements In Oracle9i - Read about some of the new performance features in Oracle9i.
Persistent Initialization Parameters - Make database parameter changes persist between shutdowns.
Real Application Clusters - A brief introduction to the Oracle9i replacement for Oracle Parallel Server.
Recovery Enhancements In Oracle9i - Reduce unplanned downtime by using the new crash, instance and
media recovery features of Oracle9i.
Recovery Manager (RMAN) Enhancements In Oracle9i - Use the latest RMAN features which make backup
and recovery quicker and more reliable.
Resource Manager Enhancements In Oracle9i - Manage system resources more precisely using the
Resource Manager enhancements in Oracle9i.
Resumable Space Allocation - Make long running operations suspend rather than abort if they encounter
space errors.
Scalable Session Management - Learn about the new session management features in Oracle9i.
Security Enhancements In Oracle9i - A run through the new security features available in Oracle9i,
focusing on those relevant for the Oracle9i Database: New Features For Administrators OCP exam.
SQL New Features In Oracle9i - Check out the new SQL features with emphasis on those relevant for the
Oracle9i Database: New Features For Administrators OCP exam.
Associative Arrays - Oracle9i Release 2 allows you to index-by string values using this renamed collection.
Bulk Binds and Record Processing in Oracle9i Release 2 - Take advantage of bulk binds for performance
improvements whilst using record structures.
DBNEWID Utility - Change the internal DBID and the database name using this new utility without
rebuilding your controlfile.
DBMS_XPLAN - Easily format the output of an explain plan with this replacement for the utlxpls.sql script.
Export BLOB Contents Using UTL_FILE - Use the new UTL_FILE functionality to write binary data to files.
FTP From PL/SQL - A description of two methods for triggering FTP jobs directly from PL/SQL.
InterMedia - Import-Export Of Images - Use Oracle interMedia to store and retrieve images without using
Java stored procedures.
Renaming Columns And Constraints - Oracle9i Release 2 now allows the renaming of columns and
constraints. Check out the syntax here.
SQL/XML - Oracle9i Release 2 includes functionality to support the emerging SQL/XML standard to simplify
XML generation from SQL queries.
STATISTICS_LEVEL - Let Oracle9i Release 2 control the collection of statistics and advisories with a single
parameter.
Streams - Based on Advanced Queuing and LogMinor, Oracle Streams form a distributed messaging
55
technology that can be used for a variety of tasks including messaging, replication and ETL processes.
UTL_FILE Enhancements - Oracle9i Release 2 includes some long overdue enhancements including basic
file handling and support for NCHAR and RAW data.
UTL_FILE - Random Access of Files - Use the UTL_FILE package for random access of files from PL/SQL.
XML DB - Store and retrieve XML documents from the Oracle XML DB repository using HTTP, FTP and
WebDAV in seconds.
XMLSEQUENCE - Use this operator to split multi-value results from XMLTYPE queries into multiple rows.
Oracle9i (9.2.0.4.0) Installation On Fedora Core 1 Linux - A brief guide to installing Oracle9i (9.2.0.4.0) on
Fedora Core 1 Linux.
Oracle9i (9.2.0.1.0) Installation On RedHat 8.0 Linux - A brief guide to installing Oracle9i (9.2.0.1.0) on
RedHat 8.0 Linux.
56
Oracle9i (9.2.0.1.0) Installation On RedHat 9.0 Linux - A brief guide to installing Oracle9i (9.2.0.1.0) on
RedHat 9.0 Linux.
Oracle9i (9.2.0.4.0) Installation On RedHat Advanced Server 2.1 Linux - A brief guide to installing Oracle9i
(9.2.0.4.0) on RedHat Advanced Server 2.1 Linux.
Oracle9i (9.2.0.4.0) Installation On RedHat Advanced Server 3.0 Linux - A brief guide to installing Oracle9i
(9.2.0.4.0) on RedHat Advanced Server 3.0 Linux.
Oracle9i (9.2.0.1.0) Installation On Tru64 5.1b - A brief guide to installing Oracle9i (9.2.0.1.0) on Tru64
5.1b.
Oracle9i RAC Installation On Tru64 - A brief guide to installing Oracle9i (9.2.0.4.0) Real Application
Clusters (RAC) on Tru64 5.1b.
Manual Oracle Uninstall - Having trouble removing all Oracle software using the OUI? Try these methods.
Load XMLTYPE From File - A simple method to load XMLTYPE data from a file.
Parse XML Documents - Explode unstructured XML documents into relational tables using the new
integrated XDB packages.
XML DB - Store and retrieve XML documents from the Oracle XML DB repository using HTTP, FTP and
WebDAV in seconds.
SQL/XML - Oracle9i Release 2 includes functionality to support the emerging SQL/XML standard to simplify
XML generation from SQL queries.
XMLSEQUENCE - Use this operator to split multi-value results from XMLTYPE queries into multiple rows.
XMLType Datatype - Store XML documents in tables and query them using SQL.
XML-Over-HTTP - XML-over-HTTP was the precursor to web services allowing easy access to XML via HTTP
GETs and POSTs.
XSQL Servlet and XSQL Pages - Publish dynamic XML documents through HTTP using the XSQL Servlet
utility.
Consuming Web Services - Access web services directly from PL/SQL using this simple API.
Email From PL/SQL In Oracle9i - Email from PL/SQL rather than using external procedures or Java.
File Upload and Download Procedures - Upload and download files directly from the database using
Database Access Descriptors.
FTP From PL/SQL - A description of two methods for triggering FTP jobs directly from PL/SQL.
Images from Oracle Over HTTP - Retrieve images directly from the database over HTTP.
57
Java Server Pages - Use Java as a scripting language to interact with the database from web pages.
PL/SQL Server Pages - Use PL/SQL as a scripting language to generate web pages directly from the
database.
PL/SQL Web Toolkit - Generate web pages directly from the database using this simple toolkit.
Stateless Locking Methods - Learn how to avoid data loss in stateless environments.
Advanced Queuing In Oracle9i - Get to grips with the basics of advanced queuing in Oracle9i.
Archivelog Mode On RAC - The differences between resetting the archive log mode on a single node
instance and a Real Application Cluster (RAC).
CASE Expressions And Statements - Learn how to use CASE expressions in both SQL and PL/SQL. In
addition, learn how to use the CASE statement in PL/SQL.
Complete Data Audit - A simple and generic solution for auditing before and after snapshots of data.
Compressed Tables - Compress whole tables or individual table partitions to reduce disk space
requirements.
Duplicate a Database Using RMAN - Use RMAN to create a duplicate, or clone, of a database from a recent
backup.
DBMS_LDAP - Accessing LDAP From PL/SQL - Use the DBMS_LDAP package to query and modify LDAP
entries from PL/SQL.
DBMS_LIBCACHE - Warm up the library cache of an instance by compiling the SQL and PL/SQL statements
from the library cache of another instance.
DBMS_PROFILER - Profile the run-time behaviour of PL/SQL code to identify potential bottlenecks.
DBMS_TRACE - Trace the run-time behaviour of PL/SQL code to identify potential bottlenecks.
Dynamic Binds Using Contexts - Simplify dynamic variable binds within dynamic SQL using contexts.
External Tables - Query the contents of flat files as if they were regular tables.
Full Text Indexing using Oracle Text - Efficiently query free text and produce document classification
applications using Oracle Text.
Generating CSV Files - A simple example of using the UTL_FILE package to generate CSV extract files.
Heterogeneous Services - Generic Connectivity In Oracle9i - Query non-Oracle datasources using ODBC.
Java Native Compilation - Improve the performance of Java procedural code by compiling it to native
shared libraries.
Mutating Table Exceptions - A simple method to prevent triggers producing mutating table exceptions.
Oracle Internet Directory - Use the Oracle Internet Directory to replace local Oracle Net configuration files
and Oracle Names Server.
58
Oracle Label Security - Configure row-level security with this out-of-the-box solution.
Pipelined Table Functions - Improve performance of ETL processes by pipelining all transformation
functions.
PL/SQL Native Compilation - Improve the performance of PL/SQL procedural code by compiling it to native
shared libraries.
RANK, DENSE_RANK, FIRST and LAST Analytic Functions - Simple examples of how to use these analytic
functions.
Recovery Manager (RMAN) - Explanation of RMANs basic backup, recovery and reporting functionality.
Storing Passwords In The Database - Store passwords securely in the database using this simple hashing
technique.
Transportable Tablespaces - Copy tablespaces to new instances in the time it takes to copy the datafiles.
Unregister a Database From RMAN - A step-by-step guide to unregistering unwanted databases from the
RMAN catalog.
Universal Unique Identifier (UUID) - Reduce data migration and replication issues by replacing sequence
generated IDs with UUIDs.
Useful Procedures And Functions - Procedures and functions you may have overlooked which can come in
useful during development.
Oracle9iAS Backup and Recovery - Simplify backup and recovery of Oracle9i Application Server using this
Oracle supplied Perl utility.
Oracle9iAS (9.0.3.0.0) Installation On RedHat Advanced Server 2.1 - A brief guide to installing Oracle9iAS
(9.0.3.0.0) on RedHat Advanced Server 2.1.
Oracle9iAS (9.0.2.0.1) Portal Installation On Tru64 5.1b - A brief guide to installing Oracle9iAS (9.0.2.0.1)
Portal on Tru64 5.1b.
Oracle9iAS (9.0.2.0.1) Portal Installation On Windows 2000 - A brief guide to installing Oracle9iAS
(9.0.2.0.1) Portal on Windows 2000.
SQL*Loader
Use Direct Path Loads - The conventional path loader essentially loads the data by using standard
insert statements. The direct path loader (direct=true) loads directly into the Oracle data files and creates
blocks in Oracle database block format. There are certain cases, however, in which direct path loads
59
cannot be used (clustered tables). To prepare the database for direct path loads, the script
$ORACLE_HOME/rdbms/admin/catldr.sql.sql must be executed.
Disable Indexes and Constraints. For conventional data loads only, the disabling of indexes and
constraints can greatly enhance the performance.
Use a Larger Bind Array. For conventional data loads only, larger bind arrays limit the number of
calls to the database and increase performance. The size of the bind array is specified using the bindsize
parameter. The bind array's size is equivalent to the number of rows it contains (rows=) times the
maximum length of each row.
Use ROWS=n to Commit Less Frequently. For conventional data loads only, the rows parameter
specifies the number of rows per commit. Issuing fewer commits will enhance performance.
Use Parallel Loads. Available with direct path data loads only, this option allows multiple
SQL*Loader jobs to execute concurrently.
Use Fixed Width Data. Fixed width data format saves Oracle some processing when parsing the
data. The savings can be tremendous.
Disable Archiving During Load. While this may not be feasible in certain environments, disabling
database archiving can increase performance considerably.
Use unrecoverable. The unrecoverable option (unrecoverable load data) disables the writing of the
data to the redo logs. This option is available for direct path loads only.
Using the table table_with_one_million_rows, the following benchmark tests were performed with the
various SQL*Loader options. The table was truncated after each test.
The results above indicate that conventional path loads take the longest. However, the bindsize and rows
parameters can aid the performance under these loads. The test involving the conventional load didn’t
come close to the performance of the direct path load with the unrecoverable option specified.
60
It is also worth noting that the fastest import time achieved for this table (earlier) was 67 seconds,
compared to 41 for SQL*Loader direct path – a 39% reduction in execution time. This proves that
SQL*Loader can load the same data faster than import. These tests did not compensate for indexes. All
database load operations will execute faster when indexes are disabled.
The control file is a text file written in a language that SQL*Loader understands. The control file describes
the task that the SQL*Loader is to carry out. The control file tells SQL*Loader where to find the data, how
to parse and interpret the data, where to insert the data, and more. See Chapter 4, "SQL*Loader Case
Studies" for example control files.
Although not precisely defined, a control file can be said to have three sections:
Examples
OPTIONS Clause
• A simple control file identifying one table and three columns to be loaded.
• Including data to be loaded from the control file itself, so there is no separate datafile.
• Loading data in stream format, with both types of delimited fields -- terminated and enclosed.
Control File
1) LOAD DATA
2) INFILE *
3) INTO TABLE dept
4) FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
5) (deptno, dname, loc)
6) BEGINDATA
12,RESEARCH,"SARATOGA"
10,"ACCOUNTING",CLEVELAND
11,"ART",SALEM
13,FINANCE,"BOSTON"
21,"SALES",PHILA.
22,"SALES",ROCHESTER
42,"INT'L","SAN FRAN"
Notes:
1. The LOAD DATA statement is required at the beginning of the control file.
2. INFILE * specifies that the data is found in the control file and not in an external file.
3. The INTO TABLE statement is required to identify the table to be loaded (DEPT) into. By default,
SQL*Loader requires the table to be empty before it inserts any records.
4. FIELDS TERMINATED BY specifies that the data is terminated by commas, but may also be
enclosed by quotation marks. Datatypes for all fields default to CHAR.
5. Specifies that the names of columns to load are enclosed in parentheses. Since no datatype is
specified, the default is a character of length 255.
62
BEGINDATA specifies the beginning of the data.
Invoking SQL*Loader
To run this example, invoke SQL*Loader with the command:
sqlldr userid=scott/tiger control=ulcase1.ctl log=ulcase1.log
• A separate datafile.
• Data conversions.
In this case, the field positions and datatypes are specified explicitly.
Control File
1) LOAD DATA
2) INFILE 'ulcase2.dat'
3) INTO TABLE emp
4) (empno POSITION(01:04) INTEGER EXTERNAL,
ename POSITION(06:15) CHAR,
job POSITION(17:25) CHAR,
mgr POSITION(27:30) INTEGER EXTERNAL,
sal POSITION(32:39) DECIMAL EXTERNAL,
comm POSITION(41:48) DECIMAL EXTERNAL,
5) deptno POSITION(50:51) INTEGER EXTERNAL,
6) modifieddate "SYSDATE",
7) customerid constant "0"
)
Notes:
1. The LOAD DATA statement is required at the beginning of the control file.
2. The name of the file containing data follows the keyword INFILE.
3. The INTO TABLE statement is required to identify the table to be loaded into.
4. Lines 4 and 5 identify a column name and the location of the data in the datafile to be loaded into
that column. EMPNO, ENAME, JOB, and so on are names of columns in table EMP. The datatypes
(INTEGER EXTERNAL, CHAR, DECIMAL EXTERNAL) identify the datatype of data fields in the file,
not of corresponding columns in the EMP table.
5. Note that the set of column specifications is enclosed in parentheses.
6. This statement let me insert the current sysdate in this field
7. This statement let me put a constant value
Datafile
Below are a few sample data lines from the file ULCASE2.DAT. Blank fields are set to null automatically.
In this case, the field positions and datatypes are specified explicitly.
Control File
This control file loads the same table as in Case 2, but it loads three additional columns (HIREDATE,
PROJNO, LOADSEQ). The demonstration table EMP does not have columns PROJNO and LOADSEQ. So if
you want to test this control file, add these columns to the EMP table with the command:
ALTER TABLE EMP ADD (PROJNO NUMBER, LOADSEQ NUMBER)
The data is in a different format than in Case 2. Some data is enclosed in quotation marks, some is set off
by commas, and the values for DEPTNO and PROJNO are separated by a colon.
1) -- Variable-length, delimited and enclosed data format
LOAD DATA
2) INFILE *
3) APPEND
INTO TABLE emp
4) FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'
(empno, ename, job, mgr,
5) hiredate DATE(20) "DD-Month-YYYY",
sal, comm, deptno CHAR TERMINATED BY ':',
projno,
6) loadseq SEQUENCE(MAX,1))
7) BEGINDATA
8) 7782, "Clark", "Manager", 7839, 09-June-1981, 2572.50,, 10:101
7839, "King", "President", , 17-November-1981,5500.00,,10:102
7934, "Miller", "Clerk", 7782, 23-January-1982, 920.00,, 10:102
7566, "Jones", "Manager", 7839, 02-April-1981, 3123.75,, 20:101
7499, "Allen", "Salesman", 7698, 20-February-1981, 1600.00,
(same line continued) 300.00, 30:103
7654, "Martin", "Salesman", 7698, 28-September-1981, 1312.50,
(same line continued) 1400.00, 3:103
7658, "Chan", "Analyst", 7566, 03-May-1982, 3450,, 20:101
Notes:
1. Comments may appear anywhere in the command lines of the file, but they should not appear in
data. They are preceded with a double dash that may appear anywhere on a line.
2. INFILE * specifies that the data is found at the end of the control file.
3. Specifies that the data can be loaded even if the table already contains rows. That is, the table
need not be empty.
4. The default terminator for the data fields is a comma, and some fields may be enclosed by double
quotation marks (").
5. The data to be loaded into column HIREDATE appears in the format DD-Month-YYYY. The length of
the date field is a maximum of 20. If a length is not specified, the length is a maximum of 20. If a
length is not specified, then the length depends on the length of the date mask.
6. The SEQUENCE function generates a unique value in the column LOADSEQ. This function finds the
current maximum value in column LOADSEQ and adds the increment (1) to it to obtain the value
for LOADSEQ for each row inserted.
7. BEGINDATA specifies the end of the control information and the beginning of the data.
8. Although each physical record equals one logical record, the fields vary in length so that some
records are longer than others. Note also that several rows have null values for COMM.
64
Case 4: Loading Combined Physical Records
• Combining multiple physical records to form one logical record with CONTINUEIF
• Inserting negative numbers.
• Indicating with REPLACE that the table should be emptied before the new data is inserted
• Specifying a discard file in the control file using DISCARDFILE
• Specifying a maximum number of discards using DISCARDMAX
• Rejecting records due to duplicate values in a unique index or due to invalid data values
Control File
LOAD DATA
INFILE 'ulcase4.dat'
1) DISCARDFILE 'ulcase4.dsc'
2) DISCARDMAX 999
3) REPLACE
4) CONTINUEIF THIS (1) = '*'
INTO TABLE emp
(empno POSITION(1:4) INTEGER EXTERNAL,
ename POSITION(6:15) CHAR,
job POSITION(17:25) CHAR,
mgr POSITION(27:30) INTEGER EXTERNAL,
sal POSITION(32:39) DECIMAL EXTERNAL,
comm POSITION(41:48) DECIMAL EXTERNAL,
deptno POSITION(50:51) INTEGER EXTERNAL,
hiredate POSITION(52:60) INTEGER EXTERNAL)
Notes:
Data File
The datafile for this case, ULCASE4.DAT, is listed below. Note the asterisks in the first position and, though
not visible, a new line indicator is in position 20 (following "MA", "PR", and so on). Note that CLARK's
commission is -10, and SQL*Loader loads the value converting it to a negative number.
Rejected Records
65
The last two records are rejected, given two assumptions. If there is a unique index created on column
EMPNO, then the record for CHIN will be rejected because his EMPNO is identical to CHAN's. If EMPNO is
defined as NOT NULL, then CHEN's record will be rejected because it has no value for EMPNO.
Case 5: Loading Data in Multiple Tables
Control File
-- Loads EMP records from first 23 characters
-- Creates and loads PROJ records for each PROJNO listed
-- for each employee
LOAD DATA
INFILE 'ulcase5.dat'
BADFILE 'ulcase5.bad'
DISCARDFILE 'ulcase5.dsc'
1) REPLACE
2) INTO TABLE emp
(empno POSITION(1:4) INTEGER EXTERNAL,
ename POSITION(6:15) CHAR,
deptno POSITION(17:18) CHAR,
mgr POSITION(20:23) INTEGER EXTERNAL)
2) INTO TABLE proj
-- PROJ has two columns, both not null: EMPNO and PROJNO
3) WHEN projno != ' '
(empno POSITION(1:4) INTEGER EXTERNAL,
3) projno POSITION(25:27) INTEGER EXTERNAL) -- 1st proj
3) INTO TABLE proj
4) WHEN projno != ' '
(empno POSITION(1:4) INTEGER EXTERNAL,
4) projno POSITION(29:31 INTEGER EXTERNAL) -- 2nd proj
2) INTO TABLE proj
5) WHEN projno != ' '
(empno POSITION(1:4) INTEGER EXTERNAL,
5) projno POSITION(33:35) INTEGER EXTERNAL) -- 3rd proj
Notes:
• REPLACE specifies that if there is data in the tables to be loaded (EMP and PROJ), SQL*loader
should delete the data before loading new rows.
• Multiple INTO clauses load two tables, EMP and PROJ. The same set of records is processed three
times, using different combinations of columns each time to load table PROJ.
• WHEN loads only rows with non-blank project numbers. When PROJNO is defined as columns
25...27, rows are inserted into PROJ only if there is a value in those columns.
• When PROJNO is defined as columns 29...31, rows are inserted into PROJ only if there is a value in
those columns.
• When PROJNO is defined as columns 33...35, rows are inserted into PROJ only if there is a value in
those columns.
Data File
• Use of the direct path load method to load and index data
• How to specify the indexes for which the data is pre-sorted.
• Loading all-blank numeric fields as null
• The NULLIF clause
• Note: Specify the name of the table into which you want to load data; otherwise, you will see LDR-
927. Specifying DIRECT=TRUE as a command-line parameter is not an option when loading into a
synonym for a table.
• The SORTED INDEXES clause identifies indexes:presorting data:case study the indexes on which
the data is sorted. This clause indicates that the datafile is sorted on the columns in the EMPIX
index. This clause allows SQL*Loader to optimize index creation by eliminating the sort phase for
this data when using the direct path load method.
• The NULLIF...BLANKS clause specifies that the column should be loaded as NULL if the field in the
datafile consists of all blanks
Insert Trigger
In this case, a BEFORE INSERT trigger is required to fill in department number, job name, and manager's
number when these fields are not present on a data line. When values are present, they should be saved
in a global variable. When values are not present, the global variables are used.
The INSERT trigger and the package defining the global variables is:
CREATE OR REPLACE PACKAGE uldemo7 AS -- Global Package Variables
last_deptno NUMBER(2);
last_job VARCHAR2(9);
last_mgr NUMBER(4);
END uldemo7;
/
Note: The phrase FOR EACH ROW is important. If it was not specified, the INSERT trigger would only fire
once for each array of inserts because SQL*Loader uses the array interface.
Control File
LOAD DATA
INFILE 'ULCASE7.DAT'
APPEND
INTO TABLE emp
68
1) WHEN (57) = '.'
2) TRAILING NULLCOLS
3) (hiredate SYSDATE,
4) deptno POSITION(1:2) INTEGER EXTERNAL(3)
5) NULLIF deptno=BLANKS,
job POSITION(7:14) CHAR TERMINATED BY WHITESPACE
6) NULLIF job=BLANKS "UPPER(:job)",
7) mgr POSITION(28:31) INTEGER EXTERNAL
TERMINATED BY WHITESPACE, NULLIF mgr=BLANKS,
ename POSITION(34:41) CHAR
TERMINATED BY WHITESPACE "UPPER(:ename)",
empno POSITION(45) INTEGER EXTERNAL
TERMINATED BY WHITESPACE,
sal POSITION(51) CHAR TERMINATED BY WHITESPACE
8) "TO_NUMBER(:sal,'$99,999.99')",
9) comm INTEGER EXTERNAL ENCLOSED BY '(' AND '%'
":comm * 100"
)
Notes:
• The decimal point in column 57 (the salary field) identifies a line with data on it. All other lines in
the report are discarded.
• The TRAILING NULLCOLS clause causes SQL*Loader to treat any fields that are missing at the end
of a record as null. Because the commission field is not present for every record, this clause says to
load a null commission instead of rejecting the record when only six fields are found instead of the
expected seven.
• Employee's hire date is filled in using the current system date.
• This specification generates a warning message because the specified length does not agree with
the length determined by the field's position. The specified length (3) is used.
• Because the report only shows department number, job, and manager when the value changes,
these fields may be blank. This control file causes them to be loaded as null, and an RDBMS insert
trigger fills in the last valid value.
• The SQL string changes the job name to uppercase letters.
• It is necessary to specify starting position here. If the job field and the manager field were both
blank, then the job field's TERMINATED BY BLANKS clause would cause SQL*Loader to scan
forward to the employee name field. Without the POSITION clause, the employee name field would
be mistakenly interpreted as the manager field.
• Here, the SQL string translates the field from a formatted character string into a number. The
numeric value takes less space and can be printed with a variety of formatting options.
• In this case, different initial and trailing delimiters pick the numeric value out of a formatted field.
The SQL string then converts the value to its stored form.
• Partitioning of data
• Explicitly defined field positions and datatypes.
• Loading using the fixed record length option
Control File
LOAD DATA
1) INFILE 'ulcase10.dat' "fix 129"
BADFILE 'ulcase10.bad'
TRUNCATE
INTO TABLE lineitem
PARTITION (ship_q1)
2) (l_orderkey position (1:6) char,
69
l_partkey position (7:11) char,
l_suppkey position (12:15) char,
l_linenumber position (16:16) char,
l_quantity position (17:18) char,
l_extendedprice position (19:26) char,
l_discount position (27:29) char,
l_tax position (30:32) char,
l_returnflag position (33:33) char,
l_linestatus position (34:34) char,
l_shipdate position (35:43) char,
l_commitdate position (44:52) char,
l_receiptdate position (53:61) char,
l_shipinstruct position (62:78) char,
l_shipmode position (79:85) char,
l_comment position (86:128) char)
Notes:
• Specifies that each record in the datafile is of fixed length (129 characters in this example). See
Input Data and Datafiles.
• Identifies the column name and location of the data in the datafile to be loaded into each column.
Table Creation
In order to partition the data the lineitem table is created using four (4) partitions according to the
shipment date:
Create table lineitem
(l_orderkey number,
l_partkey number,
l_suppkey number,
l_linenumber number,
l_quantity number,
l_extendedprice number,
l_discount number,
l_tax number,
l_returnflag char,
l_linestatus char,
l_shipdate date,
l_commitdate date,
l_receiptdate date,
l_shipinstruct char(17),
l_shipmode char(7),
l_comment char(43)
)
partition by range (l_shipdate)
(
partition ship_q1 values less than (TO_DATE('01-APR-1996', 'DD-MON-YYYY'))
tablespace p01,
partition ship_q2 values less than (TO_DATE('01-JUL-1996', 'DD-MON-YYYY'))
tablespace p02,
partition ship_q3 values less than (TO_DATE('01-OCT-1996', 'DD-MON-YYYY'))
tablespace p03,
partition ship_q4 values less than (TO_DATE('01-JAN-1997', 'DD-MON-YYYY'))
tablespace p04
)
Input Data File
The datafile for this case, ULCASE8.DAT, is listed below. Each record is 129 characters in length. Note that
five(5) blanks precede each record in the file.
1 151978511724386.60 7.04.0NO09-SEP-6412-FEB-9622-MAR-96DELIVER IN PERSONTRUCK
iPBw4mMm7w7kQ zNPL i261OPP
70
1 2731 73223658958.28.09.06NO12-FEB-9628-FEB-9620-APR-96TAKE BACK RETURN MAIL
5wM04SNyl0AnghCP2nx lAi
1 3370 3713 810210.96 .1.02NO29-MAR-9605-MAR-9631-JAN-96TAKE BACK RETURN REG AIRSQC2C
5PNCy4mM
1 5214 46542831197.88.09.06NO21-APR-9630-MAR-9616-MAY-96NONE AIR Om0L65CSAwSj5k6k
1 6564 6763246897.92.07.02NO30-MAY-9607-FEB-9603-FEB-96DELIVER IN PERSONMAIL
CB0SnyOL PQ32B70wB75k 6Aw10m0wh
1 7403 160524 31329.6 .1.04NO30-JUN-9614-MAR-9601 APR-96NONE FOB C2gOQj OB6RLk1BS15
igN
2 8819 82012441659.44 0.08NO05-AUG-9609-FEB-9711-MAR-97COLLECT COD AIR
O52M70MRgRNnmm476mNm
3 9451 721230 41113.5.05.01AF05-SEP-9629-DEC-9318-FEB-94TAKE BACK RETURN FOB
6wQnO0Llg6y
3 9717 1834440788.44.07.03RF09-NOV-9623-DEC-9315-FEB-94TAKE BACK RETURN SHIP
LhiA7wygz0k4g4zRhMLBAM
3 9844 1955 6 8066.64.04.01RF28-DEC-9615-DEC-9314-FEB-94TAKE BACK RETURN REG
AIR6nmBmjQkgiCyzCQBkxPPOx5j4hB 0lRywgniP1297
Control File
LOAD DATA
INFILE *
INTO TABLE EMP
REPLACE
FIELDS TERMINATED BY ','
( EMPNO INTEGER EXTERNAL,
ENAME CHAR,
JOB CHAR,
MGR INTEGER EXTERNAL,
SAL DECIMAL EXTERNAL,
COMM DECIMAL EXTERNAL,
DEPTNO INTEGER EXTERNAL,
1) RES_FILE FILLER CHAR,
2) "RESUME" LOBFILE (RES_FILE) TERMINATED BY EOF NULLIF RES_FILE = 'NONE'
)
BEGINDATA
7782,CLARK,MANAGER,7839,2572.50,,10,ulcase91.dat
7839,KING,PRESIDENT,,5500.00,,10,ulcase92.dat
7934,MILLER,CLERK,7782,920.00,,10,ulcase93.dat
7566,JONES,MANAGER,7839,3123.75,,20,ulcase94.dat
7499,ALLEN,SALESMAN,7698,1600.00,300.00,30,ulcase95.dat
7654,MARTIN,SALESMAN,7698,1312.50,1400.00,30,ulcase96.dat
7658,CHAN,ANALYST,7566,3450.00,,20,NONE
Notes:
• This is a filler field. The filler field is assigned values from the datafield to which it is mapped.
• RESUME is loaded as a CLOB. The LOBFILE function is used to specify the name of the field that
specifies name of the file which contains the data for the LOB field.
71
Case 10: How to use TRIM, TO_NUMBER, TO_CHAR with SQL*Loader
This note explains how to use the functions TRIM, TO_CHAR/TO_NUMBER, and user defined functions in
connection with SQL*Loader using the following example:
LOAD DATA
INFILE *
INTO TABLE TEST
TRUNCATE
72
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
ID SEQUENCE,
FIRST_NAME "TRIM (:FIRST_NAME)", -- 1)
LAST_NAME "TRIM ('x' FROM :LAST_NAME)", -- 2)
DEPARTMENT,
SALARY "TO_NUMBER (:SALARY, '99999D99', -- 3)
'NLS_NUMERIC_CHARACTERS='',.''')",
BONUS "GET_BONUS (:DEPARTMENT)", -- 4)
DESCRIPTION "'Salary: '||:SALARY||' -> '|| -- 5)
DECODE (GET_BONUS (:DEPARTMENT), NULL, 'No bonus',
'Bonus: '||TO_CHAR (GET_BONUS (:DEPARTMENT), 'S09D90'))",
TOTAL "CALC_SAL (TO_NUMBER (:SALARY, '99999D99', -- 6)
'NLS_NUMERIC_CHARACTERS='',.'''), GET_BONUS (:DEPARTMENT))"
)
BEGINDATA
" Martin ";"xxxxxxxSmithxxx";CLERK;2459,25
" David ";"xxxxxxxAllenxxx";SALESMAN;4563,9
" Brad ";"xxxxxxxWardxxxx";SALESMAN;4815,81
" Marvin ";"xxxxxxxJonesxxx";MANAGER;9765,33
" Dean ";"xxxxxxxMartinxx";SALESMAN;4214,56
" John ";"xxxxxxxBlakexxx";MANAGER;10333,87
" Clark ";"xxxxxxxGablexxx";MANAGER;11011,11
" Scott ";"xxxxxxxTigerxxx";ANALYST;6865,88
" Ralph ";"xxxxxxxKingxxxx";PRESIDENT;18955,45
" Tina ";"xxxxxxxTurnerxx";SALESMAN;5324,44
" Bryan ";"xxxxxxxAdamsxxx";CLERK;1899,48
" Jesse ";"xxxxxxxJamesxxx";CLERK;2288,99
" John ";"xxxxxxxFordxxxx";ANALYST;7564,83
" John ";"xxxxxxxMillerxx";CLERK;1865,93
1) TRIM deletes the leading/trailing blanks in the column FIRST_NAME (i.e. " Martin " becomes
"Martin")
2) TRIM deletes the leading/trailing 'x' characters in the column LAST_NAME (i.e. "xxxxxxxSmithxxx"
becomes "Smith")
3) TO_NUMBER shows that the format of the numbers in the column SALARY is in the form: 99999D99.
That means max. 5 digits integer with max. 2 digit
post-decimal positions. The decimal separator is ','. If the format is not specified, then the records are
not loaded (ORA-1722 invalid number, if NLS_NUMERIC_CHARACTERS = '.,')
4) The column BONUS is calculated with the user defined function GET_BONUS. The Function expects an
input parameter, DEPARTMENT (VARCHAR2), and
returns the value, BONUS (NUMBER(2,2))
5) The column DESCRIPTION is a composition of the information from the previous columns. The Function
DECODE checks if a bonus is available to the department. If no bonus is available, then the message 'No
bonus' will be printed. The new thing here is the function TO_CHAR. This function modifies the format of
the BONUS in this form: sign, 2 integer digits with leading zeros, decimal separator, 2 post-decimal
positions with trailing zeros.
6) The column TOTAL is calculated with the user defined function CALC_SAL (the BONUS, if available, is
applied to the SALARY)
The result after the loading procedure looks like this in the table TEST:
SQL> select * from test;
LOAD DATA
INFILE *
APPEND
INTO TABLE dept (
deptno POSITION(01:02) INTEGER EXTERNAL,
dname POSITION(03:16) CHAR "LDR_PCK.NAME(:dname, :deptno)",
loc POSITION(17:29) CHAR
)
BEGINDATA
21Dep Loc
22Dep Loc
Here LDR_PCK.NAME is name of package and function, :dnname and :deptno are parameters.
When calling user-defined functions we must remember that only conventional path may be used. An
attempt to use direct path will cause an error:
SQL*Loader-00417 SQL String (on column column_name) not allowed in direct path.
The OPTIONS clause is useful when you usually invoke a control file with the same set of options, or when
the command line and all its arguments becomes very long. This clause allows you to specify runtime
arguments in the control file rather than on the command line.
Values specified on the command line override values specified in the control file. With this precedence,
the OPTIONS keyword in the control file established default values that are easily changed from the
command line.
If SQL*Loader runs out of space for data rows or index entries, the load is discontinued. (For example, the
table might reach its maximum number of extents.) Discontinued loads can be continued after more space
is made available.
When a load is discontinued, any data already loaded remains in the tables, and the tables are left in a
valid state. SQL*Loader's log file tells you the state of the tables and indexes and the number of logical
records already read from the input data file. Use this information to resume the load where it left off.
For example:
CONTINUE\_LOAD DATA statement is used to continue a discontinued direct path load involving multiple
tables with a varying number of records to skip. For more information on this command, see chapter 6 of
``ORACLE7 Server Utilities Users Guide''.
To specify the file containing the data to be loaded, use the INFILE or INDDN keyword, followed by the
filename. A filename specified on the command line overrides the first INFILE or INDDN statement in the
control file. If no filename is specified, the filename defaults to the control filename with an extension or
filetype of DAT.
SQL*Loader does not update existing records, even if they have null columns. If the tables you are loading
already contain data, you have three choices for how SQL*Loader should proceed:
INSERT - This is the default option. It requires the table to be empty before loading. SQL*Loader
terminates with an error if the table contains rows.
APPEND - If data already exists in the table, SQL*Loader appends the new rows to it; if data doesn't
already exist, the new rows are simply loaded.
REPLACE - All rows in the table are deleted and the new data is loaded. This option requires DELETE
privileges on the table.
You can create one logical record from multiple physical records using CONCATENATE and CONTINUEIF.
See chapter 6 of ``ORACLE7 Server Utilities Users Guide''.
The INTO TABLE clause may continue with some options for loading that table. For example, you may
specify different options (INSERT, APPEND, REPLACE) for each table in order to tell SQL*Loader what to do
if data already exists in the table.
The WHEN clause appears after the table name and is followed by one or more field conditions. For
example, the following clause indicates that any record with the value ``q'' in the fifth column position
should be loaded:
A WHEN clause can contain several comparisons as long as each is preceded by AND. Parentheses are
optional but should be used for clarity with multiple comparisons joined by AND. For example:
To evaluate the WHEN clause, SQL*Loader first determines the values of all the fields in the record. Then
the WHEN clause is evaluated. A row is inserted into the table only if the WHEN clause is true.
When the control file specifies more fields for a record than are present in the record, SQL*Loader must
determine whether the remaining (specified) columns should be considered null, or whether an error
should be generated. TRAILING NULLCOLS clause tells SQL*Loader to treat any relatively positioned
columns that are not present in the record as null columns. For example, if the following data
10 Accounting
and the record ends after DNAME, then the remaining LOC field is set to null. Without the TRAILING
NULLCOLS clause, an error would be generated, due to missing data.
Oracle datatypes
m
p
b
w
The The e
precision p precision p d
can range can range
The precision p can range
from 1 to from 1 to T
from 1 to 38.
Number having precision p 38. 38. r
NUMBER(p,s)
and scale s. t
The scale s can range from
The scale s The scale s
-84 to 127.
can range can range F
from -84 to from -84 to p
127. 127. s
R
m
p
b
w
e
d
PLS_INTEGER signed integers PL/SQL PL/SQL PL/SQL only m
PLS_INTEGER values require only only r
less storage and provide -
better performance than 2
77
NUMBER values.
So use PLS_INTEGER where
you can!
m
signed integers (older slower r
BINARY_INTEGER
version of PLS_INTEGER) -
2
3
N
Character data of variable
2 2 2 Gigabytes - but now s
LONG length (A bigger version the
Gigabytes Gigabytes deprecated t
VARCHAR2 datatype)
w
c
from from f
January 1, January 1, 4
4712 BC to 4712 BC to from January 1, 4712 BC to D
DATE Valid date range
December December December 31, 9999 AD. 9
31, 4712 31, 9999 (
AD. AD. 4
the number of digits in the Accepted values of
TIMESTAMP
fractional part of the - - fractional_seconds_precision
(fractional_seconds_precision)
SECOND datetime field. are 0 to 9. (default = 6)
TIMESTAMP Accepted values of
As above with time zone
(fractional_seconds_precision) - - fractional_seconds_precision
displacement value
WITH {LOCAL} TIMEZONE are 0 to 9. (default = 6)
Time in years and months,
INTERVAL YEAR where year_precision is the Accepted values are 0 to 9.
- -
(year_precision) TO MONTH number of digits in the YEAR (default = 2)
datetime field.
Time in days, hours,
minutes, and seconds.
o
t
returned by the ROWID (
pseudocolumn.) v
b
p
u
-
r
The
l
Hex string representing the maximum
The maximum size and o
UROWID logical address of a row of N/A size and
default is 4000 bytes i
an index-organized table default is
t
4000 bytes
p
o
O
Binary format of an
operating system label.This
MLSLABEL
datatype is used with
Trusted Oracle7.
CLOB Character Large Object 4Gigabytes 4Gigabytes 4Gigabytes 4
National Character Large
NCLOB 4Gigabytes 4Gigabytes 4
Object
BLOB Binary Large Object 4Gigabytes 4Gigabytes 4
T
B
d
BFILE pointer to binary file on disk 4Gigabytes 4Gigabytes c
f
(
b
INTEGER
This ANSI datatype will be accepted by Oracle - it is actually a synonym for NUMBER(38)
VARCHAR2:
Storing character data as Varchar2 will save space:
CHAR:
Over time, when varchar2 columns are updated they will sometimes create chained rows - because CHAR
columns are fixed width they are not affected by this - so less DBA effort is required to maintain
performance.
NUMBER
When retrieving data for a NUMBER column, consider (if you can) using the PL/SQL datatype:
PLS_INTEGER for better performance.
LONG
You should start using BLOB instead of LONG
79
Comparison with other RDBMS's
Oracle8i also allows you to rebuild your indexes while online. In the past, creating or rebuilding the index
required a full lock on the table. On a large table, this could mean that an application is unusable for
several hours.
Now, however, Oracle allows you to create or rebuild the index while users can still perform the full range
of data processes. To do this, Oracle creates the index structure before populating it. While populating, all
changes to the table are recorded in a journal table. As the index is completed, the journal table changes
are then built in.
Brief table locks are made while the index structure is made and the journal table bought into the index.
For a B-Tree index, PCTFREE determines the extent of a leaf-split. In other words, PCTFREE indicates the
amount of free-space within a block for “future updates”. However in the case of an index (unlike a table),
these updates do not make sense, since an update anyway forces a delete and subsequent insert. In the
case of indexes, PCTFREE plays a role mostly at the time of index-creation. A non-zero value specifies the
block-splitting ratio. If PCTFREE is set to 20, during index-creation, up to 80% of the leaf may contain key-
information.
However the remaining 20% should be available for future inserts of key-information into that leaf-block.
This ensures that during run-time inserts, there is minimal overhead in terms of incurring leaf-block splits.
Even though a higher PCTFREE may cause index-creation time to increase, it prevents the performance-hit
from being deferred to actual usage time. So, the end-user, who is waiting for a row to be inserted, does
not incur the performance penalty associated with a leaf-block split.
Based on this information, the following points emerge:
PCTFREE for an index is used primarily during creation. It is ignored during actual usage.
Specify
a higher PCTFREE for OLTP applications, if the table is a popular one, incurring a lot of DML
changes (via interactive user-screens);
If the index-creation time is critical specify a lower PCTFREE. This will pack in more rows per leaf-
block, thereby avoiding the need for further splitting at creation-time. This is of paramount significance to
shops that have “24x7” availability requirements. Index-creation in most cases requires considerable
downtime (especially if the table is a multi-million row table). Lesser the amount of index-creation time,
80
smaller can be the maintenance-window. I have seen this tiny, often unnoticed parameter save around
20% of index-creation time. At a high-availability site, an index on a table containing around 11 million
rows took me about 80 minutes to build using PCTFREE of 30 and a parallel degree of 4. The same index
on the same table, with 13.5 million rows took me around 90 minutes to create with a PCTFREE of 0
(without any hardware/software enhancements). Nologging (minimal redo) was on during both creations.
For any column where the values are constantly increasing, it is probably a good idea to set a very low
PCTFREE (even, zero). This is because only the rightmost leaf-block will always be inserted into. This will
make the tree grow towards the right. The leftmost leaves would remain static. So, there is no sense in
leaving any part of those blocks empty with a non-zero PCTFREE.
1- Partitioned Indexes
Like tables, indexes can also be partitioned; but with indexes you have a more options because the
underlying table might or might not also be partitioned. The objective of this type of index is to separate
the index into smaller partitions, just as we do now for a database table. There are essentially two
different types of partitioned indexes available:
•Global indexes--These are created in a manner different from the underlying partitioning of the table
that is indexed.
•Local indexes--These are partitioned in the same manner as the underlying table partitioning.
Global Indexes
To create a global partitioned index, use the CREATE INDEX parameter GLOBAL. This specifies that the
index will be a global index. Further partitioning of the index is accomplished by using the following
parameters:
•GLOBAL--This parameter specifies a global partitioned index.
•PARTITION part_name--This parameter is used to identify the partition. If you do not specify the
partition name, a default name will be provided. It is not usually necessary to provide the partition
name.
•VALUES LESS THAT--This parameter is used to specify the range that is allocated for that particular
partition in the same way as the partition was specified in the CREATE TABLE statement (discussed
yesterday).
NOTE: The last partition should contain the keyword MAXVALUE for its range.
ANLYSIS:
This create two partitions, the first holding values of ID that are less than 1,000, the second holding the
remaining values of ID. If you do not specify the partition name, as is the case here, a default name will
be provided.
While this does offer a great deal of flexibility, it is not without cost. Since global partitioned indexes are
not directly related to any single table partition, operations that affect any partition in the table can render
all global partitioned indexes unusable. Specifically the operations:
ADD (HASH) - COALESCE (HASH) – DROP – EXCHANGE – MERGE - MOVE -SPLIT - TRUNCATE
Local Indexes
81
In contrast to the global index, a local partitioned index is individually created on each partition. If you
specify a local partitioned index, Oracle automatically maintains the index's partitioning along with that of
the underlying table.
Local partitioned indexes are created through the use of the LOCAL parameter with the CREATE INDEX
statement. It is unnecessary to provide partitioning information because the underlying table partitioning
will be used. A local index can be created with the following syntax:
Because the index is local, all partition changes to the table will be automatically reflected on the index
partitions as well.
Local partitioned indexes have some inherent advantages that are similar to the advantages you get from
partitioned tables. These advantages include the following:
•Because the index exists entirely on one partition, any maintenance operations affect only that one
partition.
•The Oracle optimizer can use the local index to generate better query plans based on the fact that a
local index is used.
•If a partition is lost and must be recovered, only the data and index for that particular partition needs
to be recovered. With a global index, the entire index would need recovery.
As you can see, there are many advantages of using both global and local partitioned indexes.
2- Index-Only Tables
Many systems contain several small tables (1 to 3 columns) where all of the elements form the primary
key. This is typically the case of tables created to physically implement conceptual relations of the type
O,n O,n. However, there exists an extremely efficient way to create such tables, by using a B*-Tree
structure. In ORACLE 8, an index-organized table, that is a table with the same physical structure as an
index, allows us to do exactly that. In an index-organized table, the database engine will place the data
values in a « table » segment, but with a B*-tree structure.
An index-only table is a schema object introduced in Oracle8. An index-only table is similar to an index,
but whereas an index contains the primary key value and a ROWID pointing to where the data is kept, the
index-only table stores the column data in the leaf block of the index.
Because the leaf blocks of the Oracle index are traditionally very small and tightly packed, there can be
some drawbacks to having large rows stored there. Oracle has developed a way to compensate for this: If
rows become too large (by a set threshold), the row data is stored in an overflow area as specified in the
CREATE TABLE statement. This creates storage more like the traditional index and table relationship.
An index-only table contains the same structure as the Oracle B*-tree index. Only the leaf blocks have
changed. Index-only tables have many of the attributes of both indexes and tables, but there are a few
exceptions:
•Because it is part index and part table, no other indexes can be added to the index-only table.
•The UNIQUE constraint is not allowed on an index-only table.
•A trigger can be added to the index-only table.
•An index-only table cannot be stored in a cluster.
•Replication is not supported at this time.
As you can see, there are some restrictions on index-only tables, but there is also a great deal of benefits.
When performing inserts, there is a performance improvement, since only a single segment needs to be
updated, which is the table itself. Data retrieval is also faster since the optimizer processes the table just
as if it were an index. Furthermore, one read operation is saved since ORACLE doesn't have to read the
index prior to performing the read in the table.
ANLYSIS:
This specifies that the index-only tablespace be created on the DOGS tablespace, whereas the overflow
tablespace used is DOGS2.
NOTE: It is necessary to specify the PRIMARY KEY value when creating an index-only table. This is
the value on which the index is created.
Unfortunately, there are also some disadvantages to using index-organized tables. For example, it is not
possible to create additional indexes since the occurrences of this type of table do not have a ROWID. It is
also impossible to add new columns or to modify existing columns. The message « ORA-25182 feature not
currently available for index-organized table » is used to alert us to these restrictions. Another factor to
consider is the case where updating key values is allowed. ORACLE
must delete the old record and do an insert based on the new key value, that is relocate a leaf of the B*-
tree to another location in the B*-tree. The net result is 2 SQL operations. Therefore, one should weight
the advantages and disadvantages before choosing this type of table, even though it is relatively easy to
convert an index-organized table back to a conventional format using create table as and then doing an
Import/Export to that table.
3- Bitmap Indexes
Another type of index available the bitmap index. With the traditional index you saw earlier, Oracle uses a
B*-tree method to traverse the index to find the leaf block. With a bitmap index, a bitmap of ROWIDs is
kept; this bitmap indicates which rows correspond to the index item. If the bit is set, this indicates that
the corresponding row contains the key value; if the bit is not set, the opposite is true.
As you can probably tell, bitmap indexes can probably be quite useful under the right circumstances, and
useless overhead otherwise.
SELECT employee_id
FROM payments
WHERE (salary + bonus) > 1000;
You need to enable function-based indexes before you can use them, as follows:
Or
QUERY_REWRITE_INTEGRITY = TRUSTED
QUERY_REWRITE_ENABLED = TRUE
COMPATIBLE = 8.1.0.0.0 (or greater)
6- Descending Indexes
Descending indexes are a special type of function-based index, in which index entries are sorted in
descending order. Using the DESC clause does this. When creating an index, as in the example below:
create index ix_desc
on phonebook2(zip desc);
SELECT name,height,lf_rows,lf_blks,del_lf_rows,distinct_keys,used_space
FROM INDEX_STATS;
SELECT name,height,lf_rows,del_lf_rows,
(del_lf_rows/lf_rows)*100 as ratio
FROM INDEX_STATS;
In this example, the ratio of deleted leaf rows to total leaf rows is clearly above 20%. This is a good
candidate for rebuilding. Let’s rebuild the index and examine the results.
ALTER INDEX item_basket_pk REBUILD;
SELECT name,height,lf_rows,del_lf_rows,
(del_lf_rows/lf_rows)*100 as ratio
86
FROM INDEX_STATS;
The index is rebuilt and validated once again. Examining the INDEX_STATS table shows that the 74
deleted leaf rows were dropped from the index. Notice that the total number of leaf rows went from 235 to
161, which is a difference of 74 leaf rows. This index should provide better performance for the
application.
Below is a sample script that can be run to determine which indexes need to be rebuilt. For those indexes
that need to be rebuilt, the ALTER INDEX REBUILD command is dynamically generated as output. The user
can tailor the height and percentage of deleted leaf rows by altering the vMaxHeight and vMaxDel
variables. The output of this script can be spooled to a file. This file can then be run to rebuild the indexes.
-- validate_idx.sql
--
-- This script will check indexes to find candidates for rebuilding.
-- Run this script in SQL*Plus as a user with SELECT ANY TABLE
-- privileges.
--
-- This script can be used and modified without permission. Run this
-- script at your own risk! The script author is not responsible for
-- any problems that may arise from running this script.
DECLARE
vOwner dba_indexes.owner%TYPE; /* Index Owner */
vIdxName dba_indexes.index_name%TYPE; /* Index Name */
vAnalyze VARCHAR2(100); /* String of Analyze Stmt */
vCursor NUMBER; /* DBMS_SQL cursor */
vNumRows INTEGER; /* DBMS_SQL return rows */
vHeight index_stats.height%TYPE; /* Height of index tree */
vLfRows index_stats.lf_rows%TYPE; /* Index Leaf Rows */
vDLfRows index_stats.del_lf_rows%TYPE; /* Deleted Leaf Rows */
vDLfPerc NUMBER; /* Del lf Percentage */
vMaxHeight NUMBER; /* Max tree height */
vMaxDel NUMBER; /* Max del lf percentage */
CURSOR cGetIdx IS SELECT owner,index_name
FROM dba_indexes WHERE OWNER NOT LIKE 'SYS%';
BEGIN
/* Define maximums. This section can be customized. */
vMaxHeight := 3;
vMaxDel := 20;
END LOOP;
CLOSE cGetIdx;
END;
/
Database Comparision
Table of Contents
1.Introduction
2.Operational Concerns
a.Scalability
b.Platform Availability
a.Licensing
88
b.Support and Maintenance
4.User Considerations
a.DBA Concerns
b.Programmability
5.Conclusion
SQL Server 7.0 is designed to scale from laptop databases and small business servers all the way up to
terabyte-size databases.SQL Server 7.0 is designed to operate on Microsoft’s Windows 95, 98 and NT
operating systems.However, the Windows 95 & 98 versions of SQL Server 7.0 only support desktop, laptop
and small workgroup applications.SQL Server 7.0 for Windows NT Workstation has been developed for
applications that involve a large numbers of users and transactions.
Microsoft SQL Server had 30% of the Windows NT database market in 1998, while Oracle had 46.1% of
the Windows NT market, which was a 55% growth in 1998.This statistic must be understood in the
context that it applies only to the Windows NT platform, since that is the only platform that both Microsoft
and Oracle have in common.
Although, Microsoft has taken a big step forward in enterprise capability with the SQL Server 7.0, it seems
like their product is more appropriate for departmental and small to mid-sized companies.“Rewritten for
ease of use, Microsoft’s SQL Server 7.0 is far and away the best choice for smaller organizations or branch
offices that need a full-featured relational database.Larger organizations will find SQL Server a better
performer than ever before, although competing databases including Oracle 8 and DB2 continue to provide
better programmability and scalability.”[ii]
In speaking with a few experienced database administrators and programmers, SQL Server really is not
known as a competitor with the “big boys” of the relational database software market.The well-known
database software products include Oracle, IBM’s DB2, Sybase and Informix.These companies remain
leaders in database technology at the enterprise level.
Oracle 8i Introduction
Oracle 8i is a database product that is built upon a mature Oracle 8 product, but also brings increased
capabilities to develop and integrate with Internet applications.Oracle’s databases have been developed
and proven to handle the largest of enterprise databases.But Oracle also targets smaller, mid-tier
companies who find it necessary to have 24 hours by 7 days availability due to increasing Internet
business needs.Oracle’s 8i databases are available on a wide variety of platforms.Dell, IBM, Linux, Sun,
Fujitsu and Unisys are a few of the 21 listed on Oracle’s web site.
89
Oracle’s products are definitely not the cheapest on the market. If an evaluation of the application
necessitates a high need for reliability, scalability, security and performance then Oracle should be
considered. Oracle is the world’s leading supplier of software for information management, holding 27% of
the database market share across all platforms.Oracle is the undisputed database leader on UNIX
platforms, commanding 60.9% of the market share according to Dataquest.[iii],[iv]
2.Operational Concerns
a. Scalability
Scalability in the context of database software is defined as the software’s ability to continue to perform at
a similar level with a larger amount of data and a growing number of users and transactions.
Amount of Data
Both SQL Server 7.0 and Oracle 8i are designed to be client-server database products that can take
advantage of distributed database architecture.
A distributed database is a network of databases managed by multiple database servers that appears to a
user as a single database. This means the database could be distributed across several disks and servers
with multiple processors.The data of all databases in the distributed databases can be simultaneously
accessed and modified. The database architecture on the server will dictate how fast the transaction
response time is.The speed of transactions can vary greatly based on the database design as well as
server hardware configurations, including RAM, the number and speed of the CPUs.
SQL Server 7.0 can grow up to 1,048,516 terra-bytes.Microsoft uses SMP (systems with 4 processors)
technology to distribute databases. Other maximum sizes and numbers can be referenced in Appendix C,
which outlines other technical specifications of SQL Server 7.0.
Oracle 8i is scalable up to hundreds of terabytes to manage very large databases (VLDB).Oracle takes
advantage of distributed processing and storage capabilities through architectural features that use more
than one processor to divide the processing for a set of related jobs.This distributed architecture is a good
example of the expression “the sum of the parts is greater than the whole”, because as individual
processors work on a subset of related tasks, performance of the whole system is improved.
Theoretically, there is no limit to the number of users that can access either the Oracle 8i or SQL Server
7.0 database servers at one time, given infinite processors and infinite speed.In practical terms, there is a
limit, but it should not pose any real issues to be concerned with in terms of concurrent data.One
consideration is that SQL Servers will require ODBC software to connect with clients that are not PC
based.This will require some overhead, but should be pretty negligible.
In a “Score Card” published by ZDNet[v], which is fully documented in Appendix D, the following ratings
were published.These ratings really demonstrate the equality of SQL Server and Oracle for these
performance criteria.
b.Platform Availability
As discussed in the Introduction, SQL Server 7.0 is designed to operate on Microsoft’s Windows 95, 98 and
NT 4.0 operating systems.However, the Windows 95 & 98 versions of SQL Server 7.0 will only support
desktop, laptop and small workgroup applications and requires an Intel platform.
SQL Server 7.0 for Windows NT Workstation has been developed for applications that involve a large
numbers of users and transactions and is limited to Intel or Alpha platforms.
This limitation could cause hurdles for large corporations in terms of performance that can be
expected.Brian McCarthy, CEO of Insurance Holdings said “Microsoft said we’d need an all-Microsoft
application if we wanted full scalability, but who’s going to rebuild the whole system?”[vi]
It’s especially important to note that although other platforms can be used for clients to access the SQL
Server 7.0, a third party ODBC software must be used.ODBC is an interface that allows for data to be
accessed in relational databases, independent of the database vendor.
The need for a database to be Internet ready is quickly becoming a necessity in today’s rapidly
growing web based world. At every turn in the software industry, the topic of e-business and e-
commerce is a top issue for companies wanting to compete in the future. Therefore, the need
for database software to support and enhance Internet application development is a must.
SQL Server 7.0 still lags behind in its ability to support multimedia data support and in
programmability, which are necessary for many Internet applications. Third party software will
have to be used to store special images, sound, video or geographic data support. SQL Server
7.0 doesn’t support Java, which is an industry standard for developing network applications.
Oracle 8i is the best product for companies wanting to move their database applications to the
Web. Oracle leads the market in handling of multimedia objects. Multimedia support is
particularly relevant when building Web-based applications like online stores that include
91
multimedia items such as pictures or video clips of items for sale. Oracle uses a product call
JServer, which brings Java and relational databases together. It allows for controlling the
database through Java and supports the creation of JavaBeans. JavaBeans are the basic
building blocks for Java-based Internet applications, and are (or will be) supported by just
about every high-end Internet application server on the market.
In the ZDNet Scorecard (Appendix D), SQL Server 7.0 and Oracle 8i were rated as follows, in
respect to their internet readiness features:
a.Licensing
SQL Server
Pricing structures of software can be complex.Microsoft recognizes this and even offers “A Guide to
BackOffice Pricing and Licensing” to help readers understand some of the subtleties and complexities
before “cutting a check”. SQL Server can be purchased by itself in either of the above-mentioned versions,
as an upgrade to certain other database products or as a part of Microsoft BackOffice.A license for the
server is distinct from the licenses for the CAL (client access license), but every server is accompanied
with a certain number of CALS.Although the number of CALS may vary by application, there would always
be a need for at least one CAL or the database could not be accessed to retrieve data.
To compare prices the details of the system architecture must be well understood.Some of those details
include the number of servers required to support the database, how many processors a server will have,
the number of users needing to access the database, whether the system will be serving an Internet or
intranet application.
Based on an application where 250 or more users (CALs) will be accessing one database server, the cost of
SQL Server 7.0 Enterprise Version is listed as $28,999 on Microsoft web site.To get an understanding of
the price differences between the Enterprise version and the Standard version, SQL Server 7.0 Standard
Version and Five CALs would cost $3,999, while the Enterprise version with Five CALs lists at $7999.An
upgrade of the SQL Server 7.0 Enterprise version for a customer who is also upgrading the clients
accessing the database costs $3969, while a customer who is buying licenses for new clients along with
the server license costs $7099.
The benefits of the Enterprise Edition over the Standard Edition cited in the Microsoft publications get
blurred in the marketing jargon.The Enterprise version is used for increased system scalability and
reliability by providing support for SMP (systems with multiple processors) and extended memory
These prices are negotiable and the price varies in terms of how customer acquires the SQL Server.For
example, if a customer purchases software that runs on SQL Server or purchases it from an independent
software vendor then the pricing would obviously be different.
92
Oracle 8i
Oracle’s pricing structure is different from Microsoft’s, in that it doesn’t charge per server license or client
access license.Rather Oracle charges by licensing units:“named user”, “concurrent system” and “power
unit”.
A named user is defined as “an individual who is authorized by his/her company to use the Oracle
Software programs, regardless of whether the individual is actively using these programs at any given
time.”[vii]
A concurrent device is defined as “an input device accessing the program on the designated system at any
given point in time. The number of "Concurrent Devices" you are licensed for is the maximum number of
input devices accessing the programs on the Designated System at any given point in time. If multiplexing
hardware or software (e.g., a TP Monitor or a Web server product) is used, this number must be measured
at the multiplexing front-end.”vii
A power unit is defined as one MHz of power in any Intel compatible or RISC processor in any computer of
the Designated Systems on the Order Confirmation page on which the Oracle software programs are
installed and operating. (Intel refers to Intel Solaris, Linux, and Windows NT; RISC refers to Sun SPARC
Solaris, HP-UX, and IBM/AIX. A "Processor" license shall be for the actual number of processors installed
in the licensed computer and running the Oracle program(s), regardless of the number of processors
which the computer is capable of running.)”vii
A named user licensing unit costs $600, a concurrent device costs $1495 and a power unit costs $200 for
the Oracle 8i Enterprise Edition.These prices are 5 times more than what Oracle charges for the Standard
Edition.
The Enterprise Editions includes these advanced features on top of the Standard Edition:large-database
partitioning (which helps you keep monster gigabyte-size databases under control), flexible security
features, and speed features such as bitmapped indexes, summary tables, and parallelism.
Two other modules that Oracle offers for enhanced web integration and multi-media handling are Oracle
JServer Standard Edition and WebDB, which if necessary add to the total cost of the Oracle solution.They
are also priced based on the licensing unit discussed above.
Availability of qualified database administrators (DBA) and programmers is one issue that cannot be
overlooked in considering which database will be best for the given organization and application.Due to the
relationship between supply, demand and cost, the shortage of Oracle DBAs and programmers can mean
only mean one thing.They are hard to find and when one is available, they command a very high salary.
The nature of Oracle is that it can be more difficult to program and administer, so it requires specially
trained personnel.SQL Server, on the other hand, is an easier product to learn and administer so the
number of available programmers is higher and less expensive to staff a database project.
No cost information could be obtained on the annual maintenance fees to remain current on the licensing
agreements with either Oracle or SQL Server.
4. User Considerations
Both SQL Server 7.0 and Oracle make commitments on a two-phase commit approach, which allows for
users to control a logical set of SQL statements so they all either succeed or fail as a unit. This two-phase
mechanism guarantees that no matter what type of system or network failure might occur, a distributed
transaction either commits on all involved nodes or rolls back on all involved nodes to maintain data
consistency across the global distributed database.
SQL Server 7.0 is an exceptionally easy product to administer and is more forgiving than previous SQL
Server versions.SQL Server 7.0 has an auto-tuning feature that allows for memory to be self-managed
and there are several new wizards that simply advanced tasks such as creating databases, scheduling
backups, importing and exporting data and configuring replication.This should make the training of
database administrators much easier.
Oracle 8i databases can be administered and controlled very tightly, but it is a complex and requires
trained database administrators to do so proficiently.“Oracle8i tools are Java-based and can even be run
from a Web browser. They provide all the essentials for designing and setting up a database, including
some advanced features like letting you selectively delegate authority to users of its Enterprise Manager
administration console. This is a handy tool for branch office deployment.Like previous releases of
Enterprise Manager, though, this one is a version behind the database, and it doesn't know a thing about
new Oracle8i features such as Java stored procedures.”[viii]
b.Programmability
There are languages supported within the database software for programming and controlling the
database.For example, since PL/SQL can be stored in the database, network traffic between applications
and the database is reduced, thereby increasing application and system performance.
SQL Server 7.0 comes with an internal programming language called Transact-SQL, which has received a
poor rating in several reviews.“While everyone else in the SQL database market is moving (or has already
moved) to a modern programming language like Java, SQL Server customers are still stuck in the
programming Dark Ages—no object orient development, no big class libraries to use, and no code
interoperability with anything else.”[ix]The programming can be done, but it will require a lot more work.
Oracle gets an excellent rating for it’s internal language offerings, which include Java and PL/SQL.
5.Conclusion
In comparing these two database products, it became apparent they each hold a different place and
purpose in the market.They don’t compete in the same niche.Microsoft SQL Server, a client-server
database, continues to make strides toward the enterprise database market, but is still most appropriate
for a departmental or small to mid-sized company whose database doesn’t have such high scalability,
reliability and availability needs.SQL Server’s greatest weakness is the Windows NT platform, which it
operates on, is not mature enough to provide the kind of availability that enterprise worthy systems
require.“In the small-business market, the differentiating factors are ease of database administration, Web
connectivity, the speed and features of the database server engine, branch-office and mobile support, and
94
the ability to warehouse data efficiently. SQL Server 7.0 shines in all of these areas except Web
connectivity. Its administration tools include many wizards and self-tuning settings that make it the only
database we reviewed that might not require a specially trained administrator.”[x]
Oracle, also a client-server database, operates on the high end of the database market and is also
reaching out to start ups, small to medium sized businesses who have a need for a complete, integrated
platform for critical applications for the internet. Oracle is harder to administer is an expensive choice,
unless the application being developed requires its Java or multimedia features.Another selling point to
Oracle is that is it sold on a multitude of platforms, in comparison to SQL Server 7.0, which may be
appealing to some customers who are seeking a more mature platform.
Appendix A
Server
Microsoft Internet Explorer 4.01 with Service Pack 1 or later (both included)
32 MB of RAM
CD-ROM drive
Note SQL Server 7.0 can utilize up to four processors. Additional processor support is available with SQL
Server 7.0 Enterprise Edition.
Desktop
Identical to Server requirements with the following exceptions:
95
Each installation of SQL Server Desktop requires a per-seat client access
license for SQL Server 7.0; SQL Server Desktop will only interact with SQL Server in per-seat
mode
Networking Support Windows 95, Windows 98, or Windows NT built-in network software (additional
network software is not required unless you are using Banyan VINES or AppleTalk ADSP; Novell NetWare
client support is provided by NWLink)
Clients Supported Windows 95, Windows 98, or Windows NT Workstation, UNIX,** Apple Macintosh,**
and OS/2**
*Actual requirements will vary based on your system configuration and the features you choose to install.
Appendix B
This table specifies the maximum sizes and numbers of various objects defined in Microsoft SQL Server
databases, or referenced in Transact-SQL statements.
* Database objects include all tables, views, stored procedures, extended stored procedures, triggers,
rules, defaults, and constraints.
Appendix C
Appendix D
Appendix E
[i]
Dyck, Timothy. “Mission Critical.” PC Magazine.July 30, 1999.November 1, 1999
<http://www.zdnet.com/products/stories/reviews/0,4161,408644,00.html.
[ii]
Dyck, Timothy. “SQL Server makes enterprise in roads.” PC Week. November 10, 1998.November 6,
1999 <http://www.zdnet.com/pcweek/stories/news/0,4153,372285,00.html.
[iii]
O’Neill, Paige. “Oracle Trumps Microsoft in Battle for NT Database Marketshare.”March 29, 1999.October
31, 1999. <www.oracle.com/cgi-bin/press/printpr.cgi?file=199903290500.29144.html&mode=corp.
[iv]
“Oracle Charts Landmark Year for Oracle 8; Sets the Stage for the Next Release, Code-Named
Emerald.”July 2, 1999. November 5, 1999 <www.uk.oracle.com/info/news/emerald.html.
102
[v]
“Score Card.” PC Magazine.July 29, 1999.October 27, 1999
<www.zdnet.com/products/stories/reviews/0,4161,2304448,00.html.
[vi]
Deck, Stewart. “SQL users turn to Oracle 8 for bulk.” Computerworld. May 10, 1999.October 15, 1999
<www.computerworld.com/home/print.nsf/all/990510A506.
[vii]
“Licensing Terms.”November 1, 1999
<http://oraclestore.oracle.com/cec/cstage?eccookie=&ecaction=ecpassthru&template=help_licensing.en.h
tm.
[viii]
Dyck, Timothy.“Oracle 8i.”PC Magazine.July 29, 1999.November 6, 1999
<http://www.zdnet.com/products/stories/reviews/0,4161,408777,00.html.
[ix]
Dyck, Timothy.“SQL Server 7.0”PC Magazine.July 29, 1999.November 11,
1999<http://www.zdnet.com/products/stories/reviews/0,4161,408776,00.html.
[x]
Dyck, Timthy, “Editor’s Choice:SQL Server Databases.”PC Magazine.July 30, 1999.November 9,
1999<http://www.zdnet.com/products/stories/reviews/0,4161,408779,00.html.
Other References
www.oracle.com
www.microsoft.com
I will try to explain here at some of the Oracle features and techniques that can be employed to speed up
data loads from flat files into Oracle tables. To demonstrate and compare data loading methods, I loaded a
sample data set using each of the techniques that Oracle provides. I compared elapsed time, CPU time,
and caveats of each method, but please note that timings can vary from one run to the next and all
results depends on DB Activity at that time.
To demonstrate various techniques for loading flat file data into Oracle tables, I worked with a flat file that
contains 100,000 records of data into a call center table. The file is approximately 6 Mb in size, and a few
sample records in the file look like this:
103
82302284384,04-18-2003:13:18:58,5001,COMPLAINT,SYS TICKET 183487923
82302284385,04-18-2003:13:18:59,3352,INFO-REQUEST,PAYMENT ADDRESS
82302284386,04-18-2003:13:19:01,3142,DOC-REQUEST,2002 YE CONSOL STMT
About the simplest way for an application to load data into Oracle is to insert one row at a time using the
INSERT statement with the VALUES clause. This technique is convenient for loading small amounts of
data, such as a SQL*Plus script that creates seed data at the time of application installation. However, this
approach is quite slow when compared to more sophisticated techniques, and therefore is not appropriate
for loading large volumes of data into Oracle.
Depending on the application and data volume involved, this most basic method for loading data into
Oracle might give acceptable performance. If that is the case, then there is no need to over-engineer an
unnecessarily complicated load program. However, if single row inserts are not fast enough, then there are
more sophisticated data load methods to consider.
The chief advantages of the single row insert method are simplicity and flexibility. The key disadvantage of
this technique is that it is the slowest and most CPU-resource intensive.
When a row is inserted into a table, Oracle needs to update all indexes on the table at the same time. It is
typically faster for Oracle to build an index from scratch after the table is populated rather than repeatedly
update an index as rows are inserted. It is often true that the index will be more compact and efficient as
well. These rules of thumb are especially true when bitmap indexes are involved.
From this, it follows that one way to speed up data loads would be to drop the indexes before loading the
data and recreate the indexes after the load is complete. For primary and unique key constraints, you can
get the same effect by disabling or dropping the constraint. (Be careful of the impact this might have on
foreign key constraints in other tables.)
This technique might not be appropriate if you are loading data into a table that already has a lot of rows
in it. Consider a table with 99 million rows and you wish to load 1 million more rows. The time saved in
loading the 1 million new rows will be lost when Oracle has to rebuild an index on the 99 million existing
rows plus the 1 million new rows.
Speeding up index creation is another issue to consider. Briefly, you can reduce the amount of sorting that
must take place on disk by increasing the sort_area_size in your session (if you are not using Oracle 9i
PGA aggregate targets). This allows your session to perform more of the sorting during index creation in
memory. Also, you can use the NOLOGGING keyword to reduce the amount of redo Oracle generates
104
during the index build. NOLOGGING has significant impacts on recoverability and standby databases, so
do your homework before using the NOLOGGING keyword.
I modified the application used in the last section to disable the primary key on the CALLS table and drop
its one non-unique index before loading the data, putting both back after the load was complete. In this
example, the CALLS table was empty before the data load. Factoring in the amount of time required to
recreate the two indexes, elapsed time for the load dropped from 172 seconds to 130 seconds. CPU time
used by the database server process dropped from 52 seconds to 35 seconds.
Dropping and rebuilding indexes before and after a data load can speed up the load and yield more
efficient indexes. Some drawbacks include the added complexity and potential embedding of schema
design information into the application code. (When you add another index to the table being loaded, will
you have to update your application code?) Dropping indexes before a load could also have significant
performance impacts if users need to be able to query the target table while the load is taking place.
Finally, dropping or disabling primary or unique key constraints could cause difficulties if foreign key
constraints reference them.
I modified the application used in the last example to load call center records into arrays in memory and
perform bulk inserts 100 rows at a time instead of one row at a time. I retained the logic that dropped the
indexes and rebuilt them after the data load. Elapsed time to load the data dropped from 130 seconds to
14 seconds, while CPU time used by the database server process dropped from 35 seconds to 7 seconds.
Of course, the amount of time required to rebuild the indexes was not affected by the use of bulk inserts.
The actual time to load the data (setting aside the index rebuild time) dropped by about 95%.
A clear advantage of the bulk insert technique is that load time drops dramatically, as does CPU usage.
One disadvantage of this technique is slightly increased complexity in application coding and a greater
opportunity for bugs. This disadvantage is very modest.
An external table looks somewhat like a regular table in that it has columns with standard Oracle data
types and you can query it just like any other table. However, the table data is not stored inside the
database. Instead, the external table is associated with a flat file on the database server. Whenever you
query the external table, Oracle parses the flat file and returns the rows to you as if the data were stored
in Oracle.
105
We apply this technique for Bank of America and BankLink.
You can join external tables to other tables in your queries, but you cannot index external tables nor can
you insert, update, or delete rows. If the flat file on the database server changes or is deleted, this will
affect the outcome of future queries against the external table.
External tables open the door to new strategies for loading data into Oracle. We can put the flat file on the
database server and create an external table in the database. At that time we can instantly query the data
as if it had been loaded into a conventional Oracle table. If the flat file gets removed or overwritten, the
old contents will no longer be accessible in the database. Probably we can implement this for issuedata
and images files?
However, we can (if needed) use an external table as a vehicle to copy data quickly from a flat file into a
conventional Oracle table.
I wrote a SQL*Plus script to drop the CALLS table, recreate it from the CALLS_EXTERNAL table as shown
above, and add the primary key and non-unique index. Populating the CALLS table in this way took 15
seconds, using 8 seconds of CPU time in the database server process. This case was slightly slower than
the bulk insert method discussed in the last section.
One advantage of this technique is that it achieves very good performance with very little coding effort. It
is also a method that works entirely within the database, so we can even schedule data loads using the
Oracle dbms_job scheduler.
SQL*Loader is a utility provided by Oracle specifically for the purpose of loading large volumes of data
from flat files into Oracle tables. It is very powerful, flexible, and fast. However, we must launch it from
the operating system. (Invoking SQL*Loader within a dbms_job or a PL/SQL stored procedure is tricky but
we can do it.).
SQL*Loader still seems to be about the fastest and most efficient way to get flat file data into Oracle. By
default, SQL*Loader uses what it calls "conventional path" loading–bulk inserts, basically. The
performance is not phenomenal, and there are faster alternatives. However, with a simple "direct=true" on
the command line, we can invoke "direct path" loading. In a direct path load, SQL*Loader writes rows
directly into new data blocks above the table's high water mark. This is like inserting with the Append hint
as seen in the last section, but it's even better. In a direct path load, SQL*Loader also updates indexes
using a very efficient bulk method.
Loading the call center data using SQL*Loader with conventional path load took about 81 seconds. The
database server process used about 12 seconds of CPU time. This was with indexes in place and
SQL*Loader bulk inserting rows 100 at a time. With a direct path load, SQL*Loader got the data into the
database in just 9 seconds, with only 3 CPU seconds used by the database server process. This was with
indexes in place.
Direct path SQL*Loader is fast and efficient. It is even fast when indexes on the target table are not
dropped before the load. As for disadvantages, direct path loading has implications for recovery akin to
the NOLOGGING keyword. Also, indexes on the target table are unusable for a period during the load. This
can impact users trying to access the target table while a load is in progress. Also, indexes can be left in
an unusable state if the SQL*Loader session were to crash midway through the load.
There are many different ways to load data into Oracle. Each technique offers its own balance between
speed, simplicity, scalability, recoverability, and data availability.To recap, here are all of the timing figures
in one place:
Please keep in mind that I did not even touch on the subject of parallelism in data loads. (Inserts with the
Append hint can use parallelism in the Enterprise Edition of the Oracle software.
Oracle XML
XML (eXtensible Markup Language) is a W3C initiative that allows information and services to be encoded
with meaningful structure and semantics that both computers and humans can understand. XML is great
for information exchange, and can easily be extended to include user-specified and industry-specified
tags. Look at this simple example defining a FAQ:
<?xml version="1.0"?>
<!DOCTYPE question-list SYSTEM "faq.dtd">
<?xml-stylesheet type="text/xml" href="faq.xsl"?>
<FAQ-LIST>
<QUESTION>
<QUERY>Question goes here</QUERY>
<RESPONSE>Answer goes here.</RESPONSE>
</QUESTION>
107
<QUESTION>
<QUERY>Another question goes here.</QUERY>
<RESPONSE>The answer goes here.</RESPONSE>
</QUESTION>
</FAQ-LIST>
A Document Type Definition (DTD) defines the elements or record structure of a XML document. A DTD
allows your XML files to carry a description of its format with it. The DTD for the above XML example looks
like this:
<?xml version="1.0"?>
• #PCDATA (parsed character data) means that the element contains data that can be parsed by a
parser like HTML
• The + sign in the example above declares that the "QUESTION" element must occur one or more
times inside the "FAQ-LIST" element.
• The * sign in the example above declares that the "QUERY" element can occur zero or more times
inside the "QUESTION" element.
The W3C also formulated a new standard, called XML Schemas that superceded DTD's. Schemas allow for
more complex data types within your tags and better ways to constrain (validate) data within these tags.
• XMLDB
Standard option that ships with the Oracle 9i database (from 9.2.0). Previously called
Project XDB.
connect scott/tiger
set serveroutput on
DECLARE
Ctx DBMS_XMLGEN.ctxHandle; -- Var's to convert SQL output to XML
xml clob;
emp_no NUMBER := 7369;
connect scott/tiger
set serveroutput on
DECLARE
Ctx DBMS_XMLQuery.ctxType; -- Var's to convert SQL output to XML
xml clob;
emp_no NUMBER := 7369;
How does one store and extract XML data from Oracle?
XML data can be stored in Oracle (9.2.0 and above) using the XMLType data type. Look at this example:
connect scott/tiger
Beginning with the first release of Oracle 8i, Oracle has offered the XDK for the Oracle database. For
Oracle 8i releases 1 and 2 (8.1.5 and 8.1.6) you must download the XDK from Oracle Technology Network
and install it manually. With Oracle 8i release 3 (8.1.7) and Oracle 9i, the XDK is integrated into the
database and is installed automatically when the database is installed.
The XDK may be accessed from PL/SQL, Java, C, and C++ applications. PL/SQL applications must run
inside the database (i.e. stored procedures, packages, triggers, or anonymous PL/SQL blocks) in order to
access the XDK. Java applications may reside inside or outside the database. C and C++ applications, of
course, reside outside the database.
In order to access the XDK from PL/SQL or Java applications residing inside the database, you must install
Oracle’s JVM (sometimes called the Java option or Oracle JServer). It may seem counter-intuitive that you
must have Oracle’s JVM installed in order to use the XDK from PL/SQL, but there is a reason for it: The
XDK is itself written in Java. Oracle merely put PL/SQL wrappers on the Java code in order to make it
accessible from PL/SQL.
The XDK contains an XML parser, an XSLT processor, an XPath engine, an XSQL page processor and
servlet, and an XML SQL utility. The first three are Oracle’s implementations of the XML 1.0 specification,
while the last two are features unique to Oracle.
Note that the XSQL page facility is only accessible from Java applications. Also note that in earlier releases
of the XDK, the XML SQL utility was a separate tool that you had to install separately from the XDK. You
can download the XML SQL utility from Oracle Technology Network.
Hello! Could you elaborate the example of serializing the XML into a table,
let's say the XML file has data which corresponds to 2 tables (repetitive tags
for a detail table), is it possible?
110
Followup:
Sean here...
So you can see... the department data and the employee data co-mingled. We want
to normalize this into two tables... so here's what I'd do:
-----------------------------
system@SLAP> create table dept (
2 deptno number
3 primary key,
4 dname varchar2(30));
Table created.
system@SLAP> declare
2 l_clob clob := '<?xml version = "1.0"?>
3 <ROWSET>
4 <ROW num="1">
5 <DEPTNO>10</DEPTNO>
6 <DNAME>SALES</DNAME>
7 <EMPNO>100</EMPNO>
8 <ENAME>MARK JOHNSON</ENAME>
9 </ROW>
10 <ROW num="2">
11 <DEPTNO>20</DEPTNO>
12 <DNAME>TECHNOLOGY</DNAME>
13 <EMPNO>200</EMPNO>
14 <ENAME>TOM KYTE</ENAME>
15 </ROW>
16 <ROW num="3">
17 <DEPTNO>20</DEPTNO>
18 <DNAME>TECHNOLOGY</DNAME>
19 <EMPNO>300</EMPNO>
20 <ENAME>SEAN DILLON</ENAME>
21 </ROW>
22 </ROWSET>';
23
24 l_ctx dbms_xmlsave.ctxType;
25 l_rows number;
26 begin
27 l_ctx := dbms_xmlsave.newContext('DEPTEMP');
28 l_rows := dbms_xmlsave.insertxml(l_ctx,l_clob);
29 dbms_xmlsave.closeContext(l_ctx);
30 dbms_output.put_line(l_rows || ' rows inserted...');
31 end insert_xml_emps;
32 /
PL/SQL procedure successfully completed.
DEPTNO DNAME
112
---------- ------------------------------
10 SALES
20 TECHNOLOGY
-----------------------------
How can I handle this? One row for the department table and 3 rows for the
employee table in the same XML row.
<?xml version = "1.0"?>
<ROWSET>
<ROW num="1">
<DEPTNO>10</DEPTNO>
<DNAME>SALES</DNAME>
<EMPLOYEE>
<EMPNO>100</EMPNO>
<ENAME>MARK JOHNSON</ENAME>
</EMPLOYEE>
<EMPLOYEE>
<EMPNO>200</EMPNO>
<ENAME>VICTOR JAEN</ENAME>
</EMPLOYEE>
<EMPLOYEE>
<EMPNO>300</EMPNO>
<ENAME>JHON SMITH</ENAME>
</EMPLOYEE>
</ROW>
</ROWSET>
Thanks a lot!
Followup:
DBMS_XMLGEN.GETXML('SELECTDEPTNO,DNAME,CURSOR(SELECTEMPNO,ENAMEFROMEMPWHEREEMP.D
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
<ROW>
<DEPTNO>10</DEPTNO>
<DNAME>ACCOUNTING</DNAME>
<EMPLOYEE>
113
<EMPLOYEE_ROW>
<EMPNO>7782</EMPNO>
<ENAME>CLARK</ENAME>
</EMPLOYEE_ROW>
<EMPLOYEE_ROW>
<EMPNO>7839</EMPNO>
<ENAME>KING</ENAME>
</EMPLOYEE_ROW>
<EMPLOYEE_ROW>
<EMPNO>7934</EMPNO>
<ENAME>MILLER</ENAME>
</EMPLOYEE_ROW>
</EMPLOYEE>
</ROW>
</ROWSET>
Oracle8i ONLY
Oracle8i ONLY
maximum per
Backup
session
Sessions maximum per 32K, limited by PROCESSES and SESSIONS init parameters
instance
LCK maximum per 10
Processes instance
MTS maximum per Unlimited within constraints set by PROCESSES and SESSIONS init
Servers instance parameters, for instance
Dispatchers maximum per Unlimited within constraints set by PROCESSES and SESSIONS init
instance parameters, for instance.
Parallel maximum per Unlimited within constraints set by PROCESSES and SESSIONS init
Execution instance parameters, for instance.
Slaves
Backup maximum per Unlimited within constraints set by PROCESSES and SESSIONS
Sessions instance init
parame
ters, for instance.
Oracle 9i Limits
Datatype Limits
Database Block Size Minimum 2048 bytes; must be a multiple of operating system
physical block size
Maximum Unlimited
Redo Log Files Maximum number of Limited by value of MAXLOGFILES parameter in the
logfiles CREATE DATABASE statement. Control file can be
resized to allow more entries; ultimately an operating
system limit
Maximum number of
logfiles per group Unlimited
Redo Log File Size Minimum size 50 KB
Maximum size Operating system limit; typically 2 GB
Tablespaces Maximum number 64 KB Number of tablespaces cannot exceed the
per database number of database files, as each tablespace must
include at least one file
GROUP BY clause Maximum length The GROUP BY expression and all of the
nondistinct aggregates functions (for example,
SUM, AVG) must fit within a single database block.
Indexes Maximum per table Unlimited
total size of indexed column 75% of the database block size minus some
overhead
Columns Per table 1000 columns maximum
Per index (or clustered 32 columns maximum
index)
Per bitmapped index 30 columns maximum
Constraints Maximum per column Unlimited
Subqueries Maximum levels of subqueries Unlimited in the FROM clause of the top-level
in a SQL statement query;
255 subqueries in the WHERE clause
Partitions Maximum length of linear 4 KB - overhead
partitioning key
SQL Statement Length Maximum length of 64 KB maximum; particular tools may impose
statements lower limits
Stored Packages Maximum size PL/SQL and Developer/2000 may have limits on
the size of stored procedures they can call. The
limits typically range from 2000 to 3000 lines of
code. See Also: Your PL/SQL or Developer/2000
documentation for details
Trigger Cascade Limit Maximum value Operating system-dependent, typically 32
Oracle 8i Limits
Datatype Limits
maximum unlimited
Redo Log Files maximum number of logfiles LOG_FILES initialization parameter,
or
MAXLOGFILES in CREATE DATABASE;
controlfile can be resized to allow more
entries; ultimately an O/S limit
Item Limit
Names of databases 8 bytes
Names of database links 128 bytes
All other schema objects 30 bytes
I have found that a three (or four)-space indentation not only adequately reveals the logical structure of
the code but also keeps the statements close enough together to read comfortably. And, with deeply
nested structures, you won't run off the right margin as quickly! Here is the three-space indented version
of the previous nested IF statement:
IF to_number(the_value) > 22 THEN
IF max_totals = 0 THEN
calc_totals;
ELSE
WHILE more_data
LOOP
analyze_results;
END LOOP;
END IF;
END IF;
Application-specific identifiers are the names that you give to data and program structures that are
specific to your application and that vary from system to system. The compiler treats these two kinds of
text very differently. You can improve the readability of your code greatly by reflecting this difference in
the way the text is displayed. Many developers make no distinction between reserved words and
application-specific identifiers. Consider the following lines of code:
While the use of indentation makes it easier to follow the logical flow of the IF statement, all the words in
the statements tend to blend together. It is difficult to separate the reserved words and the application
identifiers in this code. Changing entirely to uppercase also will not improve matters. Indiscriminate, albeit
consistent, use of upper- or lowercase for your code reduces its readability. The distinction between
reserved words and application-specific identifiers is ignored in the formatting. This translates into a loss
of information and comprehension for a developer.
Using a mixture of upper- and lowercase words increases the readability of the code by giving a sense of
dimension to the code. The eye can more easily cruise over the text and pick the different syntactical
elements of each statement. You can focus quickly on the lowercase words for the application-specific
content. Consistent use of this method makes the program listings more attractive and accessible at a
glance.
It is very difficult to pick out the individual statements in this line, in addition to the fact that a procedure
is called in the middle of the line. By placing each statement on its own line you mirror the complexity of a
program--the simple lines look simple and the complex statements look complex--and reinforce the top-
to-bottom logic of the program:
new_id := 15;
calc_total (new_id);
max_dollars := 105 * sales_adj;
Use indentation to offset all continuation lines under the first line.
This is the most important guideline. The best way to identify continuation lines is to use indentation to
logically subsume those lines under the main or first line of the statement. The following call to
generate_company_statistics is obscured because the continuation line butts right up against the left
margin with the module name:
If I indent the continuation line, the relationship of the second line to the first becomes clear:
• Right-align the reserved words for the clauses against the DML statement.
I recommend that you visually separate the SQL reserved words which identify the separate
clauses from the application-specific column and
table names. The following table shows how I use right-alignment on the reserved words to create
a vertical border between them and the rest of
the SQL statement:
UPDATE employee
SET hire_date = SYSDATE
WHERE hire_date IS NULL
AND termination_date IS NULL;
This right alignment makes it very easy to identify the different clauses of the SQL
statement, particularly with extended SELECTs. You might also consider placing a blank line
between clauses of longer SQL statements (this is possible in PL/SQL, but is not acceptable
in "native" SQL executed in SQL*Plus).
It's impossible to read a program when a query has a six-table join and the tables have been
assigned aliases A, B, C, D, E, and F. How can you possibly decipher the WHERE clause in the
following SELECT?
SELECT ... select list ...
FROM employee A, company B, history C, bonus D, profile E, sales F
WHERE A.company_id = B.company_id
AND A.employee_id = C.employee_id
AND B.company_id = F.company_id
AND A.employee_id = D.employee_id
AND B.company_id = E.company_id;
With more sensible table aliases (including no tables aliases at all where the table name was short
enough already), the relationships are much clearer:
SELECT ... select list ...
FROM employee EMP, company CO, history HIST, bonus, profile PROF, sales
WHERE EMP.company_id = CO.company_id
AND EMP.employee_id = HIST.employee_id
AND CO.company_id = SALES.company_id
AND EMP.employee_id = BONUS.employee_id
AND CO.company_id = PROF.company_id;
125
EXCEPTION
WHEN NO_DATA_FOUND THEN
executable_statements1;
FUNCTION
company_name (company_id_in IN company.company_id%TYPE) RETURN
VARCHAR2 IS cname company.company_id%TYPE; BEGIN
SELECT name INTO cname FROM company
WHERE company_id = company_id_in;
RETURN cname;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
You know that this program is a function because the first word in the program is FUNCTION. Other than
that, however, it is very difficult to follow the structure of this program. Where is the declaration section?
Where does the executable section begin and end? Here is that same function after we apply some
straightforward formatting rules to it:
FUNCTION company_name (company_id_in IN company.company_id%TYPE)
RETURN VARCHAR2
IS
cname company.company_id%TYPE;
BEGIN
SELECT name INTO cname FROM company
WHERE company_id = company_id_in;
RETURN cname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
The declaration section, which comes after the IS and before the BEGIN, clearly consists of a single
declaration of the cname variable. The executable section consists of all the statements after the BEGIN
and before the EXCEPTION statement; these are indented in from the BEGIN. Finally, the exception
section shows a single specific exception handler and a WHEN OTHERS exception. Generally, indent the
statements for a given section from the reserved words which initiate the section. You can also include a
126
blank line before each section, as I do above, for the executable section (before BEGIN) and the exception
section (before EXCEPTION). I usually place the IS keyword on its own line to clearly differentiate between
the header of a module and its declaration section.
/*
===========================================================
| Parameter Description |
| |
| company_id The primary key to company |
| start_date Start date used for date range |
| end_date End date for date range |
===========================================================
*/
The right-justified vertical lines and column formatting for the parameters require way too much effort to
enter and maintain. What happens if you add a
parameter with a very long name? What if you need to write a longer description? A simpler and more
maintainable version of this comment might be:
/*
===========================================================
| Parameter - Description
|
| company_id - The primary key to company
| start_date - Start date used for date range
| end_date - End date for date range
===========================================================
*/
Maintain Indentation
Inline commentary should reinforce the indentation and therefore the logical structure of the program. For
example, it is very easy to find the comments in the make_array procedures shown below.
PACKAGE package_name
/*
|| Author:
||
|| Overview:
||
|| Major Modifications (when, who, what)
||*/
IS
...
END package_name;
PACKAGE rg_select
/*
|| Author: Diego Pafumi
||
|| Overview: Manage a list of selected items correlated with a
|| block on the screen.
||
|| Major Modifications (when, who, what)
|| 12/94 - DP - Create package
|| 3/95 - IS - Enhance to support coordinated blocks
128
||*/
IS
/*----------------- Modules to Define the List -------------------*/
END rg_select;
Tables
Table names should reflect the data in the table. The names are a mix of upper and lowercase letter with no
underscores and in singular. The names should be fully spelled out and in singular or plural, but please keep only one
rule: plural or singular.
Example: UserProfile.
Columns
Table columns should be named in the same was tables are named.
Examples: FormID, ScanDate, ZoneName.
Stored Procedures
Atlantis related stored procedures start with "ap_".
Project specific stored procedure should be prefixed by "p_".
Following the prefix should be an abbreviation for a verb, or action word. Some typical actions on data are select
(sel), insert (ins), update (upd) and delete (del). If a combination of actions is possible in a stored procedure, then
use the first letters of the action. For example, if a stored procedure can do an insert or update, then use iu. An
underscore character should then follow the verb abbreviation.
Next, the name of the table affected or some other noun should be used to describe what is affected. Each word in
this part of the spec is a mix of upper and lowercase, where the first character is uppercase and the rest lowercase.
There should be no underscores in this part of the name.
Examples are listed below:
ap_ins_Form - This procedure name is an Atlantis stored procedure that inserts a record into the Form table.
ap_iu_Document - This procedure name is Atlantis stored procedure that inserts or updates a record into the
Document table.
ap_sel_Zone - This procedure name is an Atlantis stored procedure that selects records from the Zonetable.
p_del_Account - This procedure is project specific procedure that deletes records from the Zone table.
Triggers
Atlantis triggers start with "at_" and project specific triggers start with "t_".
Following this prefix is an indicator describing the action that triggers the trigger. Use del for delete, ins for insert and
upd for update. If a trigger is used for more than one of these operations, then just include the first letter. For
example, use iu for an insert/update trigger. Next comes an underscore followed by the suffix. The suffix contains an
upper/lowercase name that includes the table name and/or any other descriptive text.
Examples: at_del_Document, t_iu_UserProfile.
129
Views
Atlantis views will have a prefix of "av_". A project specific view should have a prefix of "v_" or "view_".
Following the prefix the view name should contain some sort of descriptive reference. If the view contains a simple
join of two tables, then include the table names. For example: v_Table1Table2.
The suffix should be upper/lowercase.
Indexes
Index names should have an "in_" prefix. The rest of the name is upper and lowercase. This suffix contains some
meaningful text about the nature of the index.
Example: in_EmployeeID.
Constraints
Primary keys are to be prefixed with "pk_", unique keys with "uk_" or "unique_" and foreign keys start with "fk_".
The remainder of the name is upper and lowercase and usually contains the name of the field(s) included in the key.
Example: pk_FormID, fk_ImageType.
Sequence
Sequence names begin with an "s_", followed by an underscore and then the field name (i.e. s_Field). If field name is
ambiguous, then precede the field name with table name s_TableField.
[
[
[
[
[
[
v
v
[
[
[
[
[
[
[
[
[
[
[
[