❶ 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;