Data hierarchy in Oracle is specific (and demented!) see figure 3.1 Normally, unit of storage is file
Managed by OS Can grow / shrink Physical sections logically connected by OS
Terminology
File: physical entity for storing data on disk Segment: collection of extents for a particular object Extent: unit of disk storage made up of contiguous blocks Blocks: sizing of smallest piece of database data decided upon when db created (Block OS)
Create a tablespace
Create tablespace FREDDY datafile path\freddy.dbf size 1000K Default storage (initial 10K next 10K Minextent 1 maxextents 100 Pctincrease 0) Permanent;
10 10 10 10 20 30 10 10 12 10 20 32 10 40 14.4 46.4 10 50 17.2 63.6 10 60 20.7 84.3 10 70 25 109.3 10 80 30 139.3 10 90 36 175.3 10 100 42.3 218.5
About tablespaces
Kind of partitioning
Easier to deal with small ones Inc. monitor perf. Oracle very strong at TS level
Optimal seems to be around 2GB Strategy = isolate aged data into one tablespace => can be made read-only Should have same size extents
Contiguous blocks
P 148
Block size
Smallest unit of oracle DB Critical perf. Factor as inefficient space use leads to increase I/O activity Block overhead
Integrity section (90 bytes) Runtime overhead (136 bytes) Approx. 256 b Transactional header (24 bytes and growing) Directory section (Pointers) And then..data
Big is beautiful? Percentage of overhead decreases when block size increases, but no right answer here
2 K, 4K or 16K?
OLTP prefer smaller small granularity increase likelihood that data is in buffer (indexed access) DW or DSS prefer larger physically close data used together + less movement In practice constraint on memory will dictate Buffer size + now split in 3 areas:
KEEP pool RECYCLE pool DEFAULT pool
Sizing extents
Try to match the max capacity of your storage device to limit I/O operations
eg: minimal read size of OS (64K?)
For single objects, It is beneficial to keep extents of same size (deletion) Try to limit the number of extents below 50 (measure of the growth of the object) If too many, rebuild object with larger size extents
RB Segments grow to be big enough to hold complete transacs (cannot span segments)
Total size after growth smalltab = 40K instead of 14K mediumtab = 206K instead of 240K
Constraints
Candidate key: Create table fred ( name varchar2(10) NOT NULL firstname varchar2(10) NOT NULL age number Constraint FRED_UQ unique (name, firstname) );
Primary Key
Create table fred ( name varchar2(10) firstname varchar2(10) age number Constraint FRED_PK Primary Key (name, firstname) ); Same apart: always NOT NULL + can only have one of them in a table If only one field just list primary key as special constraint
Check constraint
Create table fred ( ID number NOT NULL name varchar2(10) firstname varchar2(10) age number CHECK (age between 18 and 65) );
Deleting data
Drop table statement remove table Or truncate table statement empty it Warning: neither can be rolled back Otherwise: Alter table NAME Add () Alter table NAME modify () drop column fieldname See rules on handout
Finally
Users can have different logical views on a database These views are called Schemas They allow for a re-organisation of physical DB objects for each users Each schema can contain elements from different tablespaces Has limited size allocated to each user
Pctincrease should be used with care (affects reusability of reclaimed space i.e. extents) Wise use of tablespace eases admin (like partitioning)
sizing follows sizing of device e.g. 2GB Many utilities to work with tablespaces Isolate processes e.g. a report running inside a TS Also for archiving (move to read only huge perf. gains)
example
Table with 10 extents 64 K each I/O device buffer = 64K => 10 reads If extent = 640K => ???? Reads If 8 extents 80K each => ??? Reads
Reads cannot span extents => 16 reads or an increase of 60% in I/O operations
Cl: either use extents much larger than buffer or use multiple of buffer size
Data utilities
ORACLE is king of data handling Export: to transfer data between DBs
Extract both table structure and data content into dump file
Import: corresponding facility SQL*loader automatic import from a variety of file formats into DB files
Needs a control file
When spelling out queries drop HINT using syntax: /*+ HINT */ This invokes the OPTIMISER
RBO CBO
Example:
Accessing the worker field in the worker table: By default full scan (+ when no where statement) else
Select /*+ FULL(worker)*/ From worker Where lodging = Rose Hill Select /*+ ROWID(worker)*/ For low hit ratio need index or rowid value
More than one value returned => IRS Less efficient Especially if values are not in index => IRS followed by table rowid access Another reason why index creation important
Database creation
Using a script:
Connect internal as sysdba; create database NAME.;
When DB is started
a) Data block buffer cache created
page846
Very Hot redo is located in buffer on-line redo Semi-Hot redo is kept in redo log files archived redo Limit to how much can reasonably be held So ORACLE organises hot backups of the DB Redo files up to that point can then be discarded Needs to run in ARCHIVELOG mode
Backups
When DB is offline
Datafiles Control file On-line redo Init.ora
Stores a complete workable DB Does not work when DB is running Not to be trusted after abort shutdown
On-line backups
When running in ARCHIVELOG mode 3 distinct redo log files
When one full moves to next one After 3, overwrite first one
Archiver background process backs up each redo file before overwriting it to disk Automatic recovery from on-line backup
When DB started all failed transactions rerun or roll back
Recovery manager
Takes redo logs on by one and checks transactions Warning redo files cannot be skipped in the sequence of recovery Options:
Level 0: all blocks ever used Level 1: all blocks used since last full Bup Level 2: only those changed last Bup Huge impact on Bup performance
Other functions
Archiving Locking Logwriting Data writing Recovering Job queues System monitor Process monitor
Summary
Oracle Instance
PMON
SMON
SNPn
LCKn
RECO
Snnn
Dnnn
Pnnn
DBWR LGWR
Shared Pool
SGA
Database Buffer Cache
CKPT ARCH
Server Processes
User Processes
Oracle Database
Parameter File
Control Files
DataFiles