3、oracle表空間及索引操作



3.1、創建表空間和用戶授權:

1、創建表空間:

CREATE TABLESPACE <表空間名> LOGGING DATAFILE '<存放路徑>' SIZE 50M AUTOEXTEND ON NEXT 50M MAXSIZE 31768M EXTENT MANAGEMENT LOCAL;

#windows存放路徑:D:\app\Administrator\oradata\orcl\lc_data.dbf

#linux存放路徑:/application/oracle/oradata/orcl/lc_data.dbf

2、創建用戶並指定表空間:

CREATE USER <用戶名> IDENTIFIED BY <密碼> DEFAULT TABLESPACE <表空間名>;

#一個用戶只有一個表空間,而表空間可以有多個用戶;

3、給用戶授予權限:

grant connect,resource,dba to <用戶名>;

3.2、刪除表空間:

1、刪除用戶:

drop user <用戶名> cascade;

2、刪除表空間:

drop tablespace <表空間名> including contents and datafiles cascade constraints;

#including contents:刪除表空間中的內容,如果刪除表空間之前表空間中有內容,而未加此參數,表空間無法刪除;

#including datafiles:刪除表空間中的數據文件;

#cascade constraints:刪除表空間中表的外鍵參照;

3.3、表空間查詢操作:

1、查詢所有表空間及對應的路徑:

select tablespace_name,file_name from dba_data_files;

2、查詢所有表空間的狀態信息;

select tablespace_name,status from dba_tablespaces;

3、增加表空間大小:

ALTER TABLESPACE <表空間名> ADD DATAFILE '<存放路徑>' SIZE 50M AUTOEXTEND ON NEXT 50M MAXSIZE UNLIMITED;

4、查看所有用戶以及對應的表空間:

select username,default_tablespace from dba_users;

5、查看當前用戶的缺省表空間:

select username,default_tablespace from user_users;

6、表空間之間的鏈接:

(1)創建表空間之間的鏈接:

create database link TO_<表空間名>_LINK connect to <用戶名> identified by <密碼> using '<數據庫實例名>';

(2)查詢表空間之間的鏈接:

select * from <當前表空間名>@TO_<表空間名>_LINK;

(3)刪除表空間之間的鏈接:

drop database link TO_<表空間名>_LINK;

7、查詢所有表空間的用量:

select tablespace_name,count(*) AS extends,round(sum(bytes)/1024/1024,2) AS MB,sum(blocks) AS blocks from dba_free_space group BY tablespace_name;

8、查看序列號,last_number是當前值:

select * from user_sequences;

9、修改表空間屬性(離線):

alter tablespace <表空間名> offline;

10、修改表空間屬性(在線):

alter tablespace <表空間名> online;

11、修改表空間屬性(只讀):

alter tablespace <表空間名> read only;

12、修改表空間屬性(讀寫)

alter tablespace <表空間名> read write;

13、修改session的時間格式:

alter session set nls_date_format='yyyy-mm-dd';

3.4、索引操作:

1、創建單一索引:

create index <索引名> on <表名>(<列名1>);

2、創建組合索引:

create index <索引名> on <表名>(<列名1>,<列名2>);

select * from <表名> where <列名1>='<字符>'

#走索引

select * from <表名> where <列名2>='<字符>'

#不走索引

select * from <表名> where <列名1> like '%<字符>%'

#不走索引

select * from <表名> where <列名1>='<字符>' and <列名2>='<字符2>'

#走索引

select * from <表名> where <列名1>='<字符>' or <列名2>='<字符2>'

#不走索引

drop index <索引名稱>;

#刪除索引

3、查看索引的方法:

(1)在當前用戶中查找表名:

select * from user_tables where table_name like '<表名>%';

(2)查詢該表的所有索引:

select * from user_indexes where table_name='<表名>';

(3)查詢該表的所有索引列:

select * from user_ind_columns where table_name='<表名>';

(4)查詢當前用戶所有表的索引和索引類別:

select table_name,index_name,index_type from user_indexes order by index_name;

(5)查看當前用戶下指定索引的信息:

select * from user_indexes where index_name=upper('&index_name');

(6)查看當前用戶下指定的索引的索引列:

select * from user_ind_columns where index_name=upper('&index_name');

(7)查看當前用戶下指定索引的大小:

select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name');

(8)補充:

user_indexes #存放着當前用戶所有表的索引信息;

user_segments #存放着當前用戶所有表的索引大小;

user_ind_columns #存放着當前用戶所有表的索引列信息;

4、索引補充:

(1)哪些列適合建索引:

1)經常出現在where子句的列;

2)經常用於表連接的列,在匹配表上進行建索引;

3)該列是高基數數據列,高基數數據列是指有很多不同的值;

4)索引里面不計null值;

5)表很大,查詢結果集小;

6)在pk、uk、fk鍵上建立索引;

7)經常需要排序"order by"和分組"group by"的列;

(2)索引用不了的寫法:

1)函數導致索引用不了 where upper(colname)= 'char';

2)可以對函數建索引:

create index <索引名> on <表名>(round(<列名1>));

3)表達式導致索引用不了 where colname*12=1200;

4)索引不是萬能的;

(3)索引結構:

1)分析索引結構有效性:

analyze index <索引> validate structure;

一般來講默認的方式是offline;

當以offline的模式analyze索引時,會對table加一個表級共享鎖,對目前table的一些實時DMl操作會產生一定的影響;

而以online模式分析時候,則不會加任何lock,但在index_stats中是看不到任何信息的;

2)查看索引結構:

select NAME,HEIGHT,BLOCKS,BR_BLKS,BR_ROWS,LF_BLKS,LF_ROWS from index_stats;

3)合並索引葉級塊碎片:

alter index <索引名> coalesce;

4)重建索引:

alter index <索引名> rebuild;







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM