Beruflich Dokumente
Kultur Dokumente
juliandyke.com
Agenda
Introduction Forgotten Features Tracing and Auditing Testing and Benchmarking Administration Execution Plans Tables Indexes SQL PL/SQL Conclusion
juliandyke.com
Easy to understand Easy to implement Works in first release Documented in first release Compatible with other features Improves Productivity or Manageability Saves resources / money Intellectually stimulating
juliandyke.com
juliandyke.com
TRACEFILE_IDENTIFIER
juliandyke.com
DBMS_SYSTEM.KSDWRT
For example
BEGIN DBMS_SYSTEM.KSDWRT (1, Output to trace file); DBMS_SYSTEM.KSDWRT (2, Output to alert log); DBMS_SYSTEM.KSDWRT (3, Output to both); END; /
juliandyke.com
BITAND
Initially undocumented built-in function Performs a bit-wise AND between two operators
SELECT BITAND (42,1) FROM dual;
Beware of overflows
juliandyke.com
Introduced in Oracle 8.1.5 Convert decimal numbers to and from hexadecimal To convert from decimal to hex
SELECT TO_CHAR (1048576,XXXXXXXX) FROM dual;
returns 100000
returns 1048576
juliandyke.com
System Triggers
CREATE OR REPLACE TRIGGER us01_logoff BEFORE LOGOFF ON us01.SCHEMA BEGIN dbms_session.set_sql_trace (FALSE); END;
juliandyke.com
DBMS_MONITOR
Waits (event 10046 level 8) are enabled by default Binds (event 10046 level 4) are disabled by default
10
juliandyke.com
DBMS_MONITOR
Can be enabled at database level in Oracle 10.2 To enable trace for all database sessions use
DBMS_MONITOR.DATABASE_TRACE_ENABLE ( WAITS BOOLEAN, -- Include Waits BINDS BOOLEAN -- Include Binds INSTANCE_NAME VARCHAR2 -- Instance Name );
11
juliandyke.com
Autonomous Transactions
Introduced in Oracle 8.1.5 Recursive transaction started by main transaction Can commit or rollback independently of main transaction Committed data unaffected if main transaction rolls back Often used for auditing
CREATE OR REPLACE TRIGGER trigger1 BEFORE INSERT ON table1 FOR EACH ROW DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO log1 VALUES (:new.col1,:new.col2,SYSDATE); COMMIT; END; /
12
juliandyke.com
SYS Auditing
In Oracle 9.2 and above, operations performed by the SYS user can be audited.
13
juliandyke.com
14
Fixed Date
Initialization Parameter Useful for deterministic testing In Oracle 8.0 and above can be set dynamically using ALTER SYSTEM To set date only use
FIXED_DATE = DD-MON-YY
15
juliandyke.com
Random Functions
16
juliandyke.com
To force a checkpoint
ALTER SYSTEM CHECKPOINT;
Useful when dumping log files To force a log file switch and archive the log file
ALTER SYSTEM ARCHIVE LOG CURRENT;
Useful for testing archive log creation with Physical standby database Logical standby database
17
juliandyke.com
Introduced in Oracle 8.0 Flushes all unpinned objects from library cache
ALTER SYSTEM FLUSH SHARED_POOL;
Useful for deterministic testing but After issuing this statement All unpinned cursors need reparsing All unpinned packages need recompilation
18
juliandyke.com
Introduced in Oracle 10.1 Flushes all unpinned buffers from the buffer cache
ALTER SYSTEM FLUSH BUFFER_CACHE;
In Oracle 9.0.1 and above the following command has the same effect
ALTER SESSION SET EVENTS IMMEDIATE TRACE NAME FLUSH_CACHE;
Useful for deterministic testing but After issuing this statement Warm up the cache before testing workloads
19
juliandyke.com
Performance Tuning
20
juliandyke.com
V$SQL_PLAN
Introduced in Oracle 9.0.1 Shows actual execution plan in memory Enhanced in Oracle 9.2 to include Access Predicates (Joins) Filter Predicates Related views include V$SQL_PLAN_WORKAREA V$SQL_PLAN_STATISTICS V$SQL_PLAN_STATISTICS_ALL
21
juliandyke.com
V$SQL_PLAN
ADDRESS HASH_VALUE CHILD_NUMBER OPERATION OPTIONS OBJECT_NODE OBJECT# OBJECT_OWNER OBJECT_NAME OPTIMIZER ID PARENT_ID DEPTH POSITION COST CARDINALITY RAW(4) NUMBER NUMBER VARCHAR2(30) VARCHAR2(30) VARCHAR2(10) NUMBER VARCHAR2(30) VARCHAR2(64) VARCHAR2(20) NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER
BYTES OTHER_TAG PARTITION_START PARTITION_STOP PARTITION_ID OTHER DISTRIBUTION CPU_COST IO_COST TEMP_SPACE ACCESS_PREDICATES FILTER_PREDICATES PROJECTION TIME QBLOCK_NAME REMARKS
NUMBER VARCHAR(35) VARCHAR2(5) VARCHAR2(5) NUMBER VARCHAR2(4000) VARCHAR2(20) NUMBER NUMBER NUMBER VARCHAR2(4000) VARCHAR2(4000) VARCHAR2(4000) NUMBER VARCHAR2(31) VARCHAR2(4000)
22
juliandyke.com
Use the values in these views when determining why execution plans differ
23
juliandyke.com
Optimizer Environment Variable values reported by the dynamic performance views include:
active_instance_count bitmap_merge_area_size parallel_dml_mode parallel_execution_enabled
cpu_count
cursor_sharing db_file_multiblock_read_count
parallel_query_mode
parallel_threads_per_cpu pga_aggregate_target
hash_area_size
optimizer_dynamic_sampling optimizer_features_enable
query_rewrite_enabled
query_rewrite_integrity skip_unusable_indexes
optimizer_index_caching
optimizer_index_cost_adj optimizer_mode optimizer_mode_hinted parallel_ddl_mode
24
sort_area_retained_size
sort_area_size star_transformation_enabled statistics_level workarea_size_policy
juliandyke.com
DBMS_XPLAN
DISPLAY function parameters include TABLE_NAME name of plan table STATEMENT_ID statement ID in plan table FORMAT as below
Value
BASIC TYPICAL ALL SERIAL
Description
Operation ID, object name and operation/option only (Default) Most relevant information including partition pruning, parallelism and predicates where appropriate As TYPICAL plus parallel execution server statements As TYPICAL without parallel execution server statements
25
juliandyke.com
DBMS_XPLAN
26
juliandyke.com
DBMS_XPLAN
*2
3
T1
T2
10
1000
60
53000
58
7
27
juliandyke.com
DBMS_XPLAN
EXPLAIN PLAN FOR SELECT /*+ ORDERED PARALLEL (t1 2) USE_MERGE (t1 t2) */ t1.c2, t2.c2 FROM t1, t2 WHERE t1.c1 = t2.c1 AND t1.c2 = 10;
28
juliandyke.com
DBMS_XPLAN
Id Operation
0 SELECT STATEMENT
1 2 *3 MERGE JOIN SORT JOIN TABLE ACCESS FULL T1 T2
10
10 10 10
590
590 60 60
79
79 78,02 29 78,01 7 78,00 P->S QC(RANDOM) P->P HASH S->P HASH 33 78,02 PCWP
*4
5
SORT JOIN
TABLE ACCESS FULL
1000 53000
1000 53000
47 78,02 PCWP
29
juliandyke.com
DBMS_XPLAN
Partition pruning information can also be included e.g. for a range partitioned table
CREATE TABLE t1 (c1 NUMBER,c2 NUMBER,c3 CHAR(50)) PARTITION BY RANGE (c1) ( PARTITION p1 VALUES LESS THAN (100), PARTITION p2 VALUES LESS THAN (200), PARTITION p3 VALUES LESS THAN (300), PARTITION p4 VALUES LESS THAN (400) ); EXPLAIN PLAN FOR SELECT c2 FROM t1 WHERE c1 >= 150 AND c1 < 250; SELECT * FROM TABLE (dbms_xplan.display);
30
juliandyke.com
DBMS_XPLAN
PStart PStop
2 2 2
3 3
31
juliandyke.com
DBMS_XPLAN
In Oracle 10.1 and above New DISPLAY_CURSOR function By default displays plan for last statement executed in session
Name
(%CPU) (100)
Time
T1
51809 160
(2)
00:00:02
32
juliandyke.com
V$SESSION_WAIT_HISTORY
Externalises last 10 wait events for each session Similar information to V$SESSION_WAIT but much more user friendly
33
juliandyke.com
Administration
34
juliandyke.com
Kill Session
For example to kill a session for user US01 Identify the SID and serial number
SELECT sid, serial# FROM v$session WHERE username = US01;
For example
35
juliandyke.com
To rename a table:
RENAME oldname TO newname;
To rename an index
ALTER INDEX oldname RENAME TO newname;
36
juliandyke.com
Dropping Columns
Introduced in 8.1.5 Columns can be dropped from a table using the ALTER TABLE statement Columns can be marked unused immediately and deleted at a later time deleted immediately If the delete operation fails at any point it can be restarted from the point of failure
37
juliandyke.com
Dropping Columns
Columns can also be marked unused immediately and subsequently dropped To mark a column unused use
ALTER TABLE table1 SET UNUSED COLUMN column2;
38
juliandyke.com
Dropping Columns
If instance is shutdown while column is being dropped, drop column statement can be continued when instance restarted
ALTER TABLE table1 DROP COLUMNS CONTINUE;
DBA_PARTIAL_DROP_TABS describes partially dropped columns DBA_UNUSED_COL_TABS describes columns marked unused, but not yet dropped
39
juliandyke.com
Default Tablespaces
40
juliandyke.com
Resumable Statements
Oracle 9.0.1 and above long running operations encountering out of space errors can be resumed Resumable operations include Queries DML Statements SQL*Loader operations Import operations DDL statements Out of space errors include Tablespace full Maximum number of extents reached for object Tablespace quota exceeded for a user
41
juliandyke.com
Resumable Statements
When resumable space allocation is enabled Operation suspends if an out of space error occurs Details of the error are written to DBA_RESUMABLE DBA can optionally be alerted DBA can either fix the error abort the operation Operation automatically resumes execution when error is fixed If a further error is detected, operation will suspend again
42
juliandyke.com
Resumable Statements
1 Resumable space allocation is enabled
ALTER SESSION ENABLE RESUMABLE NAME Batch Update TIMEOUT 3600;
juliandyke.com
Resumable Statements
5 (Optional) DBA is notified by message generated by AFTER SUSPEND trigger 6 DBA queries DBA_RESUMABLE view for details of suspended operation 7 DBA fixes error condition e.g.
ALTER TABLESPACE TS99 ADD DATAFILE <filename> SIZE <size>;
44
juliandyke.com
Resumable Statements
In Oracle 10.1 and above resumable statements can be enabled at instance level
ALTER SYSTEM SET resumable_timeout = <timeout>;
45
juliandyke.com
In Oracle 9.0.1 and above, DROP TABLESPACE has been extended to optionally delete its datafiles
DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;
46
juliandyke.com
In Oracle 10.1 and above to enable/disable backup mode for all tablespaces in single statement use:
ALTER DATABASE BEGIN BACKUP; ALTER DATABASE END BACKUP;
Useful with three-way mirror or snapshot backups In Oracle 9.2 to disable backup mode for all tablespaces following a database restart use:
ALTER DATABASE END BACKUP;
47
juliandyke.com
Tables
48
juliandyke.com
Row Movement
In Oracle 8.0 updating the partition key columns in a range partitioned table fails if it would cause the row to be migrated to another partition
ORA-14402: updating partition key column would cause a partition change
In Oracle 8.1.5 and above row movement can be enabled a row may be migrated to another partition if its partition key columns are updated By default row movement is disabled
49
juliandyke.com
Row Movement
Row movement can be enabled when the partitioned table is created e.g.
CREATE TABLE table1 ( column1 NUMBER, column2 NUMBER ) PARTITION BY . ENABLE ROW MOVEMENT;
Row movement can be also be enabled and disabled from an existing table e.g.
ALTER TABLE table1 ENABLE ROW MOVEMENT; ALTER TABLE table1 DISABLE ROW MOVEMENT;
50
juliandyke.com
External Tables
External tables read-only tables definition is stored in data dictionary data is stored outside the database in operating system flat files can be queried using SQL can be queried in parallel can be included in DML statement subqueries No DML operations are allowed on external table No indexes can be created on external table
51
juliandyke.com
External Tables
Example
CREATE DIRECTORY ext_dir AS '/v01/external'; CREATE TABLE transactions ( account NUMBER, value NUMBER ) ORGANIZATION EXTERNAL ( TYPE ORACLE_LOADER DEFAULT DIRECTORY ext_dir ACCESS PARAMETERS ( RECORDS DELIMITED BY NEWLINE FIELDS TERMINATED BY , ) LOCATION (transactions.csv) );
52
juliandyke.com
External Tables
The following tests were based on a 1,000,000 row flat file. Each row contained 74 bytes of data and 4 bytes of separators Tests were performed in NOARCHIVING and ARCHIVING modes
Description Time in seconds
NoArchiving Use SQL*Loader with CONVENTIONAL path load to INSERT rows 318 Archiving 322
28
20 15
285
290 283
53
juliandyke.com
Introduced in Oracle 9.2 Data is compressed when it is written to a block decompressed when it is read from the block Compression is applied at block level Blocks will only be compressed if data is sufficiently large to fill the block rows have low enough cardinality Columns can be reordered within each block to achieve optimal compression ratios A segment may contain compressed and uncompressed blocks
54
juliandyke.com
Compression can also be specified for range and list partitioned tables materialized views nested tables
55
juliandyke.com
Only works with direct path inserts For example CREATE TABLE AS SELECT INSERT /*+ APPEND */ ALTER TABLE MOVE Materialized View Refresh SQL*Loader Online Reorganization Does not work with INSERT /*+ NOAPPEND */ UPDATE DELETE
56
juliandyke.com
4 1 4 1 1 3 3 4
0 2 0 2 2 2 2
2002 2001 2000 1999 1998 1996 1997 Olivier Panis 1995 1994 1993
Row Data
3 2
Symbol Table
1
0
2 David Coulthard
57
juliandyke.com
Compression ratios vary with number of rows number of columns cardinality of rows For example loading SALES table from sales history demo schema
$ORACLE_HOME/demo/schema/sales_history
4096
8192 16384
58
8983
4398 2179
6106
2850 1353
68.0
64.7 62.0
juliandyke.com
Test1 Loading the SALES table from a flat file into an empty table
Compressed No Yes Blocks 4398 2850 Elapsed TIme (Secs) 31.77 71.08 CPU Time (Secs) 4.13 43.86
Test2 Selecting one column from all rows in the compressed table
SELECT SUM (quantity_sold) FROM sales;
Compressed No Yes Blocks 4398 2850 Elapsed TIme (Secs) 3.41 3.78 CPU Time (Secs) 2.77 3.53
59
juliandyke.com
Indexes
60
juliandyke.com
Introduced in Oracle 8.1.5 Compresses leading columns in index Must be specified when index is created or rebuilt
CREATE INDEX i1 ON t1 (c1, c2, c3) COMPRESS 2; ALTER INDEX i1 REBUILD ONLINE COMPRESS 1;
Benefits Reduces number of blocks to hold index Reduction in physical I/O for scans Improved cache efficiency Potential reduction in index height CBO more likely to use index
61
juliandyke.com
1100
900 Rome 1000 Milan 1100 Italy 1200 Munich 1300 Frankfurt
Suffix Row
Suffix Row
Prefix Row
Suffix Row Suffix Row Suffix Row Prefix Row Suffix Row Prefix Row
Germany
Germany Germany Italy Italy
Berlin
Frankfurt Munich Milan Rome
juliandyke.com
Useful INDEX_STATS columns include PRE_ROWS Number of prefix rows PRE_ROWS_LEN Sum of lengths of all prefix rows OPT_CMPR_COUNT Optimal key compression length OPT_CMPR_PCTSAVE Space saved by implementing optimal key length
63
juliandyke.com
64
juliandyke.com
Introduced in 8.1.5 create or rebuild index whilst allowing concurrent DML operations Works with B*tree indexes (non-partitioned and partitioned) 8.1.5 IOTs (non-partitioned and partitioned) 8.1.5 Reverse key indexes 9.0.1 Function-based indexes 9.0.1 Compressed indexes 9.0.1 IOT Secondary indexes 9.0.1
65
juliandyke.com
Can
be executed in parallel
Three
phases prepare phase index created data dictionary updated build phase index populated changes written to journal table merge phase rows in journal table are merged
66
juliandyke.com
67
juliandyke.com
Index Monitoring
In Oracle 9.0.1 indexes can be monitored to determine if they are being used
If index monitoring is enabled for an index, then Oracle updates a table in the data dictionary when that index is included in an execution plan by the parser
Indexes are only monitored at parse time Only SELECT statements and subqueries monitored Also works for bitmap indexes
68
juliandyke.com
Index Monitoring
69
juliandyke.com
Index Monitoring
70
juliandyke.com
Specify the NOSORT option to avoid sorting index columns when creating an index
CREATE INDEX index1 ON table1 (column1) NOSORT
If columns are not in sorted order then index creation will fail with the following error:
ORA-01409: NOSORT option may not be used; rows are not in ascending order
71
juliandyke.com
Statistics will be computed (not estimated) If the index is composite, statistics only generated for leading column If the index is non-partitioned, table, column and index statistics are gathered If the index is partitioned only index statistics are gathered
72
juliandyke.com
In Oracle 9.0.1 and above UPDATE GLOBAL INDEXES can be specified for DDL statements on partitioned tables Global indexes remain available during the operation Updates to the global index are logged UPDATE GLOBAL INDEXES can be used with the following partition DDL statements
ADD DROP MOVE TRUNCATE SPLIT MERGE EXCHANGE COALESCE
For example
ALTER TABLE table1 DROP PARTITION table1_p1 UPDATE GLOBAL INDEXES;
73
juliandyke.com
74
juliandyke.com
Introduced in Oracle 8.1.5 Defined in data dictionary definition available to all sessions Data only visible to current session retained for duration of current transaction or session stored in users sort space can overflow to sort segment in users temporary tablespace
Indexes can be created against global temporary tables same scope and duration
75
juliandyke.com
Can define triggers and views Views cannot join permanent and temporary tables Benefits Reduction in DDL Reduction in amount of redo generated DML statements against global temporary tables Generate undo Generate redo for undo Do not generate redo for block changes
76
juliandyke.com
ON COMMIT DELETE ROWS rows are only visible to the current transaction
CREATE GLOBAL TEMPORARY TABLE temp1 (column1 NUMBER) ON COMMIT DELETE ROWS;
77
juliandyke.com
78
juliandyke.com
Introduced in Oracle 10.1 To specify that unexpired undo should be preserved in all undo segments even if this makes current operations requiring undo space fail use
ALTER TABLESPACE tablespace_name RETENTION GUARANTEE;
Only applies to undo tablespaces Useful while flashback queries are running To specify default behaviour use
ALTER TABLESPACE tablespace_name RETENTION NOGUARANTEE;
79
juliandyke.com
Enabling NOLOGGING
UNRECOVERABLE clause introduced in Oracle 7.3 NOLOGGING clause introduced in Oracle 8.0 Enabled at object level
ALTER TABLE table NOLOGGING ;
Can be used by SQL*Loader direct loads CREATE TABLE direct loads CREATE INDEX direct loads INSERT /*+ APPEND */ INSERT LOB NOCACHE
CREATE INDEX index1 ON table1(column1) NOLOGGING;
80
juliandyke.com
Disabling NOLOGGING
If NOLOGGING option is specified Limited redo is written Changes cannot be recovered Changes cannot be transported to standby database In Oracle 9.2 and above NOLOGGING can be disabled At database level
ALTER DATABASE FORCE LOGGING ; ALTER DATABASE NO FORCE LOGGING;
At tablespace level
ALTER TABLESPACE tablespace_name FORCE LOGGING ; ALTER TABLESPACE tablespace_name NO FORCE LOGGING;
81
juliandyke.com
PL/SQL
82
juliandyke.com
Prior to Oracle 8.1.5 PL/SQL DDL statements were executed using DBMS_SQL package
DECLARE l_cursor INTEGER; l_result INTEGER; BEGIN l_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE (l_cursor, ALTER SYSTEM SWITCH LOGFILE,DBMS_SQL.V7); l_result := DBMS_SQL.EXECUTE (l_cursor); DBMS_SQL.CLOSE_CURSOR (l_cursor); END;
83
juliandyke.com
In Oracle 8.1.5 and above EXECUTE IMMEDIATE can execute DDL statements
EXECUTE IMMEDIATE 'CREATE TABLE tab1 (c1 NUMBER)'; EXECUTE IMMEDIATE 'ALTER TABLE tab1 ADD (c2 NUMBER)'; EXECUTE IMMEDIATE DROP TABLE tab1;
84
juliandyke.com
EXECUTE IMMEDIATE can also be used for SELECT statements INTO clause is used to define fetch variables For example
DECLARE l_c1 NUMBER; l_str VARCHAR2(1000); BEGIN l_str := 'SELECT c0l1 FROM tab1 WHERE c2 = 20'; EXECUTE IMMEDIATE l_str INTO l_c1; DBMS_OUTPUT.PUT_LINE ('Result is || l_c1); END;
85
juliandyke.com
EXECUTE IMMEDIATE can also use bind variables USING clause is used to specify bind variable positions For example
DECLARE l_c1 NUMBER; l_str VARCHAR2(1000); BEGIN l_str := 'SELECT c1 FROM t1 WHERE c2 = :p1';
EXECUTE IMMEDIATE l_str INTO l_c0l1 USING 10; DBMS_OUTPUT.PUT_LINE ('Result is '||l_c1); EXECUTE IMMEDIATE l_str INTO l_c0l1 USING 30; DBMS_OUTPUT.PUT_LINE ('Result is '||l_c1); END;
86
juliandyke.com
Bulk Collect
Introduced in Oracle 8.1.5 Returns result set in single operation Can be used with SELECT INTO FETCH INTO RETURNING INTO
87
juliandyke.com
juliandyke.com
89
juliandyke.com
FORALL
Introduced in Oracle 8.1.5 Sends INSERT, UPDATE or DELETE statements in batches Can only repeat single DML statement Works with PL/SQL collections including TABLE, VARRAY, NESTED TABLE etc. Much faster than equivalent for-loop
Limited functionality
90
juliandyke.com
FORALL Example
DECLARE TYPE NUMTYPE IS TABLE OF NUMBER(6) INDEX BY BINARY_INTEGER; TYPE NAMETYPE IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER; l_c1 NUMTYPE; l_c2 NAMETYPE; l_c3 NUMTYPE; BEGIN FOR i IN 1..100000 LOOP l_c1(i) := i; l_c2(i) := LPAD (TO_CHAR (i),30,0); l_c3(i) := MOD (i, 100); END LOOP;
FOR i IN 1..100000 LOOP -- FOR Loop 28 seconds INSERT INTO t1 VALUES (l_c1 (i), l_c2 (i), l_c3(i)); END LOOP;
FORALL f IN 1..100000 LOOP -- FORALL Loop 4 seconds INSERT INTO t1 VALUES (l_c1 (i), l_c2 (i), l_c3(i)); END;
91
juliandyke.com
FORALL Performance
Times in seconds
92
juliandyke.com
Conclusion
Every release contains many new features and enhancements that are not highlighted in the marketing material
Oracle assumes all customers will move on to new versions when they are released
93
juliandyke.com
94
juliandyke.com