03 Data
Implementation
潘得龍 Telung Pan
[email protected]
Logical and Physical Unit of Database
基本名詞
• Instance
• Tablespace
• Table
• Block
• Extent: 連續的資料 block
• Segment: table space 中一個或多個 extent
Tablespace
• 在 Oracle 資料庫中, tablespace 是一個重要的概念,用來邏輯性地組織
和管理資料的儲存空間。
• Tablespace 可以將資料檔案( data files )與資料庫物件(如 table 、
index 等)關聯起來,方便管理磁碟空間和效能。
• 其他常見的關聯式資料庫管理系統( RDBMS )如
PostgreSQL 、 MySQL 和 SQL Server 在這方面的實現有所不同。
下面各別投影片說明。
Logical Structure of Database
Tablespace 的類型
• The primary types of tablespaces in an database
are permanent, undo, and temporary.
Permanent tablesapce
• SYSTEM tablespace
User segments should never reside in the SYSTEM
tablespace
• SYSAUX tablepsace
also should not have any user segments, use by
applications.
Undo tablespace
• Multiple undo tablespaces can exist in a database,
but only one undo tablespace can be active at any
given time.
• Undo tablespaces are used for rolling back
transactions, for providing read consistency for
select statements run concurrently with DML
statements on the same table or set of tables.
• ‘Snapshot too old’ errors
Temporary
tablespace group
Temporary
• More than one temporary tablespace can be
online and active in the database.
• temporary tablespace groups
A temporary tablespace group must consist of at
least one temporary tablespace; it cannot be
empty.
Prove a single user with multiple sessions with the
ability to use a different actual temporary
tablespace for each session.
Creating a temporary
tablespace group
• CREATE TEMPORARY TABLESPACE temp_0812
TEMPFILE ‘C:\temp01.dbf’ SIZE 20M;
• DROP TABLESPACE temp_0812 INCLUDING
CONTENTS AND DATAFILES;
• 建立三個名稱分別為 temp1, temp2, temp3 的 temporary
tablespace
• ALTER TABLESPACE temp1 TABLESPACE GROUP
temp_group1;
• ALTER TABLESPACE temp2 TABLESPACE GROUP
temp_group2;
• ALTER TABLESPACE temp3 TABLESPACE GROUP
temp_group2;
• ALTER DATABASE DEFAULT TEMPORARY
TABLESPACE temp_group1;
DBA_TABLESPACE_GROU
PS data dictionary view
• SELECT group_name, tablespace_name FROM
dba_tablespace_groups;
• To drop a tablespace group, we must first drop all
its members.
ALTER TABLESPACE temp1 tablespace group ‘’;
指定 temporary tablespace 群
組給使用
• CREATE USER u1234 IDENTIFIED BY 1234
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE temp_group1;
• If we did not assign the tablespace during user
creation, the user u1234 would be assigned the
database default temporary tablespace.
DBA_USERS data
dictionary views
• SELECT username, default_tablespace,
temporary_tablespace FROM dba_users WHERE
USERNAME = ‘U1234’
Bigfile tablespace
• Consists of only one datafile
• The single datafile can be up to 128T (terabytes) in
size if the tablespace block size is 32KB. (up to 8EB,
exabytes)
• Bigfile tablespace can save space in the SGA and
the control file because fewer datafiles need to be
tracked.
• CREATE BIGFILE TABLESPACE whs01 DATAFILE ‘C:\
big_data.dbf’ SIZE 40M;
• ALTER TABLESPACE whs01 resize 20M;
PostgreSQL
• PostgreSQL supports tablespaces, and the
concept is quite similar to Oracle’s.
• In PostgreSQL, a tablespace is a logical structure
that allows users to define the physical location of
data storage (e.g., a specific directory or disk).
• You can assign a tablespace to a database, table,
or index.
Implementation
• By default, PostgreSQL stores data in the data directory
defined during installation ($PGDATA).
• You can create a new tablespace using the CREATE
TABLESPACE command and specify a storage path.
For example:
Administrators can allocate different tables or indexes to
specific tablespaces based on performance needs (e.g.,
placing frequently accessed data on an SSD).
Differences from Oracle
• PostgreSQL’s tablespace functionality is simpler,
mainly used to control file locations, and lacks the
deep integration with advanced storage and
segment management found in Oracle.
• Unlike Oracle, PostgreSQL doesn’t have a
dedicated "temporary tablespace" concept;
temporary object storage is typically managed via
parameters like temp_tablespaces.
MySQL
• MySQL’s tablespace concept depends on the
storage engine (e.g., InnoDB or MyISAM)
• Generally less explicit and flexible than Oracle’s.
Implementation of
MySQL
• InnoDB supports general tablespaces and file-per-
table tablespaces.With innodb_file_per_table
enabled (default), each table and its indexes are
stored in a separate .ibd file, which can be seen as a
simplified form of a tablespace.
• You can create a general tablespace using CREATE
TABLESPACE and assign multiple tables to it. For
example:
Differences of MySQL
• InnoDB also has a global system tablespace (e.g.,
ibdata1 file) that stores metadata and certain
internal data.
• MyISAM doesn’t support tablespaces. Each table
is stored as separate .MYD (data) and .MYI (index)
files, with no higher-level logical grouping.
Differences of MySQL
• MySQL’s tablespace functionality is less flexible
than Oracle’s, focusing more on file-level
management rather than logical storage
organization.
• It lacks dynamic resizing or deep integration with
disk performance needs, as seen in Oracle.
SQL Server
• SQL Server doesn’t have a direct equivalent to
Oracle’s tablespace, but it uses filegroups to
achieve similar functionality.
Implementation of
SQL Server
• In SQL Server, a database consists of one or more data
files and log files, which can be grouped into filegroups.
• A filegroup is a logical structure that lets you assign
tables, indexes, or other objects to specific sets of files,
controlling their physical storage locations. For example:
Differences of MySQL
• By default, all objects are stored in the PRIMARY
filegroup, but you can create custom filegroups mapped
to different disks.
• SQL Server’s filegroups are more aligned with file
management and are often used for distributing I/O or
backup/recovery strategies (e.g., splitting large
databases).
• Oracle’s tablespace emphasizes logical grouping and
management (e.g., temporary or undo tablespaces),
while SQL Server delegates such functionality elsewhere
(e.g., temporary objects are stored in the tempdb
database).
Optimal Flexible
Architecture (OFA)
• Optimal Flexible Architecture (OFA) provides
guidelines to ease the maintenance of the Oracle
software and database files as well as improve the
performance of the database by placing the
database files such that I/O bottlenecks are
minimized.
Oracle Installation
Tablespaces
• Standard Oracle Installation
Tablespaces
Segment or datafile
segregation
• Big segments and small segments should be in separate
tablespaces.
• Table segments and their corresponding index segments
should be in separate tablespaces.
• A separate tablespace should be used for each application.
• Segments with low usage and segments with high usage
should be in different tablespaces.
• Static segments - high DML segments
• Read-only tables should be in their own tablespace.
資料庫配置與儲存空間管理
資料庫空間配置的思考重點
• Datafiles 放置位置是最大影響因素
• 最大化 I/O trhoughput
• 最大化整體資料庫效能
調整 Tablespace 與 Datafile
• In an ideal database, all tablespaces and he objects within them
are created at their optimal sizes.
Resizing a Smallfile
Tablespace Using ALTER
DATABASE
• CREATE TABLESPACE temp01 DATAFILE ‘C:\test01.dbf’ SIZE 20M;
• alter database
datafile ‘C:\test01.dbf’ resize 15m;
• alter database
datafile ‘C:\test01.dbf’ resize 10m;
• alter database
datafile ‘C:\test01.dbf’ resize 1m;
• alter database
datafile ‘C:\test01.dbf’ resize 100t;
• alter database
datafile ‘C:\test01.dbf’ resize 50g;
autoextent, next,
maxsize
• To avoid manual resizing tablespaces error.
• alter database
datafile ‘C:\test01.dbf‘
autoextend on
next 20m
maxsize 1g;
Datafile Extension Clauses
If the disk volume containing the
datafile does not have the disk
space available for the expansion
of the datafile?
• Move the datafile to another disk volume
• Or create a second datafile for the tablespace on
another disk volume.
• alter tablespace test01
add datafile ‘C:\test02.dbf‘
size 15m
autoextend on
next 5m
maxsize 50m;
Resizing a Bigfile
Tablespace Using ALTER
TABLESPACE
• create bigfile tablespace dmarts
datafile ‘C:\dmarts.dbf’ size 7m
autoextend on next 10m maxsize unlimited
extent management local
segment space management auto;
//We will discuss more at next time.
• alter tablespace dmarts resize 5m;
• What if?
alter tablespace test01 resize 100m;
Why?
Moving Datafiles
• To better manage the size of a datafile or improve
the overall I/O performance of the database, it may
be necessary to move one or more datafiles in a
tablespace to a different location.
• Methods:
• alter database
• alter tablespace
• tools
Moving Datafiles with
ALTER DATABASE
1.Connect to the database as SYSDBA and shut down the
instance.
2.Use operating system commands to move the datafile(s).
3.Open the database in MOUNT mode.
4.Use alter database to change the references to the
datafile in the database.
5.Open the database in OPEN mode.
6.Perform an incremental or full backup of the database
that includes the control file.
Example
• sqlplus / as sysdba
• select d.name from v$datafile d join
v$tablespace t using(ts#)
where t.name = ‘TEST01’;
• shutdown immediate;
• DOS> move C:\test01.dbf C:\AAA
• startup mount
• alter database rename file ‘C:\test01.dbf’ to ‘C:\AAA\test01.dbf’;
• alter database open;
• alter database backup controlfile to trace;
Moving Datafiles with
ALTER TABLESPACE
• If the datafile you want to move is part of a
tablespace other than SYSTEM, SYSAUX, the
active undo tablespace, or the temporary
tablespace.
• The database, except for the tablespace whose
datafile will be moved, remains available to all
users during the entire operation.
Example
• alter tablespace test01 offline;
• DOS > move C:\test01.dbf C:\AAA
• alter tablespace test01 rename datafile
‘C:\test01.dbf’ to ‘C:\AAA\test01.dbf’;
• alter tablespace test01 online;
Moving Online Redo Log
•
FIles
select group#, member from v$logfile
order by group#, member;
• shutdown immediate;
• move [1-2].log to new location
• startup mount
• alter database rename file ‘C:\redo01.log’ to ‘C:\AAA\
redo01.log’;
• alter database rename file ‘C:\redo02.log’ to ‘C:\AAA\
redo02.log’;
• alter database open;
Moving Control Files
• select name, value from v$spparameter
where name = ‘control_files’;
• show parameter control_files;
• alter system set control_files =
‘C:\AAA\CONTROL.DBF’;
• shutdown immediate
• move....
• startup