❶ oracle如何把一个表指定到表空间里面。(类似于biao1,和biaokongjian1)脚本怎么写,一个最简单的就行谢
只能在建表的时候指定表所在的表空间,建好以后修改是不可以了,DROP掉,重新建表了。
❷ oracle 数据库 表空间
1、sys和system都是DBA用户,sys用户的权限比system高一些。数据库的手动启动和关闭需要登入sys用户才行。sys用户可以操作系统认证(不需要口令),数据字典的所有者。
2、创建表空间:先按sys或system登入sql*plus,执行下列语句。
CREATE TABLESPACE TEST123
DATAFILE D:\oracle\oradata\TEST123.DBF' SIZE 1G REUSE
AUTOEXTEND ON NEXT 512M MAXSIZE UNLIMITED
AUTOALLOCATE EXTENT MANAGEMENT LOCAL NOLOGGING;
不要一下子创建5000M文件,先创建1G,然后让它自动扩展。
3、创建用户:
CREATE USER TEST123 IDENTIFIED BY test123
DEFAULT TABLESPACE TEST123 Temporary Tablespace temp;
4、授权:
Grant connect,resource to TEST123;
5、查询表空间及数据文件:
Select * from dba_tablespaces; --查看表空间
Select * from dba_data_files; --查看数据文件
❸ 如何检测Oracle的可用性和表空间容量
除了Oracle的激活和可用性之外,我们还需要进行检测以确保它可以用,这样我们还可以检测表空间的容量 。
检测的脚本:
◆假设所有的Oracle环境都已经搭建起来了
◆假设所有的扩展都已经达到了最大的限度
◆假设左右的表空间都缺乏运行的空闲空间
下面的脚本可以在你想要的任何时候通过crontab 来中断 。另外,如果上面的例外情况出现了的话,您还可以就这个脚本写信或者电子邮件给支持人员获得帮助 。
如果您有什么其他的测试,这个脚本可以让您轻松地进行修改以加以利用 。我使用这个Monitororcl 脚本作为模板并且在末尾添加了功能 。
按crontab来调用query_oracle_instances.sh 脚本:
#!/bin/ksh
. /u01/home/oracle/.profile
/u01/app/oracle/admin/monitororcl
cat /u01/app/oracle/admin/Get_Oracle_Instance_Listexit
Get_Oracle_Instance_List 脚本如下:
instance_name1 tnsname1 sys_password_for_this_instanceinstance_name2 tnsname2 sys_password_for_this_instanceinstance_name3 tnsname3 sys_password_for_this_instance下面是MONITORORCL脚本:
#!/bin/ksh
#script : Rick Stehno
# script will monitor to see if Oracle is upwhile [ "$1" != "" ]
do
ORACLE_INSTANCE=$1
ORACLE_TNS=$2
USR_ID=sys
USR_PASS=$3
# echo "Instance: [$ORACLE_INSTANCE]"
# echo "TNS [$ORACLE_TNS]"
# echo "PASS: [$USR_PASS]"
LOGFIL=/u01/app/oracle/admin/[email protected],userid2,[email protected]#
# 检测关键的段没有达到最大限度
sqlplus -s <$LOGFIL 2>/dev/null
$USR_ID/$USR_PASS@$ORACLE_TNS
set pages 0
select distinct YES from dba_segments
where extents >= (max_extents-5) and segment_name not like 1.%;EOF1
grep -i ^ORA- $LOGFIL >/dev/null
if [ $? -eq 0 ]
then
echo "$0 failed: check $ORACLE_INSTANCE for problems" | /bin/mailx -s "${ORACLE_INSTANCE} : Script failed" $NOTIFY_LISTexit 1
fi
MAXEXTENTS_REACHED=`awk { print $1 } $LOGFIL`if [ "$MAXEXTENTS_REACHED" = "YES" ]
then
echo "$0 failed: $ORACLE_INSTANCE max extents reached" | /bin/mailx -s "${ORACLE_INSTANCE} : max extents reached" $NOTIFY_LISTexit 1
fi
#
# 检测是否能分配下一个段
sqlplus -s <$LOGFIL 2>/dev/null
$USR_ID/$USR_PASS@$ORACLE_TNS
set pages 0
select distinct YES from dba_segments ds
where next_extent >
(select max(bytes) from dba_free_space
where tablespace_name = ds.tablespace_name);EOF2
grep -i ^ORA- $LOGFIL >/dev/null
if [ $? -eq 0 ]
then
echo "$0 failed: check $ORACLE_INSTANCE for problems" | /bin/mailx -s "${ORACLE_INSTANCE} : Script failed" $NOTIFY_LISTexit 1
fi
POSSIBLE_NEXTEXT_FAIL=`awk {print $1 } $LOGFIL`if [ "$POSSIBLE_NEXTEXT_FAIL" = "YES" ]
then
echo "$0 failed: $ORACLE_INSTANCE cannot extend segment" | /bin/mailx -s "${ORACLE_INSTANCE} : max extents reached" $NOTIFY_LISTexit 1
fi
shift 3
# echo "shift done"
done
echo "Successful completion of $0" `date`exit 0
❹ Oracle表空间容量评估
1. 推荐一个使用平均值估算表空间的脚本:
--不适用windows
with t1 as (
select ss.run_time,ts.name,
decode((round(su.tablespace_usedsize*dt.block_size/1024/1024,2)),null,0,(round(su.tablespace_usedsize*dt.block_size/1024/1024,2))) used_size_mb
from
dba_hist_tbspc_space_usage su,
(select trunc(BEGIN_INTERVAL_TIME) run_time,max(snap_id) snap_id from dba_hist_snapshot
group by trunc(BEGIN_INTERVAL_TIME) ) ss,
v$tablespace ts,
dba_tablespaces dt
where su.snap_id = ss.snap_id
and su.tablespace_id = ts.ts#
and ts.name NOT LIKE '%TEMP%'
and ts.name NOT LIKE '%UNDO%'
and ts.name = dt.tablespace_name order by 2,1),
t2 as (
select e.run_time,e.name,e.used_size_mb,e.used_size_mb - b.used_size_mb growth
from t1 e, t1 b
where e.name = b.name and e.run_time = b.run_time +1),
t5 as (select a.TABLESPACE_NAME,
a.FILE_NAME,
a.FILE_ID,
a.BYTES,
a.AUTOEXTENSIBLE,
a.ONLINE_STATUS,
a.MAXBYTES,
case
when a.AUTOEXTENSIBLE = 'YES' and
a.ONLINE_STATUS not in ('OFFLINE', 'SYSOFF') then
nvl(a.MAXBYTES, 0)
else
nvl(a.BYTES, 0)
end file_max_size
from dba_data_files a
where a.tablespace_name NOT LIKE '%TEMP%'
and a.tablespace_name NOT LIKE '%UNDO%'
),
t3 as (
select tsz.tablespace_name,
tsz.alloc_size_mb,ave.avg_growth_per_day_mb,ave.avg_growth_per_day_mb*90 projected_growth_for_3mths_mb
from
(select tablespace_name, round(sum(file_max_size)/1024/1024,2) alloc_size_mb from t5 group by tablespace_name) tsz,
(select name,decode(round(avg(growth),2),null,0.11,0,0.11, round(avg(growth),2)) avg_growth_per_day_mb from t2 group by name) ave
where tsz.tablespace_name = ave.name),
t6 as (select
d.tablespace_name tablespace_name,
round((d.sumbytes/1024/1024),2) total_g ,
round(decode(f.sumbytes,null,0,f.sumbytes)/1024/1024,2) free,
round(((d.sumbytes-f.sumbytes)/1024/1024),6) size_could_be_used,
round((d.sumbytes-decode(f.sumbytes,null,0,f.sumbytes))*100/d.sumbytes,2) used_pct,
(100-round((d.sumbytes-decode(f.sumbytes,null,0,f.sumbytes))*100/d.sumbytes,2))*round((d.sumbytes/1024/1024),2) real_free
from
(select
tablespace_name, sum(bytes) sumbytes
from dba_free_space group by tablespace_name) f,
(select tablespace_name, sum(bytes) sumbytes
from dba_data_files group by tablespace_name) d
where f.tablespace_name(+) = d.tablespace_name)
select t4.tablespace_name,decode(t3.alloc_size_mb,null,0,t3.alloc_size_mb) alloc_sz_mb,
--t6.real_free/round(decode(avg_growth_per_day_mb,null,365,0,365,(t3.avg_growth_per_day_mb)),2) Days_To_Be_Used,
((100-decode(round(t6.size_could_be_used*100/t3.alloc_size_mb,2),null,0,round(t6.size_could_be_used*100/t3.alloc_size_mb,2)))/100*t3.alloc_size_mb)/avg_growth_per_day_mb Days_To_Be_Used,
round(t6.size_could_be_used*100/t3.alloc_size_mb,4) used_pct_auto,
t6.used_pct used_pct_real
from t3,t6,
(select a.tablespace_name,
round(a.bytes/1024/1024/1024,2) alloc,
round(c.bytes/1024/1024/1024,2) free
from sys.sm$ts_avail a,
sys.sm$ts_free c
where a.tablespace_name = c.tablespace_name(+)
and a.tablespace_name NOT LIKE '%TEMP%'
and a.tablespace_name NOT LIKE '%UNDO%'
) t4
where t4.tablespace_name = t3.tablespace_name(+)
and t4.tablespace_name = t6.tablespace_name(+)
--and ((100-decode(round(t6.size_could_be_used*100/t3.alloc_size_mb,2),null,0,round(t6.size_could_be_used*100/t3.alloc_size_mb,2)))/100*t3.alloc_size_mb)/avg_growth_per_day_mb <=30
and ((100-decode(round(t6.size_could_be_used*100/t3.alloc_size_mb,2),null,0,round(t6.size_could_be_used*100/t3.alloc_size_mb,2)))/100*t3.alloc_size_mb)/avg_growth_per_day_mb>=0
order by 1;
2. 通过线性回归参数预测未来使用量(待补充):
❺ 如何解决oracle表空间满的问题
Oracle的表空间本来就是自动增长的,你何苦不用稳定的子增长设置,自己来设计方案。
还有过期不用的数据归档到备份数据库,这样表空间就够用了。
❻ 怎么写一个脚本检查oracle数据库表空间,如果利用率超过80%,就执行一条加表空间的SQL
您好
SELECT UPPER(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
F.TOTAL_BYTES "空闲空间(M)",
F.MAX_BYTES "最大块(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1;
❼ Oracle能否用SQL语言来检测某个表空间是否已经存在
oracle查询已有的表空间名语句为:
selectTABLESPACE_NAMEfromdba_tablespaces;
查询表空间使用情况语句为:
SELECTSUM(bytes)/(1024*1024)ASfree_space,tablespace_name
FROMdba_free_space
GROUPBYtablespace_name;
查看表空间物理文件的名称及大小
SELECTtablespace_name,
file_id,
file_name,
round(bytes/(1024*1024),0)total_space
FROMdba_data_files
ORDERBYtablespace_name;
❽ Oracle表空间管理脚本
自己平时用的,看看吧
.查看所有表空间的碎片程度(值在30以下表示碎片很多)
select tablespace_name,sum(bytes),sum(free),sum(free)*100/sum(bytes) from (select
b.file_id file_ID,
b.tablespace_name tablespace_name,
b.bytes Bytes,
(b.bytes-sum(nvl(a.bytes,0))) used,
sum(nvl(a.bytes,0)) free,
sum(nvl(a.bytes,0))/(b.bytes)*100 Percent
from dba_free_space a,dba_data_files b
where a.file_id=b.file_id
group by b.tablespace_name,b.file_id,b.bytes
order by b.file_id) group by tablespace_name order by sum(free)*100/sum(bytes);
查看表空间是否自动扩展
select file_name,autoextensible,increment_by from dba_data_files;
表空间自动扩展
alter database datafile 'path:\datafile name' autoextend on next 1M maxsize 100M;
表空间大小
select tablespace_name,count(*),sum(blocks),sum(bytes)/1024/1024
from dba_data_files
group by tablespace_name;
使用情况
select df.tablespace_name "表空间名",totalspace "总空间M",freespace "剩余空间M",round((1-freespace/totalspace)*100,2) "使用率%"
from
(select tablespace_name,round(sum(bytes)/1024/1024) totalspace
from dba_data_files
group by tablespace_name) df,
(select tablespace_name,round(sum(bytes)/1024/1024) freespace
from dba_free_space
group by tablespace_name) fs
where df.tablespace_name=fs.tablespace_name;
❾ 如何在Oracle服务器端用脚本创建表空间,实例
1、月底我用来创建TS(表空间)的shell,
现在的TS是VIEWER_DATA_TS,要将其改名为VIEWER_DATA_TS_201410,再创建一个VIEWER_DATA_TS,文件名包含下个月信息201411。
2、为了保证硬盘有足够的空间,我保留了2年的数据
old_ym=$(date -d -24month +%Y%m)
3、为了确保删除TS,先删除TS包含的tables
truncate table VIEWER_${old_ym};
drop table VIEWER_${old_ym} purge;
DROP TABLESPACE VIEWER_DATA_TS_${old_ym} INCLUDING CONTENTS AND DATAFILES;
4、脚本实例
#!/bin/sh
. ~/.bash_profile
this_ym=`date +%Y%m`
next_ym=`date -d 1month +%Y%m`
old_ym=$(date -d -24month +%Y%m)
vts_src=VIEWER_DATA_TS
vts_dst=VIEWER_DATA_TS_${this_ym}
viewer_data_ts1=/data1/oracle/viewer_data_ts_${next_ym}_1.dbf
viewer_data_ts2=/data1/oracle/viewer_data_ts_${next_ym}_2.dbf
sqlplus viewer/user123456 << EOF
alter tablespace $vts_src rename to $vts_dst;
CREATE TABLESPACE $vts_src DATAFILE '$viewer_data_ts1' SIZE 512M AUTOEXTEND ON NEXT 256M MAXSIZE UNLIMITED BLOCKSIZE 16k;
alter TABLESPACE $vts_src add DATAFILE '$viewer_data_ts2' SIZE 512M AUTOEXTEND ON NEXT 256M MAXSIZE UNLIMITED;
truncate table VIEWER_${old_ym};
drop table VIEWER_${old_ym} purge;
DROP TABLESPACE VIEWER_DATA_TS_${old_ym} INCLUDING CONTENTS AND DATAFILES;
EOF
5、计划任务crontab -l,每月28号执行。
0 3 28 * * /home/oracle/prepare_viewer_ts.sh;
或者
0 4 28-31 * * [ `date -d tomorrow +\%e` -eq 1 ] && /home/oracle/prepare_viewer_ts.sh;