1. 程式人生 > 其它 >3、oracle表空間及索引操作

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;

 

轉自:https://www.cnblogs.com/LiuChang-blog/p/12315505.html