今天我们主要讲解范围(range)分区,分区可以很好的提高效率。如果一个表的分区(片段)位于不同的设备上,吞吐量就会大大增加。分区还可以被独立地备份和恢复(即便它们正在使用中),这样可以减少备份期间可能出现的磁盘I/O问题。仅仅当分区被正确实现后,才能体现Oracle性能提高的良好特点。理解分区的最好方法就是看一个例子。根据deptno列,dept表被分成了3个分区(片段)。
#su - oracle
#sqlplus / as sysdba
SQL> startup
SQL> conn scott/tiger
ERROR:
ORA-28000: the account is locked
Warning: You are no longer connected to ORACLE.
SQL> conn system/tiger
SQL> alter user scott account unlock identified by tiger;
SQL> conn scott/tiger
SQL> select * from tab;
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
drop table dept
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
------------------------------ ------- ----------
EMP TABLE
BONUS TABLE
SALGRADE TABLE
BIN$sV3WqBHABH3gRAAMKXL6gA==$0 TABLE
DEPTBAK TABLE
------------------------------ ------- ----------
EMP TABLE
BONUS TABLE
SALGRADE TABLE
DEPTBAK TABLE
# mkdir -p /opt/oracle/oracle/product/10.2.0/scott/tablespace/
# sqlplus /nolog
SQL> conn system/tiger
SQL> create tablespace dept1 datafile '/opt/oracle/oracle/product/10.2.0/scott/tablespace/dept1.dbf' size 500M;
SQL> create tablespace dept2 datafile '/opt/oracle/oracle/product/10.2.0/scott/tablespace/dept2.dbf' size 500M;
SQL> create tablespace dept3 datafile '/opt/oracle/oracle/product/10.2.0/scott/tablespace/dept3.dbf' size 500M;
SQL> conn scott/tiger
--使用3个分区创建表dept
SQL>create table dept
(
deptno number(2),
dept_name varchar2(30))
partition by range (deptno)
(partition d1 values less than (10) tablespace dept1,
partition d2 values less than (20) tablespace dept2,
partition d3 values less than (maxvalue) tablespace dept3);
--把数据添加到表的3个分区上:
SQL> insert into dept values (1,'ADMIN');
SQL>insert into dept values (7,'MGMT');
SQL>insert into dept values (10,'MANUF');
SQL>insert into dept values (15,'ACCT');
SQL>insert into dept values (22,'SALES');
--我们只从一个分区中选取数据并且只访问一个分区:
SQL> select * from dept partition (d1);
---------- ------------------------------
1 ADMIN
7 MGMT
---------- ------------------------------
10 MANUF
15 ACCT
---------- ------------------------------
22 SALES
---------- ------------------------------
22 SALES
SQL> select table_name, partitioned from user_tables where table_name='DEPT';
------------------------------ ---
DEPT YES
SQL> select table_name, partition_count from user_part_tables where table_name = 'DEPT';
------------------------------ ---------------
DEPT 3
SQL> select segment_name, partition_name, segment_type, tablespace_name from user_segments;
--------------------------------------------------------------------------------
PARTITION_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
DEPTBAK
TABLE USERS
TABLE USERS
TABLE USERS
SEGMENT_NAME
--------------------------------------------------------------------------------
PARTITION_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
PK_EMP
INDEX USERS
TABLE USERS
D1 TABLE PARTITION DEPT1
SEGMENT_NAME
--------------------------------------------------------------------------------
PARTITION_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
DEPT
D2 TABLE PARTITION DEPT2
D3 TABLE PARTITION DEPT3