Gloc17 Tables PDF
Gloc17 Tables PDF
Gloc17 Tables PDF
Daniel A. Morgan
email: [email protected]
mobile: +1 206-669-2949
skype: damorgan11g
Thursday: 18 May, 2017 twitter: @meta7solutions
1
Introduction
2
Unsafe Harbor
This room is an unsafe harbor
You can rely on the information in this presentation to help you protect your
data, your databases, your organization, and your career
No one from Oracle has previewed this presentation
No one from Oracle knows
what I'm going to say
No one from Oracle has
supplied any of my materials
Everything we will discuss is
existing, proven, functionality
3
Daniel Morgan
Oracle ACE Director Alumni
Oracle Educator
Curriculum author and primary program instructor at University of Washington
Consultant: Harvard University
University Guest Lecturers
APAC: University of Canterbury (NZ)
EMEA: University of Oslo (Norway)
Latin America: Universidad Cenfotec, Universidad Latina de Panama, Technologico de Costa Rica
IT Professional
First computer: IBM 360/40 in 1969: Fortran IV
Oracle Database since 1988-9 and Oracle Beta tester
The Morgan behind www.morganslibrary.org
Member Oracle Data Integration Solutions Partner Advisory Council
Vice President Twin Cities Oracle Users Group (Minneapolis-St. Paul)
Co-Founder International GoldenGate Oracle Users Group System/370-145 system console
www.morganslibrary.org`
5
Forsythe (1:2)
In business 46 years
$1.2B in 2016
Partner with more
than 200 technology
OEMs
Dan Wilson
Account Manager
6
Forsythe (2:2)
In business 46 years
$1.2B in 2016
Partner with more
than 200 technology
OEMs
Dan Wilson
Account Manager
7
What Meta7 Brings To The Party
Oracle only division of Forsythe
Platinum Partner
Focuses on the entire Oracle technology stack
The entire line of Oracle infrastructure from x86
through the full stack of engineered systems and storage
Oracle Database
Design and Deployment
Stability
Security
Scalability
Data Integration (GoldenGate and ODI)
Oracle Cloud
DevOps
Infrastructure as Code
Jason Peterson, Solutions Architect and Maggie Mellon, Account Manager
Focusing on solutions to business problems ... not products
8
Stability: IT Fire Fighting
9
Oracle Stack Security
10
Scalability: VLDBs
and Partitioning
11
Database Performance
12
Zero Downtime Migration
13
Just In Time IT Procurement
14
Content Density Warning
15
Why Am I Focusing On Oracle Tables?
Because no one else is
Because Oracle University doesn't teach this material
Because there are 119 pages in the 12cR2 docs under CREATE TABLE
Because no one knows the full syntax for basic DDL statements
Because we have now spent more than 30 years talking about performance
tuning and yet the number one conference and training topic remains tuning
which proves that we need to stop focusing on edge cases and focus, instead,
on the basics
Because explain plans, AWR Reports, and trace files will never fix a problem if
you don't know the full range of options available
Because the best way to achieve high performance is to choose techniques
that reduce resource utilization
16
How Well Do You Know CREATE TABLE?
17
Legacy Architecture Tables
18
Legacy Data Dictionary: DBA_, ALL_, USER_
VIEW_NAME VIEW_NAME VIEW_NAME
------------------------------ ------------------------------ ------------------------------
DBA_ALL_TABLES DBA_SUMMARY_DETAIL_TABLES DBA_UPDATABLE_COLUMNS
DBA_APPLY_TABLE_COLUMNS DBA_SYNC_CAPTURE_PREPARED_TABS DBA_WM_VERSIONED_TABLES
DBA_BASE_TABLE_MVIEWS DBA_SYNC_CAPTURE_TABLES DBA_XML_NESTED_TABLES
DBA_CAPTURE_PREPARED_TABLES DBA_TABLES DBA_XML_OUT_OF_LINE_TABLES
DBA_CLUSTERING_TABLES DBA_TAB_COLS DBA_XML_TABLES
DBA_EVALUATION_CONTEXT_TABLES DBA_TAB_COLS_V$ DBA_XML_TAB_COLS
DBA_EXTERNAL_TABLES DBA_TAB_COLUMNS
DBA_FILE_GROUP_TABLES DBA_TAB_COL_STATISTICS
DBA_FILE_GROUP_TABLESPACES DBA_TAB_COMMENTS
DBA_FLASHBACK_ARCHIVE_TABLES DBA_TAB_HISTGRM_PENDING_STATS
DBA_HIST_DATABASE_INSTANCE DBA_TAB_HISTOGRAMS
DBA_LBAC_TABLE_POLICIES DBA_TAB_IDENTITY_COLS
DBA_MINING_MODEL_TABLES DBA_TAB_MODIFICATIONS
DBA_NESTED_TABLES DBA_TAB_PARTITIONS
DBA_NESTED_TABLE_COLS DBA_TAB_PENDING_STATS
DBA_OBJECT_TABLES DBA_TAB_PRIVS
DBA_PARTIAL_DROP_TABS DBA_TAB_STATISTICS
DBA_PART_TABLES DBA_TAB_STATS_HISTORY
DBA_PENDING_CONV_TABLES DBA_TAB_STAT_PREFS
DBA_QUEUE_TABLES DBA_TAB_SUBPARTITIONS
DBA_SECUREFILE_LOG_TABLES DBA_TSTZ_TABLES
DBA_SOURCE_TABLES DBA_TSTZ_TAB_COLS
DBA_SUBSCRIBED_TABLES DBA_UNUSED_COL_TABS
19
Heap Tables (1:2)
Almost every Oracle table ever built by everyone except Oracle Corp. is a
heap table stored in a tablespaces ... a description of "vanilla" as good as any
Heap tables are optimized for persistent storage of data when you have little
idea of what you are putting in and less of an idea of how that data, after
storage will be used
There isn't one good reason for making every table a heap table: Not one
20
Heap Tables (2:2)
SQL> CREATE TABLE state (state_abbrev VARCHAR2(2));
Table created.
DBMS_METADATA.GET_DDL('TABLE','STATE')
---------------------------------------------------------
CREATE TABLE "UWCLASS"."STATE"
( "STATE_ABBREV" VARCHAR2(2)
) SEGMENT CREATION DEFERRED
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
TABLESPACE "UWDATA"
21
External Tables (1:3)
An external tables is a physical file stored in a file system on disk
Columns can be defined as fixed length or delimited
External tables can be created by the database
DML is not allowed on external tables
SELECT statements interact with external tables precisely the same way they interact with
internal heap tables
External tables are a replacement for the legacy SQL*Loader tool
SQL> conn sys@pdbdev as sysdba
Enter password:
Connected.
Directory created.
SQL>
SQL> GRANT read, write ON DIRECTORY ext TO uwclass;
Grant succeeded.
22
External Tables (2:3)
-- create a file named
7369,KYTE,SME,20
7499,MILLSAP,SALESMAN,30
7521,NORGAARD,SALESMAN,30
7566,KOLK,MANAGER,20
7654,LEWIS,ANALYST,30
-- create a file on the hard disk named cost.txt with the follow 4 lines:
YEAR PID CPU GROSS REVENUE
2003 def 2.00 123.4567890
2004 ABC 1.00 39.7288841651344
2005 xyz 1.99 1107.5458517352
A comma delimited flat file is export from a UNIX database and transferred for loading into a Windows based Oracle database. The lines are terminated with 0x0A
and not the 0x0A 0x0D pair for DOS/Windows. The fields are randomly double quoted, and some contain commas internal to the field's data like the following:
23
External Tables (3:3)
Note the key words "ORGANIZATION EXTERNAL"
conn uwclass/uwclass@pdbdev
24
Global Temporary Tables (GTT) (1:2)
One of the most expensive things you can do in an Oracle database is "DROP
TABLE"
Global temporary tables address this issue by creating a permanent table
where the data is temporary
GTTs can have
Constraints
Indexes
Optimizer Statistics
Synonyms
Views
GTTs are built in the temporary tablespace so the same METADATA is shared
by all users while the data remains private
25
Global Temporary Tables (GTT) (2:2)
There are two types of GTTs
ON COMMIT DELETE ROWS
Data is deleted when a COMMIT is issued
Use these primarily to preserve a temporary working data set or an intermediate result set
ON COMMIT PRESERVE ROWS
Data is deleted when the session ends
Use these primarily to preserve a working data set across multiple transactions or as a result
table for a report writing/business intelligence tool
CREATE GLOBAL TEMPORARY TABLE gtt_ocdr (
zip_code VARCHAR2(5),
by_user VARCHAR2(30),
entry_date DATE)
ON COMMIT DELETE ROWS;
26
Index Organized Tables (IOT) (1:2)
IOTs are wholly underutilized by developers and DBAs
An IOT is a table stored as a B*Tree index rather than as an unorganized
heap of data
Use IOTs where the size of the primary key index is a substantial fraction, or
larger, than the size of the table
Use IOTs when some of the columns are accessed frequently and others may
be rarely or never accessed
Look up tables, the children of foreign key referential constraints are often
perfect candidates for storage as an IOT
27
Index Organized Tables (IOT) (2:2)
CREATE TABLE labor_hour (
WORK_DATE DATE,
EMPLOYEE_NO VARCHAR2(8),
CONSTRAINT pk_labor_hour
PRIMARY KEY (work_date, employee_no))
ORGANIZATION INDEX;
28
Partitioned Tables (1:7)
Tables are a logical, not a physical, construct
Partitions and Subpartition are maps
Just as a tablespace maps many datafile
Just as a regular heap table maps many blocks
Partitioned tables map segments so that they appear to be a single object
Partitions may optionally map many subpartitions for increased granularity
In a partitioned table the segment is no longer the table it is the segment
29
Partitioned Tables (2:7)
Multiple types of partitioning
HASH (a database internally generated hashing algorithm) - least valuable and overused
LIST (for example North, South, East West)
RANGE (for example 2015, 2016, 2017, 2018) - most valuable and most commonly used
Multiple types of RANGE partitioning
Basic
Interval (date or numeric)
Reference
SYSTEM
Data stored in partitions can be
Encrypted to protect sensitive information (for example credit cards)
Compressed to save on storage costs (for example older data)
Relocated to slower, less expensive, disk when it is less frequently accessed
Made READ ONLY so that it cannot be accidentally changed
Easily exported to flat files, spreadsheets, and other databases
Exported into spreadsheets with lessened system impact
30
Partitioned Tables (3:7)
System Partitioning
Partitioned tables created by the
Oracle Database and owned by
one of the database's built-in
schemas
User Partitioning
Partitioned tables created by users
or applications installed by the
user
Only available for Enterprise
Edition
31
Partitioned Tables (4:7)
Segments exist within a tablespace
Segments are a collection of extents
Extents are a collection of data blocks
Data blocks are mapped to disk blocks
32
Partitioned Tables (5:7)
33
Partitioned Tables (6:7)
Speed with Very Large Databases
Partition Pruning (Developer)
Oracle optimizes SQL statements to mark the partitions or subpartitions that need to be
accessed and eliminates (prunes) unnecessary partitions or subpartitions from access.
Partition pruning is the skipping of unnecessary index and data partitions or subpartitions
by a query
34
Partitioned Tables (7:7)
The following are examples of special use cases for table partitioning
CREATE TABLE orders OF XMLType
XMLTYPE STORE AS BINARY XML
VIRTUAL COLUMNS (site_id AS (XMLCast(XMLQuery('/Order/@SiteId' PASSING OBJECT_VALUE RETURNING CONTENT) AS NUMBER)))
PARTITION BY RANGE (site_id) (
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (20),
PARTITION pm VALUES LESS THAN (MAXVALUE));
35
XML Tables
Use the optimized storage of XML tables when persisting storage of XML
documents
36
Object-Relational Tables
37
Object/Nested Tables with a NESTED TABLE
The best advice I can provide you is don't build these ... you will sacrifice data
integrity, space, and performance
That said ... here's how you do it
CREATE OR REPLACE NONEDITIONABLE TYPE CourseList AS TABLE OF VARCHAR2(64);
/
38
Object/Nested Table Based with a VARRAY
The best advice I can provide you is don't build these ... you will sacrifice data
integrity, space, and performance
That said ... here's how you do it
CREATE OR REPLACE TYPE Project AUTHID DEFINER AS OBJECT (
project_no NUMBER(2),
title VARCHAR2(35),
cost NUMBER(7,2));
/
39
12c Container Architecture Tables
40
Common Tables (1:2)
As of Oracle Database version 12.2 tables in an Application Root container
can be optionally created with shared metadata or shared data
When metadata, the DDL, is shared into PDBs as pointers from the
Application Root data dictionary
Table created.
41
Common Tables (2:2)
As of Oracle Database version 12.2 tables in an Application Root container
can be optionally created with shared metadata or shared data
When data is shared the DDL and the table rows are shared into PDBs under
the application root
Table created.
42
New 12cR2 Container Database Architecture
Seed Dev 1
PDB$SEED PDB
43
Table Options
44
Tables in the 12.2 Data Dictionary (1:2)
By default all Oracle tables are heap tables
SQL> desc dba_tables SAMPLE_SIZE NUMBER
Name Null? Type LAST_ANALYZED DATE
--------------------------- -------- --------------- PARTITIONED VARCHAR2(3)
OWNER NOT NULL VARCHAR2(128) IOT_TYPE VARCHAR2(12)
TABLE_NAME NOT NULL VARCHAR2(128) TEMPORARY VARCHAR2(1)
TABLESPACE_NAME VARCHAR2(30) SECONDARY VARCHAR2(1)
CLUSTER_NAME VARCHAR2(128) NESTED VARCHAR2(3)
IOT_NAME VARCHAR2(128) BUFFER_POOL VARCHAR2(7)
STATUS VARCHAR2(8) FLASH_CACHE VARCHAR2(7)
PCT_FREE NUMBER CELL_FLASH_CACHE VARCHAR2(7)
PCT_USED NUMBER ROW_MOVEMENT VARCHAR2(8)
INI_TRANS NUMBER GLOBAL_STATS VARCHAR2(3)
MAX_TRANS NUMBER USER_STATS VARCHAR2(3)
INITIAL_EXTENT NUMBER DURATION VARCHAR2(15)
NEXT_EXTENT NUMBER SKIP_CORRUPT VARCHAR2(8)
MIN_EXTENTS NUMBER MONITORING VARCHAR2(3)
MAX_EXTENTS NUMBER CLUSTER_OWNER VARCHAR2(128)
PCT_INCREASE NUMBER DEPENDENCIES VARCHAR2(8)
FREELISTS NUMBER COMPRESSION VARCHAR2(8)
FREELIST_GROUPS NUMBER COMPRESS_FOR VARCHAR2(30)
LOGGING VARCHAR2(3) DROPPED VARCHAR2(3)
BACKED_UP VARCHAR2(1) READ_ONLY VARCHAR2(3)
NUM_ROWS NUMBER SEGMENT_CREATED VARCHAR2(3)
BLOCKS NUMBER RESULT_CACHE VARCHAR2(7)
EMPTY_BLOCKS NUMBER CLUSTERING VARCHAR2(3)
AVG_SPACE NUMBER ACTIVITY_TRACKING VARCHAR2(23)
CHAIN_CNT NUMBER DML_TIMESTAMP VARCHAR2(25)
AVG_ROW_LEN NUMBER HAS_IDENTITY VARCHAR2(3)
AVG_SPACE_FREELIST_BLOCKS NUMBER CONTAINER_DATA VARCHAR2(3)
NUM_FREELIST_BLOCKS NUMBER INMEMORY VARCHAR2(8)
DEGREE VARCHAR2(10) INMEMORY_PRIORITY VARCHAR2(8)
INSTANCES VARCHAR2(10) INMEMORY_DISTRIBUTE VARCHAR2(15)
CACHE VARCHAR2(5) INMEMORY_COMPRESSION VARCHAR2(17)
TABLE_LOCK VARCHAR2(8) INMEMORY_DUPLICATE VARCHAR2(13)
45
Tables in the 12.2 Data Dictionary (2:2)
By default all Oracle tables are heap tables
SQL> desc dba_tables SAMPLE_SIZE NUMBER
Name Null? Type LAST_ANALYZED DATE
--------------------------- -------- --------------- PARTITIONED VARCHAR2(3)
OWNER NOT NULL VARCHAR2(128) IOT_TYPE VARCHAR2(12)
TABLE_NAME NOT NULL VARCHAR2(128) TEMPORARY VARCHAR2(1)
TABLESPACE_NAME VARCHAR2(30) SECONDARY VARCHAR2(1)
CLUSTER_NAME VARCHAR2(128) NESTED VARCHAR2(3)
IOT_NAME VARCHAR2(128) BUFFER_POOL VARCHAR2(7)
STATUS VARCHAR2(8) FLASH_CACHE VARCHAR2(7)
PCT_FREE NUMBER CELL_FLASH_CACHE VARCHAR2(7)
PCT_USED NUMBER ROW_MOVEMENT VARCHAR2(8)
INI_TRANS NUMBER GLOBAL_STATS VARCHAR2(3)
MAX_TRANS NUMBER USER_STATS VARCHAR2(3)
INITIAL_EXTENT NUMBER DURATION VARCHAR2(15)
NEXT_EXTENT NUMBER SKIP_CORRUPT VARCHAR2(8)
MIN_EXTENTS NUMBER MONITORING VARCHAR2(3)
MAX_EXTENTS NUMBER CLUSTER_OWNER VARCHAR2(128)
PCT_INCREASE NUMBER DEPENDENCIES VARCHAR2(8)
FREELISTS NUMBER COMPRESSION VARCHAR2(8)
FREELIST_GROUPS NUMBER COMPRESS_FOR VARCHAR2(30)
LOGGING VARCHAR2(3) DROPPED VARCHAR2(3)
BACKED_UP VARCHAR2(1) READ_ONLY VARCHAR2(3)
NUM_ROWS NUMBER SEGMENT_CREATED VARCHAR2(3)
BLOCKS NUMBER RESULT_CACHE VARCHAR2(7)
EMPTY_BLOCKS NUMBER CLUSTERING VARCHAR2(3)
AVG_SPACE NUMBER ACTIVITY_TRACKING VARCHAR2(23)
CHAIN_CNT NUMBER DML_TIMESTAMP VARCHAR2(25)
AVG_ROW_LEN NUMBER HAS_IDENTITY VARCHAR2(3)
AVG_SPACE_FREELIST_BLOCKS NUMBER CONTAINER_DATA VARCHAR2(3)
NUM_FREELIST_BLOCKS NUMBER INMEMORY VARCHAR2(8)
DEGREE VARCHAR2(10) INMEMORY_PRIORITY VARCHAR2(8)
INSTANCES VARCHAR2(10) INMEMORY_DISTRIBUTE VARCHAR2(15)
CACHE VARCHAR2(5) INMEMORY_COMPRESSION VARCHAR2(17)
TABLE_LOCK VARCHAR2(8) INMEMORY_DUPLICATE VARCHAR2(13)
46
Table Storage Types (1:2)
Tablespace
A tablespace is the default table storage definition and is a logical mapping to space in one
or more physical datafiles
Tablespaces are limited to storing one segment per block
Tablespaces can be read-write or read-only
The default Oracle tablespace definition wastes 10% of most tables
Clusters
A cluster is a storage definition that can be mapped onto a tablespace that suspends the
default rules and allows for capabilities that can be used to greatly enhance performance
The Oracle data dictionary's incredible performance speed is based in large part on the
use of Clusters
SecureFiles
Like Clusters a SecureFile is a mapped portion of a tablespace that suspends the default
rules and enhances the ability to store LOBs (they have nothing to do with security)
File System
Used by external tables
47
Table Storage Types (2:2)
Here's how Oracle makes use of Clusters to enhance data dictionary
performance
SQL> SELECT cluster_name, tablespace_name TBS_NAME, SQL> SELECT table_name
cluster_type CTYPE 2 FROM dba_tables
2 FROM dba_clusters 3 WHERE cluster_name = 'C_OBJ#'
3 ORDER BY 1; 4* ORDER BY 1;
48
Cluster Types
Single Table Cluster By Hash
Multi-Table Cluster By Hash CREATE CLUSTER sc_srvr_id (
srvr_id NUMBER(10))
Multi-Table Cluster By Index SIZE 1024;
49
SecureFiles
SecureFiles LOB storage is one of two storage types used with Oracle
Database 12c the other type is BasicFiles LOB storage
The SECUREFILE LOB parameter enables advanced features, including
compression and deduplication (part of the Advanced Compression Option),
and encryption (part of the Advanced Security Option)
Starting with Oracle Database 12c, SecureFiles is the default storage
mechanism for LOBs
50
ASM
Not all ASM DiskGroups should be created equal
For diskgroups not on SSD you can get more I/Os from the outside of the
platter than the inside
ALTER DISKGROUP data
ADD TEMPLATE datafile_hot ATTRIBUTE (HOT MIRRORHOT);
51
Column Data Types (1:2)
Consider your options before choosing a data type
CREATE TABLE tstring ( CREATE TABLE tnumeric (
charcol CHAR(2000), deccol DEC(38),
charvaryingcol CHAR VARYING(4000), decimalcol DECIMAL(38),
charactercol CHARACTER(2000), doubleprecisioncol DOUBLE PRECISION,
charactervaryingcol CHARACTER VARYING(4000), floatcol FLOAT(126),
nationalcharvarying NATIONAL CHAR VARYING(2000), intcol INT,
nationalcharactervaryingcol NATIONAL CHARACTER VARYING(2000), integercol INTEGER,
ncharcol NCHAR(1000), numbercol NUMBER(38),
ncharvaryingcol NCHAR VARYING(2000), numberfcol NUMBER,
nvarchar2col NVARCHAR2(2000), numericcol NUMERIC(38),
varcharcol VARCHAR(4000), numericfcol NUMERIC,
varchar2col VARCHAR2(4000)); realcol REAL,
smallintcol SMALLINT);
SQL> desc tstring
Name Type SQL> desc tnumeric
--------------------------- -------------- Name Type
CHARCOL CHAR(2000) ------------------- --------------
CHARVARYINGCOL VARCHAR2(4000) DECCOL NUMBER(38)
CHARACTERCOL CHAR(2000) DECIMALCOL NUMBER(38)
CHARACTERVARYINGCOL VARCHAR2(4000) DOUBLEPRECISIONCOL FLOAT(126)
NATIONALCHARVARYING NVARCHAR2(2000) FLOATCOL FLOAT(126)
NATIONALCHARACTERVARYINGCOL NVARCHAR2(2000) INTCOL NUMBER(38)
NCHARCOL NCHAR(1000) INTEGERCOL NUMBER(38)
NCHARVARYINGCOL NVARCHAR2(2000) NUMBERCOL NUMBER(38)
NVARCHAR2COL NVARCHAR2(2000) NUMBERFCOL NUMBER
VARCHARCOL VARCHAR2(4000) NUMERICCOL NUMBER(38)
VARCHAR2COL VARCHAR2(4000) NUMERICFCOL NUMBER(38)
REALCOL FLOAT(63)
SMALLINTCOL NUMBER(38)
52
Column Data Types (2:2)
Consider your options before choosing a data type
CREATE TABLE ttemporal ( CREATE TABLE tmisc (
date_col DATE, rowid_col ROWID,
int_d2s_col INTERVAL DAY TO SECOND, urowid_col UROWID,
int_y2m_col INTERVAL YEAR TO MONTH, anytype_col ANYTYPE,
ts_col TIMESTAMP, anydata_col ANYDATA,
tswtz_col TIMESTAMP WITH TIME ZONE, anydataset_col ANYDATASET);
tswltz_col TIMESTAMP WITH LOCAL TIME ZONE);
SQL> desc tmisc
SQL> desc ttemporal Name Type
Name Type ------------------- --------------
------------------- -------------- ROWID_COL ROWID
DATE_COL DATE UROWID_COL ROWID
INT_D2S_COL INTERVAL DAY(2) TO SECOND(6) ANYTYPE_COL ANYTYPE
INT_Y2M_COL INTERVAL YEAR(2) TO MONTH ANYDATA_COL ANYDATA
TS_COL TIMESTAMP(6) ANYDATASET_COL ANYDATASET
TSWTZ_COL TIMESTAMP(6) WITH TIME ZONE
TSWLTZ_COL TIMESTAMP(6) WITH LOCAL TIME
ZONE
53
Column Defaults
Provides a default value during DML if the SQL
SQL> desc dba_tab_cols
Name Null? Type
----------------------- -------- --------------
statement would leave the column NULL OWNER
TABLE_NAME
NOT NULL
NOT NULL
VARCHAR2(128)
VARCHAR2(128)
54
Hidden Columns (1:2)
Hidden, aka Invisible, columns are columns that
SQL> desc dba_tab_cols
Name Null? Type
------------------------ -------- --------------
are invisible unless specifically named in a OWNER
TABLE_NAME
NOT NULL
NOT NULL
VARCHAR2(128)
VARCHAR2(128)
command or statement COLUMN_NAME
DATA_TYPE
NOT NULL VARCHAR2(128)
VARCHAR2(128)
55
Hidden Columns (2:2)
1 row created.
56
Virtual Columns
Virtual columns are columns that do not
SQL> desc dba_tab_cols
Name Null? Type
----------------------- -------- --------------
physically exist from the standpoint of providing OWNER
TABLE_NAME
NOT NULL
NOT NULL
VARCHAR2(128)
VARCHAR2(128)
persistent data storage but rather virtualize the COLUMN_NAME
DATA_TYPE
NOT NULL VARCHAR2(128)
VARCHAR2(128)
result of a function or expression DATA_TYPE_MOD
DATA_TYPE_OWNER
VARCHAR2(3)
VARCHAR2(128)
DATA_LENGTH NOT NULL NUMBER
Virtual columns can be used for constraints, DATA_PRECISION
DATA_SCALE
NUMBER
NUMBER
indexing, and partitioning NULLABLE
COLUMN_ID
VARCHAR2(1)
NUMBER
DEFAULT_LENGTH NUMBER
CREATE TABLE vcol ( DATA_DEFAULT LONG
NUM_DISTINCT NUMBER
salary NUMBER(8), LOW_VALUE RAW(2000)
bonus NUMBER(3), HIGH_VALUE RAW(2000)
total_comp NUMBER(10) AS (salary+bonus)); DENSITY NUMBER
NUM_NULLS NUMBER
desc vcol NUM_BUCKETS NUMBER
...
HIDDEN_COLUMN VARCHAR2(3)
SELECT column_id, column_name, virtual_column VIRTUAL_COLUMN VARCHAR2(3)
FROM user_tab_cols ...
WHERE table_name = 'VCOL' COLLATION VARCHAR2(100)
COLLATED_COLUMN_ID NUMBER
INSERT INTO vcol
(salary, bonus)
VALUES
(1,2);
57
Performance Tuning Tables
58
Table Tuning Rules
Choose the optimal type of table and table storage options based on the way
the table will be accessed
It is almost always better to accept a small penalty during INSERTs and
UPDATEs to gain an advantage for SELECT
When designing tables ... thinking vertically ... not horizontally ... generally, the
more columns the more issues
Order columns in the table by the probability they will be accessed in a filter or
join operation
Do not make primary key columns NOT NULL
Do not use the LONG or LONG RAW data types
59
Too Many Columns
Oracle claims that a table can contain up to 1,000 columns: It is not true. No
database can do 1,000 columns no matter what their marketing claims may be
The maximum number of real table columns is 255
Break the 255 barrier and optimizations such as advanced and hybrid
columnar compression no longer work
A 1,000 column table is actually four segments joined together behind the
scenes just as a partitioned table appears to be a single segment but isn't
Be suspicious of any table with more than 50 columns. At 100 columns it is
time to take a break and re-read the Codd-Date rules on normalization
Think vertically not horizontally
Be very suspicious of any table with column names in the form "SPARE1",
"SPARE2", "..."
The more columns a table has the more cpu is required when accessing
columns to the right (as the table is displayed in a SELECT * query ... or at the bottom if the table is
displayed by a DESCribe)
60
Column Ordering
Computers are not humans and tables are not paper forms
CBO's column retrieval cost
Oracle stores columns in variable length format
Each row is parsed in order to retrieve one or more columns
Each subsequently parsed column introduces a cost of 20 cpu cycles regardless of
whether it is of value or not
These tables will be accessed by person_id or state: No one will ever put the
address2 column into the WHERE clause as a filter ... they won't filter on
middle initial either
Common Design Optimized Design
CREATE TABLE customers ( CPU CREATE TABLE customers (
person_id NUMBER, 20 person_id NUMBER,
first_name VARCHAR2(30) NOT NULL, 40 last_name VARCHAR2(30) NOT NULL,
middle_init VARCHAR2(2), 60 state VARCHAR2(2) NOT NULL,
last_name VARCHAR2(30) NOT NULL, 80 city VARCHAR2(30) NOT NULL,
address1 VARCHAR2(30), 100 first_name VARCHAR2(30) NOT NULL,
address2 VARCHAR2(30), 120 address1 VARCHAR2(30),
city VARCHAR2(30), 140 address2 VARCHAR2(30),
state VARCHAR2(2)); 160 middle_init VARCHAR2(2));
61
Proof Column Ordering Matters in 12.1.0.2
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 214K| 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| READ_TEST | 1 | 214K| 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------
62
It Still Matters in 12.2.0.1
Run the query while performing a 10053 Level 1 Trace
ALTER SESSION SET tracefile_identifier = 'test_plan2';
ALTER SESSION SET EVENTS '10053 trace name context forever, level 1';
Column 101
Column 1
SINGLE TABLE ACCESS PATH SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for AUD$UNIFIED[AUD$UNIFIED] Single Table Cardinality Estimation for AUD$UNIFIED[AUD$UNIFIED]
SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE
How much are you willing to pay for accessing column 101?
63
Stop Making Primary Keys NOT NULL
the only value in creating a needless, and meaningless, check constraint?
SQL> CREATE TABLE t (
2 recid NUMBER);
Table created.
SQL> desc t
Name Null? Type
--------------------------- -------- ----------------
RECID NUMBER
Table altered.
SQL> desc t
Name Null? Type
--------------------------- -------- ----------------
RECID NOT NULL NUMBER
Is that you get to waste a little more CPU than everyone else
64
Compressed Tables
Table compression comes in three separate flavors
Basic ... no additional cost and including in the database license
Advanced ... requires the Advanced Compression Option license
Hybrid Columnar ... requires Exadata, ZFS, or Pillar Data Systems storage
Compression does far more than save disk space ... it can greatly enhance
performance by minimizing I/O
Compression and decompression require cpu so carefully calibrate usage on
systems that are cpu bound
65
DBMS_SPACE
This package provides segment space information not currently available
through the standard views
Use the CREATE_TABLE_COST and CREATE_INDEX_COST to determine
the sizes of tables and indexes prior to their creation
Use FREEBLOCKS and SPACE_USAGE to determine the free blocks
consumed by tables, partitions, LOBs, clusters, and indexes
Use OBJECT_SPACE_USAGE, SPACE_USAGE, and UNUSED_SPACE to
learn how efficiently space allocated to an object is used
Use VERIFY_SHRINK_CANDIDATE to determine whether an object can be
DECLARE
shrunk to drop the high water mark, recover space, and improve performance
ub NUMBER;
ab NUMBER;
dbms_space.create_table_cost(
BEGIN
tablespace_name IN VARCHAR2,
dbms_space.create_table_cost('UWDATA',28,250000,0,ub,ab);
avg_row_size IN NUMBER,
row_count IN NUMBER,
dbms_output.put_line('Used Bytes: ' || TO_CHAR(ub));
pct_free IN NUMBER,
dbms_output.put_line('Alloc Bytes: ' || TO_CHAR(ab));
used_bytes OUT NUMBER,
END;
alloc_bytes OUT NUMBER);
/
Used Bytes: 7880704
Alloc Bytes: 8126464
66
In-Memory Clause (1:2)
To greatly increase access
speed for joins and filters
consider the In-Memory option
which covers
Full Database Caching
In-Memory Aggregation
In-Memory Column Store
67
In-Memory Clause (2:2)
68
Automated Data Optimization (ADO) (1:2)
ADO is implemented with two Oracle capabilities ... heat maps utilizing the
DBMS_HEATMAP package and Integrated Lifecycle Management (ILM)
utilizing the DBMS_ILM and DBMS_ILM_ADMIN packages
ILM can be used to apply policies to table data that automatically apply
management policies
Compression
Data Tiering
In-Memory Option
CREATE TABLE rowmove_test (
testcol VARCHAR2(20))
ENABLE ROW MOVEMENT;
69
Automated Data Optimization (ADO) (2:2)
CREATE TABLESPACE tier1_ts DATAFILE '/u01/app/oracle/oradata/orcl12c/orcl/tier1.dbf' SIZE 10M AUTOEXTEND ON NEXT 5M;
CREATE TABLESPACE tier2_ts DATAFILE '/u01/app/oracle/oradata/orcl12c/orcl/tier2.dbf' SIZE 10M AUTOEXTEND ON NEXT 5M;
CREATE TABLESPACE tier3_ts DATAFILE '/u01/app/oracle/oradata/orcl12c/orcl/tier3.dbf' SIZE 50M AUTOEXTEND ON NEXT 5M;
70
Temporal Validity
Associates one or more valid time dimensions with a table and makes data
visible depending on its time-based validity, as determined by the start and
end dates or time stamps of the period for which a given record is considered
valid
71
More You Should Know About Tables
72
Enabling Row Movement
After a rowid is assigned to a row piece, the rowid can change in special
circumstances if row movement is enabled
Partition key updates
Flashback Table operations
Shrink table operations
CREATE TABLE rowmove_test (
testcol VARCHAR2(20))
ENABLE ROW MOVEMENT;
73
Row Dependencies
ORA_ROWSCN pseudocolumn
CREATE TABLE scnprec (
Returns, for each row, the conservative upper testcol VARCHAR2(20))
ROWDEPENDENCIES;
bound system change number (SCN) of the
most recent change to the row SELECT table_name, dependencies
FROM user_tables;
This is useful for determining approximately
SELECT current_scn
when a row was last updated FROM v$database;
It is not absolutely precise, because Oracle INSERT INTO scnprec VALUES ('ABC');
tracks SCNs by transaction committed for the COMMIT;
block in which the row resides INSERT INTO scnprec VALUES ('DEF');
COMMIT;
74
DBMS_COMPARISON
Use this fully documented and supported built-in package to compare data in
tables, views and materialized views
Can converge (fix) data discrepancies automatically
75
DBMS_REDEFINITION
Used to online redefine
Table columns
Table column names
Table column data types
Constraints
Indexes
BEGIN
Triggers dbms_redefinition.can_redef_table('PM', 'PRINT_ADS', dbms_redefinition.cons_use_pk);
dbms_redefinition.redef_table(
uname => 'PM',
tname => 'PRINT_ADS',
table_compression_type => 'ROW STORE COMPRESS ADVANCED',
table_part_tablespace => 'NEWTBS',
index_key_compression_type => 'COMPRESS 1',
index_tablespace => 'NEWIDXTBS',
lob_compression_type => 'COMPRESS HIGH',
lob_tablespace => 'NEWLOBTBS',
lob_store_as => 'SECUREFILE');
dbms_redefinition.sync_interim_table('PM', 'PRINT_ADS', 'INT_PRINT_ADS');
dbms_redefinition.finish_redef_table('PM', 'PRINT_ADS', 'INT_PRINT_ADS');
END;
/
76
Wrap Up
77
Conclusion
How comfortable are you with your knowledge of CREATE TABLE?
Probably feel pretty good about DROP TABLE
But how about all of the ALTER TABLE syntax?
The most important principle in creating optimal tables is the same as
everything else in Oracle is "do the least work"
Minimize CPU utilization
Minimize I/O
Take the load off the storage array
Off the HBA cards
Off the SAN switch
Minimize network utilization
Bandwidth
Round Trips
Minimize the memory footprint
Stop using 7.3.4 best practices ... and start using the best practices
for 11gR2 and above
78
*
ERROR at line 1:
ORA-00028: your session has been killed
Thank You
Daniel A. Morgan
email: [email protected]
mobile: +1 206-669-2949
skype: damorgan11g
twitter: @meta7solutions 79