Practices for Lesson 7: Managing Database Storage Structures
2020.01.29 BoobooWei
实践7:管理存储
实践7:概览
实践7-1:查看数据库存储结构信息
Overview
Task
Practice
问题1:EXAMPLE表空间几乎满了。在不采取任何操作的情况下,表空间的大小是否可以增加
问题2:EXAMPLE表空间中有多少段?
问题3:EXAMPLE表空间中,哪个索引占用了最多的空间?
KnowledgePoint
实践7-2:创建一个表空间
Overview
Task
Practice
KnowledgePoint
实践7:概览
Practices for Lesson 7: Overview
You need to view existing storage structure information and create a new tablespace for the INVENTORY application. You also need to create a database user to perform all administrative tasks without using the SYS and SYSTEM accounts.
您需要查看现有的存储结构信息,并为INVENTORY应用程序创建一个新的表空间。您还需要创建一个数据库用户来执行所有管理任务,而不需要使用SYS和SYSTEM帐户。
实践7-1:查看数据库存储结构信息
Practice 7-1: Viewing Database Storage Structure Information
Overview
In this practice, you examine existing storage structure information for your database.
Assumptions: The lab_03_03_01.sh script has already been executed to create a user named DBA1 in the database. This user has SYSDBA privileges.
在这个实践中,您将检查数据库的现有存储结构信息。
假设:lab_03_03_01.sh
脚本已经执行,以便在PDB数据库中创建一个名为DBA1
的用户。该用户具有SYSDBA
特权。
[oracle@oracle01 labs]$ pwd |
这里注意,根据自己的实际情况去修改环境变量中的值,以及脚本。
Task
Launch Enterprise Manager Cloud Control and log in as ADMIN.
Navigate to the orcl Database Home page.
- The orcl Database Home page appears.
- Using Cloud Control, view information about the EXAMPLE tablespace and answer the given questions.
Question 1: The EXAMPLE tablespace is nearly full. Can the tablespace size increase without you having to take any action?
Question 2: How many segments are there in the EXAMPLE tablespace?
Question 3: Which index in the EXAMPLE tablespace takes up the most space?
Practice
任务要求使用云管理器,此处我们修改为使用EM。
登陆到EM中,访问PDB数据库。
创建实验用的表空间
EXAMLE
。CREATE SMALLFILE TABLESPACE "EXAMPLE"
DATAFILE
'/u01/app/oracle/oradata/booboo/booboopdb1/example.dbf' SIZE 100M AUTOEXTEND OFF
LOGGING
DEFAULT NOCOMPRESS NO INMEMORY
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT AUTO;创建使用用的表,将该表空间已使用占比超过95%。
SCOTT@booboopdb1>conn dba1/oracle_4U@booboopdb1 as sysdba;
CREATE TABLE SCOTT.test
( EMPNO NUMBER(4,0),
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4,0),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2,0)
)
TABLESPACE example;
insert into scott.test select * from scott.emp;
insert into scott.test select * from scott.test;
--重复上一条16次
SYS@booboopdb1>select count(*) from scott.test;
COUNT(*)
----------
2228224使用EM,查看关于EXAMPLE表空间的信息,并回答给出的问题。
问题1:EXAMPLE表空间几乎满了。在不采取任何操作的情况下,表空间的大小是否可以增加
不会,因为创建时,我选择了不自动扩展。
问题2:EXAMPLE表空间中有多少段?
YS@booboopdb1>select tablespace_name,count(*) from dba_segments where tablespace_name like '%EXAMPLE%' group by tablespace_name; |
问题3:EXAMPLE表空间中,哪个索引占用了最多的空间?
SYS@booboopdb1>create index idx_empno on scott.test (empno) TABLESPACE example; |
查询结果
SYS@booboopdb1>exec print_table(q'[select tablespace_name,segment_name ,sum(bytes)/1024/1024 from dba_segments where segment_type ='INDEX' and tablespace_name='EXAMPLE' group by tablespace_name,segment_name]') |
KnowledgePoint
表占用空间: |
实践7-2:创建一个表空间
Practice 7-2: Creating a Tablespace
Overview
In this practice, you create a script to create the INVENTORY tablespace that will be used in a later practice.
Task
- Create a new locally managed tablespace (LMT) called INVENTORY of size 5 MB
- Execute the P7script.sql script you just created.
- As the DBA1 user, run the lab_07_02_03.sql script to create and populate a table (called X) in the INVENTORY tablespace. What error do you eventually see?
- In Cloud Control, go to the Tablespaces page for the orcl database and increase the amount of space available for the INVENTORY tablespace. For educational purposes, you will accomplish this by using two different methods. First, increase the size of the current data file to 40 MB. Then, add a second data file by using file system storage. This second data file should be 30 MB in size. For both techniques, use the show SQL functionality to view the supporting SQL statements.
- Log out of Enterprise Manager Cloud Control.
- Go back to the terminal window and run the lab_07_02_03.sql script again. It drops the table and re-executes the original script that previously returned the space error.
- In a terminal window, run the $LABS/P7/lab_07_02_06.sql script in SQLPlus as the *dba1 user to clean up the tablespace for later practice sessions.
Practice
在这个实践中,您将通过创建一个脚本来创建INVENTORY表空间,该脚本将在以后的实践中使用。
创建一个新的本地管理表空间(LMT),名为
INVENTORY
大小5MB
。cat > /home/oracle/labs/P7script.sql << ENDF
CREATE SMALLFILE TABLESPACE "INVENTORY"
DATAFILE
'/u01/app/oracle/oradata/booboo/booboopdb1/inventory01.dbf' SIZE 5M AUTOEXTEND OFF
LOGGING
DEFAULT NOCOMPRESS NO INMEMORY
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT AUTO;
ENDF创建一个名为
P7script.sql
的脚本文件,并执行。sqlplus dba1/oracle_4U@booboopdb1 as sysdba @/home/oracle/labs/P7script.sql
使用
DBA1
用户登陆并运行lab_07_02_03.sql
,在INVENTORY
表空间中创建和填充表X
,你将看到什么报错?sqlplus dba1/oracle_4U@booboopdb1 as sysdba @/home/oracle/labs/P7/lab_07_02_03.sql
运行结果:
[oracle@oracle01 labs]$ sqlplus dba1/oracle_4U@booboopdb1 as sysdba @/home/oracle/labs/P7/lab_07_02_03.sql
SQL*Plus: Release 12.2.0.1.0 Production on Sat Feb 1 00:34:47 2020
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
PL/SQL procedure successfully completed.
SYS@booboopdb1>CREATE TABLE x
2 (a CHAR(1000)
3 ) TABLESPACE inventory;
Table created.
SYS@booboopdb1>INSERT INTO x
2 VALUES ('a');
1 row created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
1 row created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
2 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
4 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
8 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
16 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
32 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
64 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
128 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
256 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
512 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
1024 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
INSERT INTO x
*
ERROR at line 1:
ORA-01653: unable to extend table SYS.X by 128 in tablespace INVENTORY
SYS@booboopdb1>COMMIT;
Commit complete.
SYS@booboopdb1>quit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
您将使用两种不同的方法来完成此任务。1)将当前数据文件的大小增加到
40MB
;2)使用添加第二个大小为30MB
的数据文件。--将当前数据文件的大小增加到`40MB`
ALTER DATABASE DATAFILE '/u01/app/oracle/oradata/booboo/booboopdb1/inventory01.dbf' RESIZE 40M;
--使用添加第二个大小为`30MB`的数据文件
ALTER TABLESPACE "INVENTORY" ADD DATAFILE '/u01/app/oracle/oradata/booboo/booboopdb1/inventory02.dbf' SIZE 30M AUTOEXTEND ON NEXT 100M;继续运行
lab_07_02_03.sql
,在INVENTORY
表空间中创建和填充表X
。[oracle@oracle01 labs]$ sqlplus dba1/oracle_4U@booboopdb1 as sysdba @/home/oracle/labs/P7/lab_07_02_03.sql
SQL*Plus: Release 12.2.0.1.0 Production on Sat Feb 1 00:37:42 2020
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
PL/SQL procedure successfully completed.
SYS@booboopdb1>CREATE TABLE x
2 (a CHAR(1000)
3 ) TABLESPACE inventory;
Table created.
SYS@booboopdb1>INSERT INTO x
2 VALUES ('a');
1 row created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
1 row created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
2 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x;
4 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
8 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
16 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
32 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
64 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
128 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
256 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
512 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
1024 rows created.
SYS@booboopdb1>INSERT INTO x
2 SELECT * FROM x ;
2048 rows created.
SYS@booboopdb1>COMMIT;
Commit complete.
SYS@booboopdb1>quit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
使用
DBA1
用户登陆并运行$LABS/P7/lab_07_02_06.sql
来清理表空间,方便以后的练习。[oracle@oracle01 labs]$ sqlplus dba1/oracle_4U@booboopdb1 as sysdba @/home/oracle/labs/P7/lab_07_02_06.sql
SQL*Plus: Release 12.2.0.1.0 Production on Sat Feb 1 00:39:28 2020
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Table dropped.
SYS@booboopdb1>exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
KnowledgePoint
表空间是将相关逻辑结构分组在一起的数据库存储单元。数据库数据文件存储在表空间中。
- 管理表空间的
准则您可以遵循有关使用表空间的准则。 - 创建表空间
您可以创建一个表空间来将相关的逻辑结构(例如表和索引)组合在一起。数据库数据文件存储在表空间中。 - 考虑将表空间存储在内存中列存储中
您可以在表空间创建过程中或通过更改表空间来为内存中列存储启用表空间。当这为内存中列存储启用表空间时,默认情况下为该内存中列存储启用表空间中的所有表。 - 为表空间指定非标准块大小
您可以创建表空间,其表块大小不同于由DB_BLOCK_SIZE
初始化参数指定的标准数据库块大小。此功能使您可以在数据库之间传输具有不同块大小的表空间。 - 控制重做记录的写入
对于某些数据库操作,您可以控制数据库是否生成重做记录。 - 更改表空间可用性
您可以使联机表空间脱机,以使其暂时不可用以供一般使用。数据库的其余部分保持打开状态,可供用户访问数据。相反,您可以使脱机表空间联机,以使表空间中的模式对象可供数据库用户使用。必须打开数据库才能更改表空间的可用性。 - 使用只读表
空间可以将表空间置于只读模式。这样可以防止存储在其中的任何数据被更新。 - 更改和维护表空间
您可以通过执行诸如添加数据文件和临时文件之类的任务来更改和维护表空间。 - 重命名表空间
使用的RENAME TO
子句ALTER TABLESPACE
,您可以重命名永久或临时表空间。 - 删除表空间
如果不再需要表空间及其内容,则可以从数据库中删除表空间及其内容(表空间中包含的段)。 - 管理SYSAUX表空间创建数据库时
,SYSAUX
表空间已作为表空间的辅助表空间安装SYSTEM
。以前创建并使用单独的表空间的某些数据库组件现在占用该SYSAUX
表空间。 - 纠正本地管理的表空间的问题
Oracle数据库包括用于纠正本地管理的表空间的问题的辅助工具。 - 将SYSTEM表空间迁移到本地管理的表空间
使用此DBMS_SPACE_ADMIN.TABLESPACE_MIGRATE_TO_LOCAL
过程将SYSTEM
表空间从字典管理的迁移到本地管理的表。 - 查看有关表空间的信息
Oracle数据库包含数据字典视图,您可以查询有关表空间的信息。