Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

时间:2022-06-06 23:41:50
摘要:Oracle数据库的库表常用操作:创建与添加表空间、临时表空间、创建表分区、创建索引、锁表处理

1.表空间

■  详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间

--详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间
select t.*
from (SELECT D.TABLESPACE_NAME,
SPACE "SUM_SPACE(M)",
BLOCKS SUM_BLOCKS,
SPACE - NVL(FREE_SPACE, 0) "USED_SPACE(M)",
ROUND((1 - NVL(FREE_SPACE, 0) / SPACE) * 100, 2) "USED_RATE(%)",
FREE_SPACE "FREE_SPACE(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE,
SUM(BLOCKS) BLOCKS
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) D,
(SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) FREE_SPACE
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
UNION ALL --if have tempfile
SELECT D.TABLESPACE_NAME,
SPACE "SUM_SPACE(M)",
BLOCKS SUM_BLOCKS,
USED_SPACE "USED_SPACE(M)",
ROUND(NVL(USED_SPACE, 0) / SPACE * 100, 2) "USED_RATE(%)",
SPACE - USED_SPACE "FREE_SPACE(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE,
SUM(BLOCKS) BLOCKS
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) D,
(SELECT TABLESPACE,
ROUND(SUM(BLOCKS * 8192) / (1024 * 1024), 2) USED_SPACE
FROM V$SORT_USAGE
GROUP BY TABLESPACE) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE(+)) t
order by "USED_RATE(%)" desc;

Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

■  查看当前表空间数据文件序号

select  * from dba_data_files where tablespace_name='TblSpaceName';

■  添加表/索引空间,数据文件大小固定为30720M

alter tablespace TblSpaceName add datafile 'DATA/orcl/datafile/new_data11.dbf' size  30720M autoextend off;
alter tablespace TblSpaceName add datafile 'DATA/orcl/datafile/new_data12.dbf' size 30720M autoextend off;

■  删除表空间的某个数据文件

alter tablespace TblSpaceName drop datafile 'DATA/orcl/datafile/new_data11.dbf';

2.临时表空间

临时表空间用来管理数据库排序操作以及用于存储临时表、中间排序结果等临时对象,当ORACLE里需要用到SORT的时候,并且当PGA中sort_area_size大小不够时,将会把数据放入临时表空间里进行排序。像数据库中一些操作: CREATE INDEX、 ANALYZE、SELECT DISTINCT、ORDER BY、GROUP BY、 UNION ALL、 INTERSECT、MINUS、SORT-MERGE JOINS、HASH JOIN等都可能会用到临时表空间。

■  查看实例的临时表空间

SELECT PROPERTY_NAME, PROPERTY_VALUE FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE'

■  创建临时表空间

CREATE TEMPORARY TABLESPACE TMP  TEMPFILE '/u01/gsp/oradata/TMP01.dbf' SIZE 8G AUTOEXTEND OFF;

■  增加数据文件

ALTER  TABLESPACE TMP ADD TEMPFILE '/u03/eps/oradata/temp02.dbf' SIZE 64G

■  删除临时表空间的数据文件

ALTER TABLESPACE TEMP DROP TEMPFILE '/u01/app/oracle/oradata/GSP/temp02.dbf';

■  调整文件大小

ALTER DATABASE TEMPFILE  '/u01/app/oracle/oradata/GSP/temp02.dbf' RESIZE 2G;

■  删除临时表空间

DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;

3.表分区

■  查看某个表分区及行数;查看所有表分区时间范围;

--查看某个表分区及行数
select t.table_name,t.partition_name,changeintegertodate(substr(t.partition_name,3,length(t.partition_name)-2)),t.num_rows,t.tablespace_name
from user_tab_partitions t
where t.table_name =upper('t_table1'); --查看所有表分区时间范围
select t.table_name,changeintegertodate(substr(mintime,3,length(mintime)-2)),changeintegertodate(substr(maxtime,3,length(maxtime)-2))
from (select table_name,min(partition_name) mintime,max(partition_name) maxtime from user_tab_partitions group by table_name) t
order by maxtime

这里用到时间转换函数 changeintegertodate:

create or replace function changeIntegerToDate(dateInteger in int) return varchar2
is
returnDate varchar2(20);
begin
select to_char( TO_DATE('1970-1-1 08:00:00','YYYY-MM-DD hh24:mi:ss')+(dateInteger/(3600*24)),'YYYY-MM-DD hh24:mi:ss') into returnDate FROM DUAL;
return returnDate;
end changeIntegerToDate;

■  创建分区表:

CREATE TABLE ▪▪▪
▪▪▪▪▪▪
TABLESPACE "TblName"
PARTITION BY RANGE ("TIME")
(PARTITION "P_1465747200" VALUES LESS THAN (1465747200)
PCTFREE 0 PCTUSED 40 INITRANS 1 MAXTRANS 255
STORAGE(INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TBL_MRODATA" COMPRESS BASIC );

■  添加表分区

ALTER TABLE  TblName ADD PARTITION  "P_1465747200" VALUES LESS THAN (1465747200)    TABLESPACE TBLSpace;

4.索引

■  查询表是否有索引

--查询表是否有索引
select index_name,column_name from user_ind_columns where table_name='TEMP';

■  创建索引

--创建索引
create index temp_id_i on temp(id);

5.锁表

■  锁表查询的代码有以下的形式:

select count(*) from v$locked_object;
select * from v$locked_object;

■  查看哪个表被锁

select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;

■  查看是哪个session引起的

select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time; 

■  杀掉对应进程

执行命令:alter system kill session'1025,41';
其中1025为sid,41为serial#.