Oracle SQL tuning involves optimizing SQL statements for better performance. Key aspects of SQL tuning include identifying SQL statements with high resource consumption or response times using tools like ADDM, AWR, and V$SQL. Statements can then be tuned by gathering accurate optimizer statistics, adjusting the execution plan using hints, rewriting the SQL, or changing indexes and tables. Tuning is done at both the design and execution stages.
The document discusses Oracle database architecture including the relationship between Oracle software, operating system resources like CPUs, memory and disks, Oracle processes like background processes and server processes, and database structures like the system global area (SGA), program global area (PGA), control files, redo logs and data files. It also covers Oracle memory management, instance startup/shutdown, and basic database administration tasks.
The document discusses Oracle database backup and recovery concepts. It covers write-ahead logging, how data modifications are written to the redo log and then to datafiles, and how backup and recovery works by applying redo logs to restore datafiles to a previous consistent state. It also explains different log record types, archive log mode, and how media recovery uses redo logs and backups to recover from data failures.
The document discusses Oracle database performance tuning. It covers reactive and proactive performance tuning, the top-down tuning methodology, common types of performance issues, and metrics for measuring performance such as response time and throughput. It also compares online transaction processing (OLTP) systems and data warehouses (DW), and describes different architectures for integrating OLTP and DW systems.
Oracle ASM Training covers database and storage concepts, ASM architecture, installation and configuration of ASMLIB, creating and managing ASM disk groups, best practices for ASM configuration, and migrating databases to ASM. The document includes an agenda with topics on ASM, ASMLIB, disk groups, files, redundancy, rebalancing, and troubleshooting. It also lists office locations for consulting services.
Troubleshooting Tips and Tricks for Database 19c - EMEA Tour Oct 2019Sandesh Rao
This session will focus on 19 troubleshooting tips and tricks for DBA's covering tools from the Oracle Autonomous Health Framework (AHF) like Trace file Analyzer (TFA) to collect , organize and analyze log data , Exachk and orachk to perform mass best practices analysis and automation , Cluster Health Advisor to debug node evictions and calibrate the framework , OSWatcher and its analysis engine , oratop for pinpointing performance issues and many others to make one feel like a rockstar DBA
Automatic Storage Management allows Oracle databases to use disk storage that is managed as an integrated cluster file system. It provides functions like striping, mirroring, and rebalancing of data across storage disks. The document outlines new features in Oracle Exadata and Automatic Storage Management including Flex ASM, which eliminates the requirement for an ASM instance on every server, and Flex Disk Groups, which provide file groups and enable quota management and redundancy changes for databases. It also discusses enhancements to disk offline and online operations and rebalancing.
This document discusses Oracle Management Cloud, which provides application performance monitoring, infrastructure monitoring, log analytics, and IT analytics services. It describes the services included in standard and enterprise editions, and provides pricing examples based on metrics like entities per hour and gigabytes per hour.
Oracle architecture with details-yogiji creationsYogiji Creations
Oracle is a database management system with a multi-tiered architecture. It consists of a database on disk that contains tables, indexes and other objects. An Oracle instance contains a memory area called the System Global Area that services requests from client applications. Background processes facilitate communication between the memory structures and database files on disk. Logical database structures like tablespaces, segments, extents and blocks help organize and manage the physical storage of data.
This document provides definitions and explanations of various Oracle database concepts and components. It defines terms like log switch, online redo log, archived redo log, database startup process, instance recovery, full backup restrictions, mounting modes, ARCHIVELOG mode advantages, database shutdown process, restricted instance startup, partial backup, mirrored redo log, and control file usage. It also answers questions on topics like views, tablespaces, schemas, segments, clusters, integrity constraints, indexes, extents, synonyms, and transactions.
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
Oracle Week 2017 slides.
Agenda:
Basics: How and What To Tune?
Using the Automatic Workload Repository (AWR)
Using AWR-Based Tools: ASH, ADDM
Real-Time Database Operation Monitoring (12c)
Identifying Problem SQL Statements
Using SQL Performance Analyzer
Tuning Memory (SGA and PGA)
Parallel Execution and Compression
Oracle Database 12c Performance New Features
Session aims at introducing less familiar audience to the Oracle database statistics concept, why statistics are necessary and how the Oracle Cost-Based Optimizer uses them
1) The document discusses Oracle database auditing features before and after version 12.1. It describes migrating the audit trail to the unified audit trail and using the SYS.UNIFIED_AUDIT_TRAIL table.
2) It provides steps to configure syslog auditing on Linux for Oracle database audit records. Procedures are created to output messages to syslog and call it from a fine-grained auditing policy handler.
3) An example fine-grained auditing policy is created to audit access to the SECDEMO.CUSTOMER table and call the syslog handler for non-application users.
This document discusses techniques for optimizing SQL performance in Oracle databases. It covers topics like optimizing the optimizer itself through configuration changes and statistics collection, detecting poorly performing SQL, and methods for improving plans such as indexing, partitioning, hints and baselines. The goal is to maximize the optimizer's accuracy and ability to handle edge cases, while also knowing how to intervene when needed to capture fugitive SQL and ensure acceptable performance.
Oracle RAC 12c Practical Performance Management and Tuning as presented during Oracle Open World 2013 with Michael Zoll.
This is part three of the Oracle RAC 12c "reindeer series" used for OOW13 Oracle RAC-related presentations.
This part concludes the main part of the "reindeer series" except for one bonus track "Oracle Multitenant meets Oracle RAC 12c" (available via SlidesShare, too).
This document provides interview questions for an experienced 3+ year Oracle DBA divided into 5 sections with increasing difficulty. The sections cover basic Oracle knowledge, moderate daily tasks, advanced topics, RAC-specific questions, and very specialized questions. Example questions address topics like Oracle documentation resources, database startup/shutdown modes, data dictionary views, and RAC components. Separate documents further describe common DBA tasks and default Oracle passwords.
Oracle Databaseの既存バージョンの10gや11gOracle Zero Data Loss Recovery Applianceの登場で、ますます重要な機能となってきたOracle Recovery Managerについて、OTN人気連載シリーズ「しばちょう先生の試して納得!DBAへの道」の執筆者が語ります。RMANバックアップの運用例から、高速増分バックアップの内部動作とチューニング方法まで、出し惜しみなく解説します。
Oracle SQL tuning involves optimizing SQL statements for better performance. Key aspects of SQL tuning include identifying SQL statements with high resource consumption or response times using tools like ADDM, AWR, and V$SQL. Statements can then be tuned by gathering accurate optimizer statistics, adjusting the execution plan using hints, rewriting the SQL, or changing indexes and tables. Tuning is done at both the design and execution stages.
The document discusses Oracle database architecture including the relationship between Oracle software, operating system resources like CPUs, memory and disks, Oracle processes like background processes and server processes, and database structures like the system global area (SGA), program global area (PGA), control files, redo logs and data files. It also covers Oracle memory management, instance startup/shutdown, and basic database administration tasks.
The document discusses Oracle database backup and recovery concepts. It covers write-ahead logging, how data modifications are written to the redo log and then to datafiles, and how backup and recovery works by applying redo logs to restore datafiles to a previous consistent state. It also explains different log record types, archive log mode, and how media recovery uses redo logs and backups to recover from data failures.
The document discusses Oracle database performance tuning. It covers reactive and proactive performance tuning, the top-down tuning methodology, common types of performance issues, and metrics for measuring performance such as response time and throughput. It also compares online transaction processing (OLTP) systems and data warehouses (DW), and describes different architectures for integrating OLTP and DW systems.
Oracle ASM Training covers database and storage concepts, ASM architecture, installation and configuration of ASMLIB, creating and managing ASM disk groups, best practices for ASM configuration, and migrating databases to ASM. The document includes an agenda with topics on ASM, ASMLIB, disk groups, files, redundancy, rebalancing, and troubleshooting. It also lists office locations for consulting services.
Troubleshooting Tips and Tricks for Database 19c - EMEA Tour Oct 2019Sandesh Rao
This session will focus on 19 troubleshooting tips and tricks for DBA's covering tools from the Oracle Autonomous Health Framework (AHF) like Trace file Analyzer (TFA) to collect , organize and analyze log data , Exachk and orachk to perform mass best practices analysis and automation , Cluster Health Advisor to debug node evictions and calibrate the framework , OSWatcher and its analysis engine , oratop for pinpointing performance issues and many others to make one feel like a rockstar DBA
Automatic Storage Management allows Oracle databases to use disk storage that is managed as an integrated cluster file system. It provides functions like striping, mirroring, and rebalancing of data across storage disks. The document outlines new features in Oracle Exadata and Automatic Storage Management including Flex ASM, which eliminates the requirement for an ASM instance on every server, and Flex Disk Groups, which provide file groups and enable quota management and redundancy changes for databases. It also discusses enhancements to disk offline and online operations and rebalancing.
This document discusses Oracle Management Cloud, which provides application performance monitoring, infrastructure monitoring, log analytics, and IT analytics services. It describes the services included in standard and enterprise editions, and provides pricing examples based on metrics like entities per hour and gigabytes per hour.
Oracle architecture with details-yogiji creationsYogiji Creations
Oracle is a database management system with a multi-tiered architecture. It consists of a database on disk that contains tables, indexes and other objects. An Oracle instance contains a memory area called the System Global Area that services requests from client applications. Background processes facilitate communication between the memory structures and database files on disk. Logical database structures like tablespaces, segments, extents and blocks help organize and manage the physical storage of data.
This document provides definitions and explanations of various Oracle database concepts and components. It defines terms like log switch, online redo log, archived redo log, database startup process, instance recovery, full backup restrictions, mounting modes, ARCHIVELOG mode advantages, database shutdown process, restricted instance startup, partial backup, mirrored redo log, and control file usage. It also answers questions on topics like views, tablespaces, schemas, segments, clusters, integrity constraints, indexes, extents, synonyms, and transactions.
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
Oracle Week 2017 slides.
Agenda:
Basics: How and What To Tune?
Using the Automatic Workload Repository (AWR)
Using AWR-Based Tools: ASH, ADDM
Real-Time Database Operation Monitoring (12c)
Identifying Problem SQL Statements
Using SQL Performance Analyzer
Tuning Memory (SGA and PGA)
Parallel Execution and Compression
Oracle Database 12c Performance New Features
Session aims at introducing less familiar audience to the Oracle database statistics concept, why statistics are necessary and how the Oracle Cost-Based Optimizer uses them
1) The document discusses Oracle database auditing features before and after version 12.1. It describes migrating the audit trail to the unified audit trail and using the SYS.UNIFIED_AUDIT_TRAIL table.
2) It provides steps to configure syslog auditing on Linux for Oracle database audit records. Procedures are created to output messages to syslog and call it from a fine-grained auditing policy handler.
3) An example fine-grained auditing policy is created to audit access to the SECDEMO.CUSTOMER table and call the syslog handler for non-application users.
This document discusses techniques for optimizing SQL performance in Oracle databases. It covers topics like optimizing the optimizer itself through configuration changes and statistics collection, detecting poorly performing SQL, and methods for improving plans such as indexing, partitioning, hints and baselines. The goal is to maximize the optimizer's accuracy and ability to handle edge cases, while also knowing how to intervene when needed to capture fugitive SQL and ensure acceptable performance.
Oracle RAC 12c Practical Performance Management and Tuning as presented during Oracle Open World 2013 with Michael Zoll.
This is part three of the Oracle RAC 12c "reindeer series" used for OOW13 Oracle RAC-related presentations.
This part concludes the main part of the "reindeer series" except for one bonus track "Oracle Multitenant meets Oracle RAC 12c" (available via SlidesShare, too).
This document provides interview questions for an experienced 3+ year Oracle DBA divided into 5 sections with increasing difficulty. The sections cover basic Oracle knowledge, moderate daily tasks, advanced topics, RAC-specific questions, and very specialized questions. Example questions address topics like Oracle documentation resources, database startup/shutdown modes, data dictionary views, and RAC components. Separate documents further describe common DBA tasks and default Oracle passwords.
Oracle Databaseの既存バージョンの10gや11gOracle Zero Data Loss Recovery Applianceの登場で、ますます重要な機能となってきたOracle Recovery Managerについて、OTN人気連載シリーズ「しばちょう先生の試して納得!DBAへの道」の執筆者が語ります。RMANバックアップの運用例から、高速増分バックアップの内部動作とチューニング方法まで、出し惜しみなく解説します。
1. AWSOME Day whiteboard discusses AWS authorized training center 恆逸教育訓練中心 and its certified instructors.
2. It shows diagrams of basic EC2 architecture including instances, ephemeral storage, EBS volumes, and hosting across availability zones.
3. It also illustrates setting up a VPC with private and public subnets and attaching an internet gateway.
This document provides an overview of MySQL including its architecture, clients, connection layer, SQL layer, storage engines, and installation methods. Key points include:
- MySQL uses a connection thread pool, query cache, parser, optimizer, and various storage engines like InnoDB and MyISAM.
- Common MySQL clients allow executing queries, administering the server, checking tables, backing up data, and more.
- The connection layer handles authentication and the communication protocol.
- The SQL layer performs parsing, optimization, and determining the optimal execution plan.
- Storage engines like InnoDB and MyISAM support different features and have different performance characteristics for storage, indexing, and more.
The document discusses how an in-memory column store manages cached data and transaction journals. It explains that the IMCO prioritizes cached data refresh based on priority levels like critical, high, and medium. For SELECT statements, it is best to choose cached data when it is not stale, while UPDATE statements check if cached data has become stale before deciding whether to use the cache or transaction journal. The SMCO checks cached data against a staleness threshold every 2 minutes to determine if the cache needs repopulating from the transaction journal.
This document compares the key differences between Oracle and SQL Server databases. It discusses differences in database structure, terminology, stored procedure languages, and transaction control. It provides comparisons of table types, constraints, indexes, views, data types, functions and more between the two database systems.
This document provides an overview of Hadoop and the Hadoop ecosystem. It discusses key Hadoop concepts like HDFS, MapReduce, YARN and data locality. It also summarizes SQL on Hadoop using tools like Hive, Impala and Spark SQL. The document concludes with examples of using Sqoop and Flume to move data between relational databases and Hadoop.
The document discusses B-tree indexes in Oracle Database, including their structure as balanced trees, how index entries are stored for B-tree and bitmap indexes, and how the indexes are maintained during data manipulation language (DML) operations like inserts, deletes, and updates on the associated table. It also covers topics like why an optimizer may not use an index, index constraints, reorganizing indexes, and myths about index performance tuning.
3. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
不過如果想要查詢 TAB$或 COL$的記錄,必須先知道此表格的物件編號(OBJECT ID),這物件編號必須
從 OBJ$中才能查知。然後由 TAB$中可得知表格的相關資料,COL$中可得知欄位相關的資料,如果想
要得知這個表格放在哪個表格空間中,必須要再由 TS$中得知。
SQL> SELECT obj# FROM sys.obj$ WHERE name='TESTTAB';
OBJ#
--------
11970
SQL> SELECT obj#,ts#,cols FROM tab$ WHERE obj#=11970;
OBJ# TS# COLS
--------- ------ --------
11970 4 3
SQL> SELECT name FROM ts$ WHERE ts#=4;
NAME
----------
USERS
SQL> SELECT name,type# FROM col$ WHERE obj#=11970;
NAME TYPE#
--------- ----------
COL1 2 ß NUMBER or FLOAT
COL2 1 ß VARCHAR2 or NVARCHAR2
COL3 12 ß DATE
依照上面的程式範例來看,只不過想看一些基本的資料,就要查詢好多的資料辭典,並且要了解資料辭典
表格之間的關聯,與一些數字與其所代表的意義。所以在建立資料庫後,資料庫管理者可以執行
CATALOG.SQL 指令稿,用來建立資料辭典視圖,讓使用者可以藉由簡單的視圖存取資料辭典的資料。
例如只要查詢 DBA_TABLES 與 DBA_TAB_COLUMNS 即可取代上面的查詢指令。
SQL> SELECT table_name,tablespace_name FROM dba_tables WHERE table_name='TESTTAB';
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TESTTAB USERS
SQL> SELECT column_name,data_type FROM dba_tab_columns WHERE table_name='TESTTAB';
COLUMN_NAME DATA_TYPE
------------------------------ ----------------------------------------
COL1 NUMBER
COL2 VARCHAR2
COL3 DATE
不過為了方便使用者使用這些資料辭典視圖,Oracle 提供一個視圖 DICTIONARY,其中包含所有資料辭
典視圖的名字與註解。只要使用者知道視圖的名字或相關資訊就可以透過 DICTIONARY 查出相關視圖的
資訊。
SQL> DESCRIBE dictionary
Name Null? Type
----------------------------------------------------- ---------- ----------------------------------------
TABLE_NAME VARCHAR2(30)
COMMENTS VARCHAR2(4000)
SQL> COL table_name FORMAT a30
SQL> COL comments FORMAT a60 wrap
SQL> SELECT table_name,comments FROM dictionary WHERE table_name LIKE '%INDEX%';
4. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
TABLE_NAME COMMENTS
------------------------------------------------------ ------------------------------------------------------------
ALL_INDEXES Descriptions of indexes on tables accessible to the user
ALL_INDEXTYPES All indextypes available to the user
ALL_INDEXTYPE_ARRAYTYPES All array types specified by the indextype
ALL_INDEXTYPE_COMMENTS Comments for user-defined indextypes
……………………………………………..以下省略
同時 Oracle 也將這些視圖分成三種簡單的分類:USER_XXX、ALL_XXX 與 DBA_XXX,分別表示不同
範圍的內容。XXX 可能為任何長度的字串。
• USER_XXX 表示為目前此會談的使用者所擁有的相關物件,例如 USER_TABLES 表示此使用者
所擁有的所有表格所組成的資料辭典視圖。
• ALL_XXX 表示為此會談的使用者所能存取的相關物件,包含使用者自己擁有的物件以及他人授
權可以存取的物件。因此 ALL_XXX 與 USER_XXX 相比之下,多了一個欄位 OWNER,用來區
分物件的擁有者。例如 ALL_TABLES 表示此使用者所能存取的所有表格。
• DBA_XXX 表示為整個資料庫的所有相關物件,所以必須擁有 DBA 角色的人才能存取。這些視圖
也有一個欄位 OWNER,用來區分物件的擁有者。例如 DBA_TABLES 表示此資料庫的所有的
表格。
SQL> SHOW USER
USER is "SYS"
SQL> SELECT COUNT(*) FROM user_tables;
COUNT(*)
-------------
837
SQL> SELECT COUNT(*) FROM all_tables;
COUNT(*)
-------------
1010
SQL> SELECT COUNT(*) FROM dba_tables;
COUNT(*)
-------------
1010
SQL> CONNECT testuser/oracle
SQL> SHOW USER
USER is "TESTUSER"
SQL> SELECT COUNT(*) FROM user_tables;
COUNT(*)
-------------
1
SQL> SELECT COUNT(*) FROM all_tables;
COUNT(*)
-------------
24
SQL> SELECT COUNT(*) FROM dba_tables;
SELECT COUNT(*) FROM dba_tables ß 因為TESTUSER的權限不足
*
ERROR at line 1:
ORA-00942: table or view does not exist
5. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
6.1.2 SYSAUX 表格空間
SYSAUX 表格空間是從 Oracle10g 開始出現的必要存在表格空間,這個表格空間從名字來
看’SYSAUX=SYStem AUXiliary’就可以知道是用來輔助系統表格空間而產生的表格空間。其內容是
存放那些雖然不是資料辭典,但也是 Oracle 系統所需要的表格,如 AWR(Automatic Workload
Repository)、XML 資料庫、JVM、Oracle TEXT 等功能所需要的表格。這些表格分別屬於不同的綱要
(SCHEMA),提供不同的 Oracle 功能所使用。在 Oracle10g 之前只有 SYSTEM 表格空間是必要的,其
餘的表格空間都是可以選擇不建立。
由於這個表格空間是必要存在的,所以這個表格空間不能被刪除、更名、傳遞或唯讀,但是可以被離線。
當此表格空間被離線時,相關的 Oracle 功能就不能被使用,但是不影響整個資料庫的運行。
同時在 Oracle10g 之前,現在被放在 SYSAUX 表格空間的綱要物件,其實是分別放在不同的表格空間中,
現在只是化繁為簡地將所有的相關綱要物件統一的存放在 SYSAUX 表格空間中,以減輕資料庫管理者的
管理成本。
Option or Feature Schema Tablespace in 8, 8i or 9i
---------------------------- ------------------ ---------------------------------
OLAP CWMLITE CWMLITE
Text CTXSYS DRSYS
Ultra Search WKSYS DRSYS
Intermedia and Spatial ORDSYS SYSTEM
Workspace Manager WMSYS
Data Mining DMSYS ODM
EM Repository SYSMAN OEM_REPOSITORY
Streams SYS SYSTEM
LogMiner SYSTEM SYSTEM
Logical Standby SYSTEM SYSTEM
Statspack PERFSTAT 管理者指定
Job Scheduler SYS SYSTEM
如果資料庫管理者想要回復為 Oracle9i 的方式,必須自己將相關的綱要物件搬移到其他的表格空間中。
不過不是每個綱要物件都可以搬移到其他的表格空間,如 AUTO_TASK、EM_MONITORING_USER 等
等,最簡單的方法,就是由 V$SYSAUX_OCCUPANTS 中的 MOVE_PROCEDURE,如果沒有任何搬移
方式,則表示此綱要物件就不能搬移到其他的表格空間或是要資料庫管理者自己想辦法。
SQL> SELECT occupant_name,schema_name,NVL(move_procedure,’不能搬移’) MOVE_PROCEDURE
2 FROM v$sysaux_occupants
3 ORDER BY occupant_name;
OCCUPANT_NAME SCHEMA_NAME MOVE_PROCEDURE
------------------------------------------ ----------------------- ----------------------------------------
AO SYS DBMS_AW.MOVE_AWMETA
AUTO_TASK SYS 不能搬移
EM SYSMAN emd_maintenance.move_em_tblspc
EM_MONITORING_USER DBSNMP 不能搬移
EXPRESSION_FILTER EXFSYS 不能搬移
JOB_SCHEDULER SYS 不能搬移
LOGMNR SYSTEM SYS.DBMS_LOGMNR_D.SET_TABLESPACE
LOGSTDBY SYSTEM SYS.DBMS_LOGSTDBY.SET_TABLESPACE
ORDIM ORDSYS 不能搬移
ORDIM/PLUGINS ORDPLUGINS 不能搬移
ORDIM/SQLMM SI_INFORMT 不能搬移
7. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
6.1.3 預設的暫時型態表格空間(Default Temporary Tablespace)
雖然資料庫中只有 SYSTEM 與 SYSAUX 兩個表格空間為必要存在的表格空間。但是每個資料庫必須有
一個預設的暫存表格空間存在,如果當 SYSTEM 表格空間的空間管理方式為說明管理(DICTIONARY
MANAGEMENT)時,資料庫管理者可以將 SYSTEM 表格空間當作預設的暫時表格空間,不過這樣的組
態方式對資料庫的管理與效能有著不良的影響。可是若當 SYSTEM 表格空間的空間管理方式為本機管理
(LOCALLY MANAGEMENT)時,表格空間的類型為永久或還原的表格空間,不能當作資料庫預設的暫
時表格空間,所以必須建立一個暫時型態的表格空間,當作資料庫的預設暫時表格空間。
每個資料庫中的使用者帳號,都有一個設定值-暫存表格空間(TEMPORARY TABLESPACE),當做此使
用者進行磁碟排序時所使用的暫存表格空間。不過如果當建立新的使用者帳號時,沒有設定暫時表格空間
的參數,Oracle 伺服器會使用 Oracle 資料庫的預設暫時表格空間設定值當作此使用者帳號的設定值。
暫時表格空間是用來放置暫時區段(TEMPORARY SEGMENT)。當使用者執行 SQL 指令時,有時需要進
行一些排序操作,如使用到 GROUP BY、ORDER BY、GROUPING FUNCTION(AVG、MIN、MAX
etc..)、CREATE INDEX 等。這些排序操作預設是在 SERVER PROCESS 的 PGA 空間中進行,這種稱
作 MEMORY SORT(記憶體排序)。若所需要排序的資料過大,無法在 PGA 完成排序,則會將需要排序的
資料暫時寫到暫時表格空間中,然後配合 PGA 進行排序,這種排序方式稱作 DISK SORT(磁碟排序),這
種排序方式的效能比較慢,因為需排序的資料比較多外,磁碟機的存取速度比記憶體慢也是主因。
所以如果可以結果可以不要排序,就盡量不要加上會造成排序的指令。如果一定要排序,則盡量使用記憶
體排序。若發生真的不可避免發生磁碟排序,則盡量讓整體的磁碟排序成本下降,就是正確設定暫存表格
空間。
SQL> SELECT username,temporary_tablespace FROM dba_users;
USERNAME TEMPORARY_TABLESPACE
------------------------------ ------------------------------
OUTLN TEMP
SYS TEMP
SYSTEM TEMP
DBSNMP TEMP
TSMSYS TEMP
DIP TEMP
ORACLE_OCM TEMP
………
SQL> SELECT property_name,property_value FROM database_properties
2 WHERE property_name='DEFAULT_TEMP_TABLESPACE';
PROPERTY_NAME PROPERTY_VALUE
------------------------------------------------------ ----------------------------------------
DEFAULT_TEMP_TABLESPACE TEMP ß 資料庫目前的預設值
SQL> CREATE USER testuser IDENTIFIED BY oracle; ß 新增一個測試使用者
User created.
SQL> SELECT username,temporary_tablespace FROM dba_users;
USERNAME TEMPORARY_TABLESPACE
------------------------------ ------------------------------
OUTLN TEMP
SYS TEMP
8. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SYSTEM TEMP
TESTUSER TEMP ß 剛新增的使用者所使用的暫時表格空間
DBSNMP TEMP
TSMSYS TEMP
DIP TEMP
ORACLE_OCM TEMP
………
如果有必要,事後也可以更改使用者的暫時表格空間,不過只能選擇暫時型態(TEMPORARY)的表格空間,
不能使用其他型態(PERMANENT、UNDO)的表格空間。
SQL> CREATE TMEPORARY TABLESPACE tempts TEMPFILE ‘/u02/oradata/ora11g/tempts01.dbf’
SIZE 100M; ß新增一個暫時表格空間
SQL> SELECT tablespace_name,contents FROM dba_tablespaces;
TABLESPACE_NAME CONTENTS
------------------------------ -------------------
SYSTEM PERMANENT
SYSAUX PERMANENT
UNDOTBS UNDO
TEMP TEMPORARY
USERS PERMANENT
TEMPTS TEMPORARY
SQL> ALTER USER testuser TEMPORARY TABLESPACE users;
ALTER USER testuser TEMPORARY TABLESPACE users;
*
ERROR at line 1:
ORA-12911: permanent tablespace cannot be temporary tablespace
SQL> ALTER USER testuser TEMPORARY TABLESPACE undotbs;
ALTER USER testuser TEMPORARY TABLESPACE undotbs
*
ERROR at line 1:
ORA-30034: Undo tablespace cannot be specified as temporary tablespace
SQL> ALTER USER testuser TEMPORARY TABLESPACE tempts;
User altered.
SQL> SELECT username,temporary_tablespace FROM dba_users WHERE username='TESTUSER';
USERNAME TEMPORARY_TABLESPACE
------------------------------ --------------------------------------
TESTUSER TEMPTS
同時資料庫管理者也可以變更資料庫的預設暫時表格空間,不過即使有一個以上的暫時表格空間,也只能
選擇一個為資料庫的預設值。這時候便可以利用暫時表格空間群組,將多個暫時表格空間組合在一起。如
此再將此暫時表格空間群組指派給使用者或當作資料庫預設值,這樣就可以在多人同時操作的環境中,將
磁碟排序的 I/O 操作分散在多個暫時表格空間中,如此可以減少 I/O 競爭的情況發生。
SQL> SELECT property_name,property_value FROM database_properties
9. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
2 WHERE property_name='DEFAULT_TEMP_TABLESPACE';
PROPERTY_NAME PROPERTY_VALUE
------------------------------------------------ --------------------------------------------------
DEFAULT_TEMP_TABLESPACE TEMP
SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE tempts;
SQL> SELECT property_name,property_value FROM database_properties
2 WHERE property_name='DEFAULT_TEMP_TABLESPACE';
PROPERTY_NAME PROPERTY_VALUE
------------------------------------------------ --------------------------------------------------
DEFAULT_TEMP_TABLESPACE TEMPTS
將temp加入tempgroup中,如果目前沒有tempgroup存在,則為自動建立tempgroup。
SQL> alter tablespace temp tablespace group tempgroup;
將temp由tempgroup中移除,若tempgroup沒有任何暫時表格空間,則tempgroup會自動被刪除。
SQL > ALTER TABLESPACE temp TABLESACE GROUP ‘’;
將tempts也加入tempgroup中。
SQL> ALTER TABLESPACE tempts TABLESPACE GROUP TEMPGROUP;
將tempgroup設為系統預設暫時表格空間
SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE tempgroup;
SQL> SELECT property_name,property_value FROM database_properties
2 WHERE property_name='DEFAULT_TEMP_TABLESPACE';
PROPERTY_NAME PROPERTY_VALUE
------------------------------------------------ --------------------------------------------------
DEFAULT_TEMP_TABLESPACE TEMPGROUP
10. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
6.1.4 自動管理的還原表格空間(Auto Managed UNDO Tablespace)
當資料庫管理者希望將還原區段(UNDO Segment)交由 Oracle 伺服器管理時,資料庫管理者必須先建立
一個還原型態的表格空間,然後在 Oracle 執行處理的參數檔設定:UNDO_MANAGEMENT=AUTO 及
UNDO_TABLESPACE=還原表格空間的名字即可,之後 Oracle 執行處理則會自己決定該建立多少個還
原區段,每個還原區段的大小等問題。資料庫管理者只需要決定還原表格空間的大小即可,而適合的還原
表格空間大小,可以由自動還原管理頁面得知。
還原區段的還原資訊可以用來進行交易退回(TRANSACTION ROLLBACK)、交易復原(TRANSACTION
RECOVERY)與讀取一致性(READ CONSISTENT)。這些還原資訊來自於進行資料異動操作(DML)前,儲
存在還原區段的還原資訊。
SQL> SELECT tablespace_name,contents FROM dba_tablespaces;
TABLESPACE_NAME CONTENTS
------------------------------ -------------------
SYSTEM PERMANENT
SYSAUX PERMANENT
UNDOTBS UNDO ß UNDO型態的表格空間,才能被當作自動管理的還原表格空間
TEMP TEMPORARY
USERS PERMANENT
TEMPTS TEMPORARY
………….
SQL> SHOW PARAMETER undo_management
NAME TYPE VALUE
------------------------------------------ ----------- ------------
undo_management string AUTO ßOracle Server自動管理還原區段
SQL> SHOW PARAMETER undo_tablespace
NAME TYPE VALUE
------------------------------------------ ----------- -------------
undo_tablespace string undotbs ßOracle Server將在此表格空間中建立還原區段
11. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> SELECT owner,segment_name,tablespace_name FROM dba_rollback_segs;
OWNER SEGMENT_NAME TABLESPACE_NAME
------------------ ----------------------------------------------- ------------------------------
SYS SYSTEM SYSTEM
PUBLIC _SYSSMU10_1207193410$ UNDOTBS ßOracle Server建立的還原區段
PUBLIC _SYSSMU9_1207193410$ UNDOTBS
PUBLIC _SYSSMU8_1207193410$ UNDOTBS
PUBLIC _SYSSMU7_1207193410$ UNDOTBS
PUBLIC _SYSSMU6_1207193410$ UNDOTBS
PUBLIC _SYSSMU5_1207193410$ UNDOTBS
PUBLIC _SYSSMU4_1207193410$ UNDOTBS
PUBLIC _SYSSMU3_1207193410$ UNDOTBS
PUBLIC _SYSSMU2_1207193410$ UNDOTBS
PUBLIC _SYSSMU1_1207193410$ UNDOTBS
還原擴充區塊的保留管理
還原區段中的擴充區塊依其的狀態可以分為下列幾種:
• 正在使用的擴充區塊(Active Extent):這些擴充區塊上,至少還有一個交易未完成。所以這些擴
充區塊並不能被重新使用。
• 未過期的擴充區塊(Unexpired Extent):這些擴充區塊上的交易都結束了,但是從最後一個交易的
結束時間到現在,並未超過 UNDO_RETENTION 所設定的期限,所以這種擴充區塊稱作未過期。
未過期的擴充區塊在某些情況下,可以被重新使用。
• 過期的擴充區塊(Expired Extent):這些擴充區塊上的交易都結束了,而且從最後一個交易的結束
時間到現在,已超過 UNDO_RETENTION 所設定的期限,所以這種擴充區塊可以立刻被重新使用。
這些還原資訊當整個擴充區塊(Extent)上的交易都結束後,其所佔有的還原區段空間便可以被新的交易使
用。如果資料庫管理者希望將那些已經結束交易的還原資料繼續,可以藉由設定參數
UNDO_RETENTION,讓還原資料保留幾秒鐘。不過還原擴充區塊保留的管理方式有一些特殊的設定(自
動管理與人為管理):
自動管理
從 Oracle10g 開始,Oracle 資料庫建議資料庫管理者可以藉由設定 UNDO_RETENTION 為零
(UNDO_RETENTION=0)或根本不要設定此參數,讓 Oracle 資料庫自行管理還原擴充區塊的管理。還原
擴充區塊的保留時間由 Oracle 資料庫依據現在的還原表格空間的大小與資料庫的交易狀況,自行調整
UNDO_RETENTION 的值。
SQL> SHOW PARAMETER undo_retention
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_retention integer 0
12. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
人為管理
當 UNDO_RETENTION 的參數值不是零,理論上未過期的還原擴充區塊不會被重新使用。可是如果沒有
設定保證還原保留(RETENTION GUARANTEE),即是是未過期的還原擴充區塊也可能會提前被重新使用。
SQL> SHOW PARAMETER undo_retention
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_retention integer 900
SQL> SELECT tablespace_name,retention FROM dba_tablespaces WHERE contents=’UNDO’;
TABLESPACE_NAME RETENTION
------------------------------------ -----------------------
UNDOTBS NOGUARANTEE
--只有還原型態的表格空間可以設定為絕對保留(RETENTION GURANTEE)
SQL> ALTER TABLESPACE undotbs RETENTION GUARANTEE;
Tablespace altered.
SQL> SELECT tablespace_name,retention FROM dba_tablespaces WHERE contents=’UNDO’;
TABLESPACE_NAME RETENTION
------------------------------------ -------------------
UNDOTBS GUARANTEE
SQL> SELECT tablespace_name,autoextensible FROM dba_data_files;
TABLESPACE_NAME AUT
------------------------------------ ---------
SYSTEM YES
SYSAUX YES
UNDOTBS NO
USERS NO
TESTTS NO
如果當還原表格空間並未設定自動增長的功能,實際的還原保留時間是由 Oracle 資料庫依據還原表格空
間的大小與 Oracle 資料庫的狀況決定,除非資料庫管理者要求絕對保留(GUARANTEE)。以下為進行中
的交易使用還原的擴充區塊的規則:
當還原表格空間不會自動增長與未設定絕對保留時
1. 使用過期的擴充區塊。
2. 使用未過期的擴充區塊。
如果以上兩種擴充區塊都用完了,但交易卻還沒有結束,這時該交易便會出現錯誤訊息”ORA-01650
unable to extend rollback segment string by string in tablespace string”,表示目前使用中的還原表
格空間的空間不夠,這個交易將會自動被退回(Rollback)。
當還原表格空間不會自動增長與已設定絕對保留時
• 僅能使用過期的擴充區塊,未過期的擴充區塊絕對不會被重新使用。如果所有過期的擴充區塊都
用完了,進行中的交易便會出現錯誤訊息”ORA-01650 unable to extend rollback segment ...
13. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
by ... in tablespace ...”,表示目前使用中的還原表格空間的空間不夠,這個交易將會自動被退
回(Rollback)。
當還原表格空間會自動增長與未設定絕對保留時
1. 使用過期的擴充區塊。
2. 使用未過期的擴充區塊。
3. 當以上兩種擴充區塊都用完了,但交易卻還沒有結束。此時 Oracle Server 會使用資料檔自動增長
功能,增加還原表格空間的大小。然後在將可用的擴充區塊配置給目前使用中的還原區段,讓進
行中的交易可以繼續運行。
當還原表格空間會自動增長與已設定絕對保留時
1. 使用過期的擴充區塊。
2. 因為有設定絕對保留為未過期擴充區塊,所以當過期的擴充區塊都用完了,但交易卻還沒有結束。
此時 Oracle Server 會使用資料檔自動增長功能,增加還原表格空間的大小。然後在將可用的擴充
區塊配置給目前使用中的還原區段,讓進行中的交易可以繼續運行。
14. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
6.2 建立額外的表格空間
一個資料庫只要有 SYSTEM 與 SYSAUX 表格空間就可以成功開啟。但是為了下列幾種原因,建議建立
一些額外的表格空間。
• 更靈活地管理資料庫:因為每個表格空間都可以設定為讀寫、唯讀、離線狀態,讓整個資料庫可
以部份表格空間與其他的表格空間不同,增加資料庫管理者的管理彈性。
• 區隔不同的型態的區段物件:資料辭典、還原區段、暫時區段、表格、索引、叢集等綱要物件,
應該依照不同的型態與使用特色,分別放在不同的表格空間存放。這樣讓資料庫的架構,更具管
理性與效能更好。
• 更細微地控制使用者對資料庫的空間使用:在 Oracle 資料庫中,每個使用者都依照所設定的空間
配額來使用資料庫空間。而空間配額是以使用者與表格空間為依據,所以越多的表格空間,資料
庫管理者有更細微的單位(表格空間)來設定相關的配額。
6.2.1 擴充區塊的管理方式(Extent Management)
因為 Oracle 資料庫中的空間配置單位是擴充區塊(Extent),而每個擴充區塊不是位於可用的狀態(Free
Extent),就是使用中的狀態(Used Extent)。也就是說每次當區段(Segment)有空間需求的情況發生時(新
增一個區段或現有區段的擴充區塊都被使用完了),Oracle 資料庫每次至少配置一個可用的擴充區塊給區
段。而表格空間的空間管理方式,可以分為兩種:說明管理(Dictionary Management)、本機管理
(Locally Management),用來表示這個表格空間中那些擴充區塊是可用的(Free Extent)或使用中的
(Used Extent)。
SQL> SELECT tablespace_name,extent_management FROM dba_tablespaces;
TABLESPACE_NAME EXTENT_MAN
------------------------------ --------------------
SYSTEM DICTIONARY
SYSAUX LOCAL
UNDOTBS LOCAL
TEMP LOCAL
USERS LOCAL
15. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
說明管理(Dictionary Management):
說明管理是 Oracle8i 之前唯一的一種擴充區塊管理的方法,這種方法是利用資料辭典中的兩個表格 FET$
與 UET$,記錄每個表格空間中可用的擴充區塊(FET$)與使用中的擴充區塊(UET$)。FET$可使用
DBA_FREE_SPACE,而 UET$可以使用 DBA_EXTENTS 觀察其內容。因此當每次建立一個新的區段或
現有區段有增加空間的需求時,Oracle Server 會進行以下的步驟,尋找可用的擴充區塊。
1.Oracle 資料庫會先搜索 FET$,檢查此表格空間中是否有合適的可用擴充區塊可用。
2.如果找到合適可用的擴充區塊,Oracle 資料庫會進行刪除(DELETE)(找到一模一樣大小的可用擴充區
塊,第一優先)或異動(UPDATE)(如果找不到相同大小的可用擴充區塊,但是找到比所需要大小更大的擴
充區塊)的操作,將 FET$中相關的記錄進行修改。之後再將相關擴充區塊的記錄新增(INSERT)到 UET$。
這種 SQL 操作被稱作遞迴的 SQL(Recursive SQL),這些 SQL 指令是為了執行使用者的 SQL 指令,
Oracle 資料庫額外所衍生出的 SQL 指令。
3.如果找不到合適可用的擴充區塊,Oracle 資料庫會要求此表格空間的資料檔增加大小,以增加表格空
間的可用空間,這時資料檔必須事先設定好自動成長(AUTOEXTEND)的功能。但是如果此時資料檔沒有
事先設定自動成長的功能,則會出現 ORA-01653: unable to extend table .. in tablespace。
因為這兩個表格位在 SYSTEM 表格空間中,所以越常存取這兩個表格,也越有可能與其他的資料庫操作
資料辭典的動作發生 IO 競爭。而且這兩個表格的異動也會產生重做資訊(REDO)與還原資訊(UNDO),造
成額外的管理成本。最重要的問題是將兩個相鄰的可用的擴充區塊合併為一塊較大的可用擴充區塊,這個
工作是由 SMON 每 12 小時進行一次,這個動作會消耗一定的系統資源。
設定說明管理是在建立表格空間時,利用 EXTENT MANAGEMENT DICTIONARY 子句,將該表格空間
設定為說明管理的表格空間。或者是使用
DBMS_SPACE_ADMIN.TABLESPACE_MIGRATE_FROM_LOCAL 將本機管理的表格空間變更為說明
管理。
SQL> SELECT tablespace_name,extent_management FROM dba_tablespaces
TABLESPACE_NAME EXTENT_MAN
------------------------------ --------------------
SYSTEM DICTIONARY
SYSAUX LOCAL
UNDOTBS LOCAL
TEMP LOCAL
USERS LOCAL
SQL> CREATE TABLESPACE testdictts
2 DATAFILE '/u02/oradata/ora11g/testdictts01.dbf' SIZE 10M
3 EXTENT MANAGEMENT DICTIONARY; --這個子句用來指定將表格空間為說明管理
SQL> EXECUTE DBMS_SPACE_ADMIN.TABLESPACE_MIGRATE_FROM_LOCAL(‘USERS’);
SQL> SELECT tablespace_name,extent_management FROM dba_tablespaces
TABLESPACE_NAME EXTENT_MAN
------------------------------ --------------------
SYSTEM DICTIONARY
SYSAUX LOCAL
UNDOTBS LOCAL
16. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
TEMP LOCAL
USERS DICTIONARY
TESTDICTTS DICTIONARY
--將表格空間變更為本機管理
SQL> EXECUTE DBMS_SPACE_ADMIN.TABLESPACE_MIGRATE_TO_LOCAL(‘USERS’);
SQL> SELECT tablespace_name,extent_management FROM dba_tablespaces
TABLESPACE_NAME EXTENT_MAN
------------------------------ --------------------
SYSTEM DICTIONARY
SYSAUX LOCAL
UNDOTBS LOCAL
TEMP LOCAL
USERS LOCAL
TESTDICTTS DICTIONARY
不過以上的說明都建立在一個重要的前提上,就是 SYSTEM 表格空間必須是說明管理,不然將會出現無
法建立說明管理表格空間的錯誤(ORA-12913: Cannot create dictionary managed tablespace)或者無
法將表格空間轉換為說明管理(ORA-12914: Cannot migrate tablespace to dictionary managed type)。
SQL> SELECT tablespace_name,extent_management FROM dba_tablespaces
TABLESPACE_NAME EXTENT_MAN
------------------------------ --------------------
SYSTEM LOCAL
SYSAUX LOCAL
UNDOTBS LOCAL
TEMP LOCAL
USERS LOCAL
SQL> CREATE TABLESPACE testdictts
2 DATAFILE '/u02/oradata/ora11g/testdictts01.dbf' SIZE 10M
3 EXTENT MANAGEMENT DICTIONARY; --這個子句用來指定將表格空間為說明管理
CREATE TABLESPACE testdictts
*
ERROR at line 1:
ORA-12913: Cannot create dictionary managed tablespace --因為SYSTEM表格空間已經是本機管理
因為上述的一些缺點,Oracle 資料庫由 Oracle8i 開始介紹一種新的擴充區塊管理方式-本機管理(Locally
Management),這種管理方式可以避免說明管理所帶來的副作用。因此從 Oracle9i 開始本機管理模式成
為表格空間預設的管理方式,而說明管理只是為了提供與之前的 Oracle 版本相容而繼續存在。同時如果
採用 DBCA(資料庫組態輔助管理程式)來建立一個新的資料庫,這時 SYSTEM 表格空間將會設定為本機
管理。
本機管理(Locally Management):
本機管理的擴充區塊管理方式,是在資料檔標頭(Datafile Header)中使用一組位元映像(Bitmap),呈現相
對的擴充區塊的使用狀態。如果位元值為 0,則表示相對的擴充區塊為可用的(Free),反之如果位元值為
1,則表示擴充區塊為使用中(Used)。不過一個位元到底代表幾個資料區塊?則是依據建立表格空間當時
所作的設定,一個位元可以代表一個資料區塊或多個資料區塊。當建立表格空間所使用的參數為
17. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
EXTENT MANAGEMENT LOCAL AUTOALLOCATE 時,這時一個位元代表一個資料區塊。若為
EXTENT MANAGEMENT LOCAL UNIFORM SIZE nM,這時一個位元代表多個資料區塊。
使用本機管理可以避免說明管理所造成的缺點,例如:擴充區塊的配置或收回,只需要變更位元地圖即可,
不需要異動資料辭典中的 FET$與 UET$這兩個表格,因此不會有 REDO 或 UNDO 的資料產生,以及資
料辭典的競爭相對也會減少。而且不需要 SMON 定期合併可用的擴充區塊,因為位元圖中的連續的位元
值為 0,就表示有連續的可用空間可供使用。
SQL> CREATE TABLESPACE testlocalts
2 DATAFILE '/u02/oradata/ora11g/testlocalts01.dbf' SIZE 10M
3 EXTENT MANAGEMENT LOCAL AUTOALLOCATE;
--將表格空間設定為本機管理,同時此表格空間上的每個區段,其擴充區塊的大小是由Oracle資料庫自動管
理。
--首先前16個擴充區塊的大小為64K =>16*64K=1M
--第17到79個擴充區塊的大小為1M =>63*1M=63M
--第80到199個擴充區塊的大小為8M=>200*8M=960M
--第200個擴充區塊開始皆為64M
SQL> SELECT tablespace_name,extent_management,allocation_type
2 FROM dba_tablespaces
3 WHERE tablespace_name='TESTLOCALTS';
TABLESPACE_NAME EXTENT_MAN ALLOCATIO
------------------------------ ----------------------- -----------------
TESTLOCALTS LOCAL SYSTEM
SQL> CREATE TABLESPACE testlocalts
2 DATAFILE '/u02/oradata/ora11g/testlocalts01.dbf' SIZE 10M
3 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;
--將表格空間設定為本機管理,同時此表格空間上的每個區段,其中每個擴充區塊的大小皆固定為1M。
SQL> SELECT tablespace_name,extent_management,allocation_type
2 FROM dba_tablespaces
3 WHERE tablespace_name='TESTLOCALTS';
TABLESPACE_NAME EXTENT_MAN ALLOCATIO
------------------------------ ----------------------- -----------------
TESTLOCALTS LOCAL UNIFORM
18. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
當每次建立一個新的區段或現有區段有增加空間的需求時,Oracle Server 會進行以下的步驟,尋找可用
的擴充區塊。
1.搜索資料檔的檔頭(File Header)中的位元映像(BitMap),尋找 Bit 值為 0 的那些 Bits。
AUTOALLOCATE:須先確定這個擴充區塊的大小(64K、1M、8M、64M),接著只要找到足夠多的連
續 Bit 值為 0,因為每個 Bit 代表一個資料區塊,如此便可以將這些連續空的資料區塊組成一個自由的擴
充區塊,並將這個擴充區塊配置給需要空間的區段。
UNIFORM:只要找到一個值為 0 的 Bit 即可,因為一個 Bit 代表一組資料區塊,也代表一個擴充區塊,
如此便可以將這個擴充區塊配置給需要空間的區段。
2.如果在 Bitmap 中找不到足夠多個 Bit 值為 0 的連續資料區塊,表示此資料檔的可用空間不足,如果此
資料檔沒有設定可以自動增長大小,將會出現新增表格時的錯誤”ORA-01652: unable to extend temp
segment by n in tablespace YYY”或延伸現有表格時的錯誤”ORA-01653: unable to extend table
XXX by n in tablespace YYY”。但是如果資料檔有設定可以自動增長大小,此時資料檔將會向檔案系
統要求新增空間,這時所新增的資料檔空間將會被當作可用的擴充區塊,這些新增的可用擴充區塊可以配
置給需要的區段。
6.2.2 區段空間管理(Segment Space Management)
32. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
使用資料庫加密功能之前,必須先建立錢包(Wallet),錢包用來儲存 Master Key。而 Master Key 用來解
密儲存在資料辭典的表格空間或表格欄位的加密鍵值,因為存放加密鍵值的表格為安全起見也被加密,所
使用的加、解密鍵值就是儲存在錢包中的 Master Key。資料庫管理者可以使用 Oracle Wallet Manager
或 SQL 敘述句建立錢包,不過要先設定錢包的位置,由 SQLNET.ORA 中
ENCRYPTION_WALLET_LOCATION 來指定錢包的位置。但錢包的名字與型態有其固定的設定,錢包
名字固定為 ewallet.p12。而錢包的型態為 Public Key Cryptography Standard No. 12(PKCS#12),並且
由一個密碼保護,這個密碼在建立錢包時指定。
所建立的錢包可以由 v$encryption_wallet 查詢得知,錢包的型態位置與是否開啟。以及可以使用
ALTER SYSTEM SET ENCRYPTION WALLET {CLOSE | OPEN IDENTIFIED BY password}將錢包
開啟或關閉,當錢包關閉時,所有被加密的表格空間或表格欄位的內容,都無法被存取。
SQL> SELECT * FROM v$encryption_wallet;
WRL_TYPE WRL_PARAMETER STATUS
------------------- ---------------------------------------------- -------------
file /home/oracle/encryption_wallet/ OPEN
SQL> ALTER SYSTEM SET ENCRYPTION WALLET CLOSE;
/*關閉錢包後,所有被加密的表格空間或表格欄位的內容,將無法被存取。*/
SQL> SELECT * FROM v$encryption_wallet;
WRL_TYPE WRL_PARAMETER STATUS
------------------- ---------------------------------------------- -------------
file /home/oracle/encryption_wallet/ CLOSED
SQL> ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY oracle;
/*必須提供錢包密碼才能開啟錢包*/
SQL> SELECT * FROM v$encryption_wallet;
WRL_TYPE WRL_PARAMETER STATUS
------------------- ---------------------------------------------- -------------
file /home/oracle/encryption_wallet/ OPEN
33. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
錢包搜尋順序
當 Oracle 伺服器為取得 Master Key 時,必須先找到錢包。而錢包可能在下列三個位置找到:
1.SQLNET.ORA 的 ENCRYPTION_WALLET_LOCATION 所設定的位置。
2.若第一個位置找不到錢包,則繼續由 SQLNET.ORA 的 WALLET_LOCATION 所指定的位置搜尋。
3.若第二個位置也找不到錢包,則使用預設位置$ORACLE_BASE/admin/$ORACLE_SID/wallet 來尋
找錢包。
設定表格空間加密功能
一但表格空間被設定為加密表格空間後,就無法轉換為未加密表格空間,反之未加密表格空間也不能改為
加密表格空間。所以只能在建立表格空間時,設定該表格空間是否要被加密,事後不能改變。
設定表格空間加密時,可以指定加密所使用的演算法有 3DES168、AES128、AES192、AES256 四種,
預設值為 AES192。
SQL> CREATE TABLESPACE demots_encrypt
2 DATAFILE '/u02/oradata/orcl/demots_encrypt01.dbf' SIZE 10M
3 ENCRYPTION --使用預設的AES128,若想使用其他演算法,ENCRYPTION USING 演算法 即可。
4 DEFAULT STORAGE(ENCRYPT);
Tablespace created.
SQL> SELECT tablespace_name,encrypted FROM dba_tablespaces;
TABLESPACE_NAME ENC
------------------------------ ----
SYSTEM NO
SYSAUX NO
UNDOTBS1 NO
TEMP NO
USERS NO
EXAMPLE NO
DEMOTS NO
DEMOTS_ENCRYPT YES
--可以由v$encrypted_tablespaces查知加密的表格空間所使用的加密演算法為何?
SQL> SELECT tn.name,et.encryptionalg
2 FROM v$tablespace tn JOIN v$encrypted_tablespaces et ON (tn.ts#=et.ts#);
NAME ENCRYPT
------------------------------ -------------
DEMOTS AES128
34. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
建立表格空間的語法範例
CREATE {SMALLFILE|BIGFILE} {TEMPORARY|UNDO} TABLESPACE 表格空間名稱
DATAFILE‘檔案名稱’SIZE n {AUTOEXTEND OFF|ON NEXT n MAXSIZE m}
EXTENT MANAGEMENT {DICTIONARY|LOCAL {AUTOALLOCATE|UNIFORM SIZE n}}
SEGMENT SPACE MANAGEMENT {AUTO|MANUAL}
ENCRYPTION USING ‘encryption algorithm’ DEFAULT STORAGE(ENCRYPT)
SQL> CREATE SMALLFILE TABLESPACE demots
2> DATAFILE ‘/u02/oradata/ora11g/demots01.dbf’ SIZE 100M
3> AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
4> EXTENT MANAGEMENT LOCAL AUTOALLOCATE
5> SEGMENT SPACE MANAGEMENT AUTO;
/*這個SQL指令建立一個表格空間叫做DEMOTS,可以由多達1023個資料檔組成(SMALLFILE),不過目前僅
有一個資料檔/u02/oradata/ora11g/demots01.dbf,檔案初始大小為100M,如果不足,則會自動每次增長10
M,直到資料檔的最大值為止(4000000個資料區塊)。此表格空間的擴充區塊由Oracle自動管理,每個擴充區
塊的大小也由Oracle自動管理(AUTOALLOCATE),區段中的資料區塊也由Oracle自動管理(SEGMENT SPA
CE MANAGEMENT AUTO)。這個表格空間可以放置任何形態的區段,因為其內容為永久型態(Permanent)
的表格空間。*/
SQL> CREATE TEMPORARY TABLESPACE tempts
2> TEMPFILE ‘/u02/oradata/ora11g/tempts01.dbf’ SIZE 100M
3> EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K;
36. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
------------------------------- -----------------
FRANKTS 10485760
/*目前此表格空間大小為10M*/
SQL> SELECT file_id,file_name,bytes FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES
------------- -------------------------------------------------- -------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760
/*目前此表格空間為一個資料檔所組成*/
SQL> ALTER TABLESPACE frankts ADD DATAFILE '/u02/oradata/ora11g/frankts02.dbf' SIZE 10M;
/*新增一個資料檔/u02/oradata/ora11g/frankts02.dbf到表格空間frankts之中*/
SQL> SELECT file_id,file_name,bytes FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES
------------- -------------------------------------------------- -------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760
7 /u02/oradata/ora11g/frankts02.dbf 10485760
/*若要新增資料檔時,將資料檔設定為自動成長,只要在SIZE參數的後面,加上NEXT與MAXSIZE參數即
可。相關的範例請參考5.2的內容*/
SQL> SELECT tablespace_name,sum(bytes) FROM dba_data_files WHERE tablespace_name='FRAN
KTS' GROUP BY tablespace_name;
TABLESPACE_NAME SUM(BYTES)
------------------------------- -----------------
FRANKTS 20971520
/*經過新增資料檔後,表格空間frankts變成擁有20M的空間*/
減少資料檔的個數(僅適用於某種特殊狀況)
只有當資料檔的內容是空的,才可以被刪除,這裡所謂的”空”,是表示這個資料檔上沒有任何擴充區塊
存在(用過(Used)或可用(Free))。而且每個表格空間至少要存在一個資料檔存在,除非是暫時表格空間。
SQL> SELECT file_id,file_name,bytes FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES
------------- -------------------------------------------------- ------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760
7 /u02/oradata/ora11g/frankts02.dbf 10485760
/*目前表格空間frankts由兩個資料檔組成*/
SQL> ALTER TABLESPACE frankts DROP DATAFILE '/u02/oradata/ora11g/frankts02.dbf';
/*刪除資料檔/u02/oradata/ora11g/frankts02.dbf’*/
SQL> SELECT file_id,file_name,bytes FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES
------------- -------------------------------------------------- -------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760
/*資料檔/u02/oradata/ora11g/frankts02.dbf’成功地被刪除*/
SQL> ALTER TABLESPACE frankts ADD DATAFILE '/u02/oradata/ora11g/frankts02.dbf' SIZE 10M;
/*新增資料檔/u02/oradata/ora11g/frankts02.dbf’到表格空間frankts中*/
37. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> SELECT file_id,file_name,bytes FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES
------------- -------------------------------------------------- ------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760
7 /u02/oradata/ora11g/frankts02.dbf 10485760
SQL> CREATE TABLE system.t1 TABLESPACE frankts AS SELECT * FROM dba_objects;
/*建立一個表格system.t1放在frankts這個表格空間中*/
SQL> SELECT file_id,owner,segment_name,segment_type,count(*) FROM dba_extents WHERE table
space_name='FRANKTS' GROUP BY file_id,owner,segment_name,segment_type;
FILE_ID OWNER SEGMENT_NAME SEGMENT_TY COUNT(*)
------------- --------------- ------------------------------ --------------------- --------------
5 SYSTEM T1 TABLE 1
7 SYSTEM T1 TABLE 16
/*system.t1的擴充區塊有1個放在/u02/oradata/ora11g/frankts01.dbf上,其餘的16個都放在/u02/oradata/ora
11g/frankts02.dbf’上*/
SQL> ALTER TABLESPACE frankts DROP DATAFILE '/u02/oradata/ora11g/frankts02.dbf';
ALTER TABLESPACE frankts DROP DATAFILE '/u02/oradata/ora11g/frankts02.dbf'
*
ERROR at line 1:
ORA-03262: the file is non-empty
/*即便是將區段system.t1刪除,還是無法刪除此資料檔*/
例外:暫時表格空間可以連一個暫時檔都不需要存在,只是當進行磁碟排序時,會出現錯誤訊息
SQL> SELECT tablespace_name FROM dba_tablespaces WHERE contents='TEMPORARY';
TABLESPACE_NAME
------------------------------
TEMPTS1
TEMPFRANK
SQL> SELECT tablespace_name,file_id,file_name FROM dba_temp_files;
TABLESPACE_NAME FILE_ID FILE_NAME
------------------------------ ---------- --------------------------------------------------
TEMPTS1 1 /u02/oradata/ora11g/temp01.dbf
TEMPFRANK 2 /u02/oradata/omf/ORA11G/datafile/o1_mf_tempfran_473mq0xr_.tmp
SQL> ALTER DATABASE TEMPFILE '/u02/oradata/ora11g/temp01.dbf' DROP INCLUDING DATAFILE
S;
/*刪除此暫時檔/u02/oradata/ora11g/temp01.dbf*/
SQL> SELECT tablespace_name,file_id,file_name FROM dba_temp_files;
TABLESPACE_NAME FILE_ID FILE_NAME
------------------------------ ----------- ----------------------------------------------------------------------------------------
TEMPFRANK 2 /u02/oradata/omf/ORA11G/datafile/o1_mf_tempfran_473mq0xr_.tmp
SQL> SELECT tablespace_name FROM dba_tablespaces WHERE contents='TEMPORARY';
TABLESPACE_NAME
------------------------------
TEMPTS1
38. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
TEMPFRANK
變更資料檔的大小(Datafile Resize)
變更資料檔的大小,可以由資料庫管理者或 Oracle 自動操作達到。不過 Oracle 自動操作只能增加資料檔
的大小,資料庫管理者確可以增加或減少資料檔大小。
SQL> CREATE TABLESPACE frankts DATAFILE '/u02/oradata/ora11g/frankts01.dbf' SIZE 10M AUTO
EXTEND ON NEXT 10M MAXSIZE 100M;
/*建立一個表格空間frankts,由一個資料檔組成。一開始這個資料檔為10M,如果沒有剩餘空間可供使用,將
會一次增加10M,一直到資料檔為100M為止。*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespac_name='FRANKTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
------------ -------------------------------------------------- ------------ ------ ----------------------- ----------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760 YES 1280 104857600
SQL> SELECT file#,name,bytes,create_bytes,block_size FROM v$datafile WHERE file#=5;
FILE# NAME BYTES CREATE_BYTES BLOCK_SIZE
------------ -------------------------------------------------- ------------- ----------------------- -----------------
5 /u02/oradata/ora11g/frankts01.dbf 10485760 10485760 8192
/*v$datafile的create_bytes是這個資料檔的初始大小,bytes為目前的大小。*/
SQL> ALTER DATABASE DATAFILE '/u02/oradata/ora11g/frankts01.dbf' RESIZE 20M;
/*將資料檔的大小增加為20M*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
------------ -------------------------------------------------- ------------- ------ ----------------------- -----------------
5 /u02/oradata/ora11g/frankts01.dbf 20971520 YES 1280 104857600
SQL> SELECT file#,name,bytes,create_bytes,block_size FROM v$datafile WHERE file#=5;
FILE# NAME BYTES CREATE_BYTES BLOCK_SIZE
------------ -------------------------------------------------- ------------- ----------------------- -----------------
5 /u02/oradata/ora11g/frankts01.dbf 20971520 10485760 8192
SQL> CREATE TABLE system.t1 TABLESPACE frankts AS SELECT * FROM dba_objects;
/*建立一個表格system.t1放在表格空間frankts中*/
SQL> SELECT SUM(bytes) FROM dba_extents WHERE tablespace_name='FRANKTS';
SUM(BYTES)
-----------------
2097152
/*目前表格空間frankts已經被使用的空間大約為2M*/
SQL> INSERT INTO system.t1 SELECT * FROM dba_objects;
SQL> INSERT INTO system.t1 SELECT * FROM dba_objects;
SQL> commit;
39. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> SELECT SUM(bytes) FROM dba_extents WHERE tablespace_name='FRANKTS';
SUM(BYTES)
-----------------
6291456
/*經過一些DML後,目前此表格空間已經被使用的空間,已經達到6M左右。*/
SQL> ALTER DATABASE DATAFILE '/u02/oradata/ora11g/frankts01.dbf' RESIZE 10M;
/*將資料檔縮小為10M*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespace_name='FRANKTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
------------ -------------------------------------------------- ------------- ------ ----------------------- -----------------
5 /u02/oradata/ora11g/frankts01.dbf 20971520 YES 1280 104857600
SQL> ALTER DATABASE DATAFILE '/u02/oradata/ora11g/frankts01.dbf' RESIZE 5M;
ALTER DATABASE DATAFILE '/u02/oradata/ora11g/frankts01.dbf' RESIZE 5M
*
ERROR at line 1:
ORA-03297: file contains used data beyond requested RESIZE value
/*想要更進一步地,將資料檔縮小到5M,但卻失敗了,因為目前已使用的空間已經大於5M。*/
SQL> CREATE BIGFILE TABLESPACE bigts DATAFILE '/u02/oradata/ora11g/bigts01.dbf' SIZE 10M;
/*建立一個BIGFILE型態的表格空間bigts,資料檔大小為10M,但是不會自動增加大小。*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespace_name='BIGTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
---------- -------------------------------------------------- ------------- ------ ----------------------- -----------------
7 /u02/oradata/ora11g/bigts01.dbf 10485760 NO 0 0
SQL> ALTER TABLESPACE bigts AUTOEXTEND ON ;
/*將資料檔設定為自動增加大小。不過因為是BIGFILE型態的表格空間,所以可以使用表格空間的名字取代資
料檔的名字,因為一個BIGFILE表格空間中只能有一個資料檔。*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespace_name='BIGTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
---------- -------------------------------------------------- ------------- ------ ----------------------- -----------------
7 /u02/oradata/ora11g/bigts01.dbf 10485760 YES 1 3.5184E+13
SQL> ALTER TABLESPACE bigts RESIZE 20M;
/*因為bigts表格空間為BIGFILE格式。*/
SQL> ALTER TABLESPACE bigts AUTOEXTEND OFF;
/*因為bigts表格空間為BIGFILE格式。*/
SQL> SELECT file_id,file_name,bytes,autoextensible,increment_by,maxbytes FROM dba_data_files W
HERE tablespace_name='BIGTS';
FILE_ID FILE_NAME BYTES AUT INCREMENT_BY MAXBYTES
---------- -------------------------------------------------- ------------- ------ ----------------------- -----------------
7 /u02/oradata/ora11g/bigts01.dbf 20971520 NO 0 0
40. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
縮小暫時表格空間(Shrink Space)
暫時表格空間的主要用途為存放進行磁碟排序時,所產生的暫時資料。這些暫時資料在排序完成後就不再
被需要,其所佔的空間可以被重複使用,以避免暫時表格空間的大小成長。但是如果同時間有大量的磁碟
排序需求,可能造成暫時表格空間的空間成長,但是事後卻沒有如此多的空間需求,導致暫時表格空間過
大。自 Oracle11g 後,資料庫管理者可以縮小暫時表格空間的大小,而不需要重建暫時表格空間。
SQL> SELECT tablespace_name FROM dba_tablespaces WHERE contents='TEMPORARY';
TABLESPACE_NAME
------------------------------
TEMPTS1
TEMPFRANK
SQL> SELECT tablespace_name,file_id,file_name FROM dba_temp_files;
TABLESPACE_NAME FILE_ID FILE_NAME
------------------------------ ----------- ----------------------------------------------------------------------------------------
TEMPFRANK 2 /u02/oradata/omf/ORA11G/datafile/o1_mf_tempfran_473mq0xr_.tmp
SQL> ALTER TABLESPACE tempts1 ADD TEMPFILE '/u02/oradata/ora11g/temp01.dbf' SIZE 100M;
/*加一個暫時檔到表格空間tempts1,此暫時檔大小為100M*/
SQL> SELECT tablespace_name,file_id,file_name FROM dba_temp_files;
TABLESPACE_NAME FILE_ID FILE_NAME
------------------------------ ----------- ----------------------------------------------------------------------------------------
TEMPTS1 1 /u02/oradata/ora11g/temp01.dbf
TEMPFRANK 2 /u02/oradata/omf/ORA11G/datafile/o1_mf_tempfran_473mq0xr_.tmp
SQL> SELECT SUM(bytes) FROM dba_temp_files WHERE tablespace_name='TEMPTS1';
SUM(BYTES)
------------------
104857600
SQL> ALTER TABLESPACE tempts1 SHRINK SPACE KEEP 20M;
/*將暫時表格空間縮小到20M,但不會影響到正在進行的磁碟排序。*/
SQL> SELECT SUM(bytes) FROM dba_temp_files WHERE tablespace_name='TEMPTS1';
SUM(BYTES)
------------------
22020096
SQL> ALTER TABLESPACE tempts1 SHRINK SPACE ;
/*儘可能將暫時表格空間縮到最小。*/
SQL> SELECT SUM(bytes) FROM dba_temp_files WHERE tablespace_name='TEMPTS1';
SUM(BYTES)
------------------
1114112
/*暫時檔被縮小到1M*/
41. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> ALTER DATABASE TEMPFILE '/u02/oradata/ora11g/temp01.dbf' RESIZE 100M;
/*將暫時檔設定為100M*/
SQL> SELECT SUM(bytes) FROM dba_temp_files WHERE tablespace_name='TEMPTS1';
SUM(BYTES)
------------------
104857600
6.3.2 變更表格空間的狀態
一個表格空間的狀態可以分成:ONLINE(線上) 、OFFLINE(離線)、Read Only(唯讀)等三種。
線上(ONLINE)
表示這個表格空間中的區段(SEGMENT)的資料可以被讀/寫(Read/Write),或進行任何區段定義的操作,
如建立、刪除、修改等操作 ,這種狀態是表格空間的預設狀態。
SQL> SELECT tablespace_name,status FROM dba_tablespaces WHERE tablespace_name='BIGTS';
TABLESPACE_NAME STATUS
------------------------------ ------------
BIGTS ONLINE
SQL> CREATE TABLE system.t2 TABLESPACE bigts AS SELECT * FROM dba_objects;
/*建立一個測試表格在bigts表格空間中。*/
SQL> SELECT COUNT(*) FROM system.t2;
COUNT(*)
--------------
15714
SQL> DELETE system.t2 WHERE object_id<=10;
SQL> COMMIT;
/*system.t這個表格目前可以被DML。*/
SQL> SELECT COUNT(*) FROM system.t2;
COUNT(*)
----------
15705
42. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> ALTER TABLE system.t2 DROP COLUMN namespace;
/*system.t2這個表格目前可以被DDL。*/
SQL> DROP TABLE system.t2;
/*system.t2這個表格可以被刪除。*/
唯讀(READ ONLY)
表示這個表格空間只允許查詢(Query)操作,不能夠進行區段的資料異動(DML)或建立區段的操作。但是
可以刪除或修改區段的定義(因為區段的定義是存放在 SYSTEM 表格空間中,只有區段的資料才是存放在
一般的表格空間中,所以可以進行那些不需要異動區段所在表格空間內容的操作)。
資料庫管理者進行唯讀某個表格空間的操作時,這個操作可能不會立刻完成。因為 Oracle 資料庫必須等
到所有已經對這個表格空間中的資料進行異動的交易都結束(交易確認或退回)後,才會完成將表格空間設
定為唯讀狀態的操作,這種狀態稱作 Transitional Read Only。之後 DBWR 會將所有屬於此表格空間的
DIRTY BUFFERs 都寫回相對的資料檔,CKPT 將相對的檢查點資訊(Checkpoint Information)記錄到所
有的控制檔與該表格空間中所有的資料檔檔頭。最後 Oracle 資料庫便會將資料檔的檔頭與資料檔都設定
為唯讀,之後任何的異動都不允許發生。
為什麼要將表格空間狀態設為唯讀?因為有時候需要將表格空間的內容開放查詢,但卻又不希望表格空間
內容被改變,這時將表格空間狀態設定為唯讀便是一個好的選擇。當然也可以用在需要將磁碟空間讓給需
要異動的表格空間,所以要將某些表格空間搬移到唯讀裝置(WROM)。
註記:SYSTEM、SYSAUX 與所有還原(UNDO)或暫時(TEMPORARY)型態的表格空間都不能設定唯讀
狀態。
SQL> SELECT tablespace_name,status FROM dba_tablespaces WHERE tablespace_name='BIGTS';
TABLESPACE_NAME STATUS
------------------------------- ------------
BIGTS ONLINE
SQL> CREATE TABLE system.t3 TABLESPACE bigts AS SELECT * FROM dba_objects;
/*建立一個測試表格在bigts表格空間。*/
SQL> ALTER TABLESPACE bigts READ ONLY;
/*將bigts表格空間的狀態設定為唯讀。*/
SQL> SELECT tablespace_name,status FROM dba_tablespaces WHERE tablespace_name='BIGTS';
TABLESPACE_NAME STATUS
------------------------------ ------------------
BIGTS READ ONLY
SQL> DELETE system.t3 WHERE object_id<=10;
DELETE system.t3 WHERE object_id<=10
*
ERROR at line 1:
ORA-00372: file 7 cannot be modified at this time
ORA-01110: data file 7: '/u02/oradata/ora11g/bigts01.dbf'
/*目前此表格空間內的任何區段都不能被異動。*/
SQL> ALTER TABLE system.t3 DROP COLUMN secondary;
ALTER TABLE system.t3 DROP COLUMN secondary
*
43. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
ERROR at line 1:
ORA-12985: tablespace 'BIGTS' is read only, cannot drop column
/*當表格空間狀態為唯讀時,只能進行不會影響到此表格空間中區段的DDL指令,如DROP TABLE。但是如
果是DROP COLUMN會出現錯誤訊息,因為除了異動資料辭典外,也會異動表格內容。*/
SQL> alter table system.t3 add (testcol2 varchar2(10) default 'TestCol2');
alter table system.t3 add (testcol2 varchar2(10) default 'TestCol2')
*
ERROR at line 1:
ORA-00372: file 7 cannot be modified at this time
ORA-01110: data file 7: '/u02/oradata/ora11g/bigts01.dbf'
SQL> ALTER TABLE system.t3 ADD (testcol VARCHAR2(10));
/*這個ADD COLUMN可以成功執行,因為所加入的欄位並沒有預設值,所以只有異動到資料辭典(SYSTEM
表格空間),並未異動到bigts表格空間上的區段。*/
SQL> DROP TABLE system.t3;
/*這個指令只影響到資料辭典內容(Dictionary Managed Tablespace)或資料辭典內容與資料檔檔頭(Locally
Managed Tablespace)。*/
SQL> CREATE TABLE system.t4 TABLESPACE bigts AS SELECT * FROM dba_objects;
CREATE TABLE system.t4 TABLESPACE bigts AS SELECT * FROM dba_objects
*
ERROR at line 1:
ORA-01647: tablespace 'BIGTS' is read only, cannot allocate space in it
SQL> ALTER TABLESPACE bigts READ WRITE;
/*將表格空間狀態設定為可讀寫即線上。*/
45. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
SQL> ALTER TABLESPACE bigts OFFLINE;
/*如果OFFLINE後面,沒有加上任何參數,表示採用一般(NORMAL)模式。
暫時模式:OFFLINE TEMPORARY
立即模式:OFFLINE IMMEDIATE
用於復原:OFFLINE FOR RECOVERY */
SQL> SELECT tablespace_name,status FROM dba_tablespaces WHERE tablespace_name='BIGTS';
TABLESPACE_NAME STATUS
------------------------------ -------------
BIGTS OFFLINE
SQL> SELECT COUNT(*) FROM system.t4;
SELECT COUNT(*) FROM system.t4
*
ERROR at line 1:
ORA-00376: file 7 cannot be read at this time
ORA-01110: data file 7: '/u02/oradata/ora11g/bigts01.dbf'
/*目前這個表格空間不能被存取(不能讀也不能異動)。*/
SQL> ALTER TABLE system.t4 DROP COLUMN namespace;
ALTER TABLE system.t4 DROP COLUMN namespace
*
ERROR at line 1:
ORA-12993: tablespace 'BIGTS' is offline, cannot drop column
/*DROP COLUMN除異動資料辭典外,也會異動表格資料。*/
SQL> ALTER TABLE system.t4 ADD (testcol1 varchar2(10));
/*ADD COLUMN沒有設定預設值,只異動資料辭典,並不會異動表格資料。*/
SQL> ALTER TABLE system.t4 ADD (testcol2 varchar2(10) default 'TestCol2');
ALTER TABLE system.t4 ADD (testcol2 varchar2(10) default 'TestCol2')
*
ERROR at line 1:
ORA-00376: file 7 cannot be read at this time
ORA-01110: data file 7: '/u02/oradata/ora11g/bigts01.dbf'
/*ADD COLUMN加上DEFAULT,除異動資料辭典外,也會異動表格資料。*/
SQL> DROP TABLE system.t4;
/*這個指令只影響到資料辭典內容(Dictionary Managed Tablespace)或資料辭典內容與資料檔檔頭(Locally
Managed Tablespace)。*/
6.3.3 變更表格空間的名字
自 Oracle10g 開始,資料庫管理者可以動態地變更表格空間的名字。不過有些條件必須遵守,才能變更
表格空間的名字。
1.表格空間 SYSTEM、SYSAUX,不能改變其名字。
2.若該表格空間已經離線或任何屬於該表格空間的資料檔已經離線,更名的操作將會失敗。
SQL> show parameter compatible
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
compatible string 11.1.0
46. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
/*compatible至少要為10.0.0以上*/
SQL> SELECT tablespace_name FROM dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS
TEMPTS1
USERS
TEMPFRANK
BIGTS
SQL> ALTER TABLESPACE bigts RENAME TO bigts_new;
SQL> SELECT tablespace_name FROM dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS
TEMPTS1
USERS
TEMPFRANK
BIGTS_NEW
6.3.4 變更表格空間中的資料檔名字或位置
有時候資料檔的名字或位置,因為某些原因需要改變。資料庫管理者可以利用下列的步驟完成資料檔更名
或變更資料檔位置的操作。不過在變更資料檔名字與位置之前,該資料檔與表格空間必須先離線。然而有
些表格空間不能在資料庫開啟狀態下被離線,因此將變更資料檔的方法有兩種不同的方法,依表格空間不
同而使用不同的方式。
可離線的表格空間
SQL> SELECT file_name FROM dba_data_files WHERE tablespace_name='USERS';
FILE_NAME
--------------------------------------------------
/u02/oradata/ora11g/users01.dbf
SQL> ALTER TABLESPACE users OFFLINE;
/*想要將表格空間users的資料檔,由/u02/oradata/ora11g/users01.dbf搬到/u03/oradata/ora11g/users01.d
bf。*/
SQL> host
[oracle@ELinux ~]$ cp /u02/oradata/ora11g/users01.dbf /u03/oradata/ora11g/users01.dbf
/*使用OS複製的指令搬移檔案或是使用更名的指令*/
[oracle@ELinux ~]$ exit
exit
SQL> ALTER TABLESPACE users
2 RENAME DATAFILE
47. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
3 '/u02/oradata/ora11g/users01.dbf'
4 TO
5 '/u03/oradata/ora11g/users01.dbf';
/*使用ALTER TABLESPACE指令變更資料檔名字。*/
SQL> ALTER TABLESPACE users ONLINE;
/*將表格空間狀態改為線上。*/
SQL> SELECT file_name FROM dba_data_files WHERE tablespace_name='USERS';
FILE_NAME
--------------------------------------------------
/u03/oradata/ora11g/users01.dbf
/*資料檔已經成功地搬移*/
不可離線的表格空間
有些表格空間不能夠在資料庫開啟時離線,如 SYSTEM 表格空間。所以必須先將資料庫正常關閉後,重
新掛載資料庫,此時執行處理(Instance)只開啟控制檔,並未開啟資料檔,便跳脫了不能夠離線的限制了。
SQL> ALTER TABLESPACE system OFFLINE;
ALTER TABLESPACE system OFFLINE
*
ERROR at line 1:
ORA-01541: system tablespace cannot be brought offline; shut down if necessary
SQL> SHUTDOWN IMMEDIATE
/*正常關閉資料庫*/
SQL> exit
[oracle@ELinux ~]$ cp /u02/oradata/ora11g/system01.dbf /u03/oradata/ora11g/system01.dbf
/*使用OS指令搬移資料檔*/
[oracle@ELinux ~]$ sqlplus / as sysdba
Connected to an idle instance.
SQL> startup mount
/*掛載資料庫*/
SQL> SELECT name FROM v$datafile WHERE file#=1;
NAME
--------------------------------------------------------------------------------------------------------------
/u02/oradata/ora11g/system01.dbf
SQL> ALTER DATABASE RENAME FILE
2 '/u02/oradata/ora11g/system01.dbf'
3 TO
4 '/u03/oradata/ora11g/system01.dbf';
/*使用ALTER DATABASE指令變更資料檔名字,這時後還不能使用ALTER TABLESPACE指令,因為資料
庫尚未開啟。*/
SQL> SELECT name FROM v$datafile WHERE file#=1;
NAME
--------------------------------------------------------------------------------------------------------------
48. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
/u03/oradata/ora11g/system01.dbf
SQL> ALTER DATABASE OPEN;
SQL> SELECT file_id,file_name FROM dba_data_files WHERE tablespace_name='SYSTEM';
FILE_ID FILE_NAME
---------- --------------------------------------------------
1 /u03/oradata/ora11g/system01.dbf
/*資料檔已經成功地搬移*/
6.3.5 刪除表格空間
當一個表格空間不再被需要後,為節省儲存空間與資料庫管理方便。資料庫管理者通常會選擇刪除這些不
要的表格空間,以下為刪除表格空間的步驟。
SQL> CREATE TABLESPACE frankts datafile '/u02/oradata/ora11g/frankts01.dbf' SIZE 10M;
/*建立一個測試的表格空間*/
SQL> SELECT file_name FROM dba_data_files WHERE tablespace_name='FRANKTS';
FILE_NAME
--------------------------------------------------
/u02/oradata/ora11g/frankts01.dbf
SQL> DROP TABLESPACE frankts ;
/*當一個表格空間被刪除時,僅將資料辭典與控制檔中的相關資料刪除,而該表格空間的檔案並未自動被刪除
(如果資料檔是以OMF格式建立,則會自動被刪除)。因此資料庫管理者需要自行使用OS指令將其刪除。*/
SQL> host
[oracle@ELinux ~]$ ls /u02/oradata/ora11g/frankts01.dbf
/u02/oradata/ora11g/frankts01.dbf
[oracle@ELinux ~]$ rm /u02/oradata/ora11g/frankts01.dbf
[oracle@ELinux ~]$ exit
exit
SQL> CREATE TABLESPACE frankts datafile '/u02/oradata/ora11g/frankts01.dbf' SIZE 10M;
SQL> CREATE TABLE system.t4 TABLESPACE frankts AS SELECT * FROM dual;
/*建立一個測試用表格在frankts上*/
SQL> DROP TABLESPACE frankts;
DROP TABLESPACE frankts
*
ERROR at line 1:
ORA-01549: tablespace not empty, use INCLUDING CONTENTS option
/*因為此表格空間中已經有區段存在,必須使用INCLUDING CONTENTS選項,先將此表格空間上的區段都
刪除後,才能刪除此表格空間。但若此表格空間上的區段被其他表格空間中的區段參考(即有外鍵參考Foreign
Key Reference),則必須再加上CASCADE CONSTRAINTS選項。例如:DROP TABLESPACE frankts IN
CLUDING CONTENTS CASCADE CONSTRAINTS。*/
SQL> DROP TABLESPACE frankts INCLUDING CONTENTS AND DATAFILES;
/* INCLUDING CONTENTS AND DATAFILES選項,除可以將表格空間上的區段都刪除外,同時也要求將O
S上的資料檔一起刪除,節省資料庫管理者的負擔。*/
SQL> host
[oracle@ELinux ~]$ ls /u02/oradata/ora11g/frankts01.dbf
ls: /u02/oradata/ora11g/frankts01.dbf: 沒有此一檔案或目錄
49. Chapter 6 Tablespace
Oracle Database 11g 資料庫管理入門
[oracle@ELinux ~]$ exit
exit
5.4 常用的資料辭典視圖
身為 Oracle 資料庫管理者需要能夠從資料辭典中擷取出所需要的資訊,因此記憶一些常用的資料辭典視
圖是必要的。以下的資料辭典視圖是有關於表格空間的視圖,其中為節省篇幅每個視圖僅列出常用的欄位,
如果需要更多的欄位資訊,請自行使用 SQL*PLUS 指令:DESCRIBE 來查詢相關視圖定義。
SQL> SHOW USER
USER is "SYS"
SQL> DESCRIBE dba_tablespaces
Name Null? Type
--------------------------------------------------------- ------------------- ----------------------------------------
TABLESPACE_NAME NOT NULL VARCHAR2(30)
BLOCK_SIZE NOT NULL NUMBER
INITIAL_EXTENT NUMBER
NEXT_EXTENT NUMBER
MIN_EXTENTS NOT NULL NUMBER
MAX_EXTENTS NUMBER
MAX_SIZE NUMBER
PCT_INCREASE NUMBER
MIN_EXTLEN NUMBER
STATUS VARCHAR2(9)
CONTENTS VARCHAR2(9)
LOGGING VARCHAR2(9)
FORCE_LOGGING VARCHAR2(3)
EXTENT_MANAGEMENT VARCHAR2(10)
ALLOCATION_TYPE VARCHAR2(9)
PLUGGED_IN VARCHAR2(3)
SEGMENT_SPACE_MANAGEMENT VARCHAR2(6)
DEF_TAB_COMPRESSION VARCHAR2(8)
RETENTION VARCHAR2(11)
BIGFILE VARCHAR2(3)
PREDICATE_EVALUATION VARCHAR2(7)
ENCRYPTED VARCHAR2(3)
COMPRESS_FOR VARCHAR2(18)