表空间表分区

来源:互联网 发布:国外商城cms排行 编辑:程序博客网 时间:2024/04/29 16:24

表空间 表分区

1、进入DOS,启动sqlplus "/ as sysdba" 

2shutdown immediate  关闭数据库

3startup mount 启动数据库到装载状态

4alter database archivelog; 进入归档模式

5alter database open; 打开数据库

也可以修改初始化参数实现归档模式

 log_archive_start = true

去除归档模式,要先关闭数据库,再用指令

alter database noarchivelog;  -- 进入非归档模式

select status from v$instance; -- 查数据库状态

select log_mode from v$database; -- 查看数据库模式

表空间:

 创建表空间:

    需要用拥有sys权限的 用户:    

       create tablespace test_xm datafile 'c:\temp\test_xm.dbf' size 2M;

 扩展表空间:

   1,添加数据文件:

       alter tablespace test_xm add datafile 'd:\test\xm2.dbf' size 3M;

   

   2, 改变数据文件的大小:

       alter database datafile 'c:\temp\test_xm.dbf' resize 4M;

   

   3, 允许数据文件自动扩展:

      alter database datafile 'c:\temp\test_xm.dbf' autoextend on next 1 maxsize 20M;

 删除表空间

      drop tablespace test_xm including contents and datafiles;

     如果没有and datafiles 则不会删除 物理文件

表分区:

  分区的优点:1、增强可用性:如果表的某个分区出现故障,其他分区还可以用;

              2、 维护方便:表的某个分区出现故障,只要修复该分区即可;

              3、 平衡I/O流;可以把不同的分区68映射到不同的磁盘以平衡I/O流。  

                  改善系统的性能;

              4、改善查询功能:对分区对象进行查询时,只要搜索自己关心的分区,提高检索速度;

 1. 分区前的准备:

      分区必须要在不同的表空间进行。

      创建三个表空间:

           create tablespace test_1 datafile 'd:\temp\test_1.dbf' size 3M;

           create tablespace test_2 datafile 'd:\temp\test_2.dbf' size 3M;

           create tablespace test_3 datafile 'd:\temp\test_3.dbf' size 3M;

 2. 分区方法:

       范围分区 、 HASH分区 、列表分区、复合分区

    一、 范围分区:

            范围分区就是对数据表中的某个值的范围进行分区。

            根据某个值的范围,决定将该数据存储在哪个分区上  

           根据fid分区: 

            create table test_xm (

                 fid number not null primary key,

                 fname varchar2(10),

                 fsex varchar2(1),

                 fdata data

               )

               partition by range(fid)(

                   partition part_01 values less than(2000000) tablespace test_1,

                   partition part_02 values less than(4000000)  tablespace test_2,

                   partition part_03 values less than(maxvalues) tablespace test3

                )

   

    二、 HASH分区 

          create table test_xm (

                 fid number not null primary key,

                 fname varchar2(10),

                 fsex varchar2(1),

                 fdata data

               )

               partition by hash(fid)(

                 partition part_01 tablespace test_1,

                 partition part_02 tablespace test_2,

                 partition part_03 tablespace test_3

                )

          

     三、 列表分区:

create table test_xm (

                 fid number not null primary key,

                 fname varchar2(10),

                 fsex varchar2(1),

                 fdata data

               )

               partition by listcity(

  partition  part1  value(‘北京’)  tablespace test1 ,

partition  part2  value(‘上海’)   tablespace test 2,

partition  part3  value(default)   tablespace test3

)

        

 3. 分区表的操作:

      insert updatedeleteselect

    1> insert

        insert into test_xm values(1,'joke','M','to_data('1985-05-7','yyyy-mm-dd')');

        insert into test_xm values(2,'lucy','F','to_data('1986-06-3','yyyy-mm-dd')');

        insert into test_xm values(3,'nancy','F','to_data('1984-06-3','yyyy-mm-dd')');     

        insert into test_xm values(4,'king','M','to_data('1986-08-5','yyyy-mm-dd')');

    2> select 

        select * from test_xm partition(part_01);

        select * from test_xm partition(part_02);

        select * form test_xm partition(part_03);

        select * form test_xm ;

    3>  update

         update test_xm partition(part_01) t

          set t.fname='simith' where fid=2;

        commit;

     4> delete

         delete from test_xm partition(part_01) t

          where t.fid=3;

         commit;

            

分区索引:

     分区表和一般表一样可以建立索引,

     分区表可以创建局部索引和全局索引.

    

    create index idx_test_xm on test_xm(fid)

    local(

          partition idx_1 tablespace test_1,

          partition idx_2 tablespace test_2,

          partition idx_3 tablespace test_3

         );

     commit;



原创粉丝点击