Oracle學習筆記2

xypincle發表於2017-03-22

  1. --檢視具有dba許可權的使用者
  2. select * from v$pwfile_users

  3. --計算日誌緩衝區的塊尺寸
  4. SELECT ROUND((A.REDOSIZE + B.REDOWAST) / C.REDOBLKS) + 16 AS REDO_BLOCK_SIZE
  5.   FROM (SELECT VALUE REDOSIZE FROM V$SYSSTAT WHERE NAME = 'redo size') A,
  6.        (SELECT VALUE REDOWAST FROM V$SYSSTAT WHERE NAME = 'redo wastage') B,
  7.        (SELECT VALUE REDOBLKS FROM V$SYSSTAT WHERE NAME = 'redo blocks written') C
  8.     
  9. --快取命中率查詢語句
  10. select 1 - (sum(decode(name, 'physical reads', value, 0)) /
  11.        (sum(decode(name, 'db block gets', value, 0)) +
  12.        (sum(decode(name, 'consistent gets', value, 0))))) "緩衝命中率"
  13.   from v$sysstat;    

  14. --合併索引分割槽碎片
  15. select 'alter index ' || a.owner || '.' || a.index_name || ' coalesce ;'
  16.   from dba_indexes a
  17.  where a.owner in ('ZLHIS', 'ZLTOOLS')
  18.    and a.index_type = 'NORMAL'
  19.    AND A.uniqueness = 'UNIQUE'
  20.    AND A.status = 'VALID'
  21.    AND A.tablespace_name IS NOT NULL;
  22.     
  23. --檢查資料庫的狀態
  24. SQL>select instance_name,status from v$instance ; --status=open    

  25. --監聽器的啟動、停止和狀態
  26. $lsnrctl start
  27. $lsnrctl stop
  28. $lsnrctl status

  29. --檢查表空間的使用情況
  30. SELECT A.TABLESPACE_NAME AS "TableSpace Name",
  31.        A.TOTAL_SIZE AS "Total Size",
  32.        ROUND(B.TOTAL_FREE_SIZE, 1) AS "Total Free Size",
  33.        ROUND((A.TOTAL_SIZE - B.TOTAL_FREE_SIZE), 2) AS "Used Size",
  34.        TO_CHAR(100 * B.TOTAL_FREE_SIZE / A.TOTAL_SIZE, '99.99') || '%' AS "Percent Free"
  35.   FROM (SELECT TABLESPACE_NAME, SUM(BYTES) / 1024 / 1024 AS TOTAL_SIZE
  36.           FROM DBA_DATA_FILES
  37.          GROUP BY TABLESPACE_NAME) A,
  38.        (SELECT TABLESPACE_NAME, SUM(BYTES / 1024 / 1024) AS TOTAL_FREE_SIZE
  39.           FROM DBA_FREE_SPACE
  40.          GROUP BY TABLESPACE_NAME) B
  41.  WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME

  42. --另一種查詢方法
  43. SELECT A.TABLESPACE_NAME AS TABLESPACE_NAME,
  44.        ROUND(A.BYTES / (1024 * 1024 * 1024), 2) AS "TOTAL(G)",
  45.        ROUND(B.BYTES / (1024 * 1024 * 1024), 2) AS "USED(G)",
  46.        ROUND(C.BYTES / (1024 * 1024 * 1024), 2) AS "FREE(G)",
  47.        ROUND((B.BYTES * 100) / A.BYTES, 2) AS "% USED",
  48.        ROUND((C.BYTES * 100) / A.BYTES, 2) AS "% FREE"
  49.   FROM SYS.SM$TS_AVAIL A, SYS.SM$TS_USED B, SYS.SM$TS_FREE C
  50.  WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME
  51.    AND A.TABLESPACE_NAME = C.TABLESPACE_NAME;

  52. --計算空間使用情況
  53. SELECT UPPER(F.TABLESPACE_NAME) AS "表空間名稱",
  54.        ROUND(D.AVAILB_BYTES, 2) AS "表空間大小(G)",
  55.        ROUND(D.MAX_BYTES, 2) AS "最終表空間大小(G)",
  56.        ROUND((D.AVAILB_BYTES - F.USED_BYTES), 2) AS "已使用空間(G)",
  57.        TO_CHAR(ROUND((D.AVAILB_BYTES - F.USED_BYTES) / D.AVAILB_BYTES * 100,2),'999.99') AS "使用比",
  58.        ROUND(F.USED_BYTES, 6) AS "空閒空間(G)",F.MAX_BYTES AS "最大塊(M)"
  59.   FROM (SELECT TABLESPACE_NAME,
  60.                ROUND(SUM(BYTES) / (1024 * 1024 * 1024), 6) USED_BYTES,
  61.                ROUND(MAX(BYTES) / (1024 * 1024 * 1024), 6) MAX_BYTES
  62.           FROM SYS.DBA_FREE_SPACE
  63.          GROUP BY TABLESPACE_NAME) F,
  64.        (SELECT DD.TABLESPACE_NAME,
  65.                ROUND(SUM(DD.BYTES) / (1024 * 1024 * 1024), 6) AVAILB_BYTES,
  66.                ROUND(SUM(DECODE(DD.MAXBYTES, 0, DD.BYTES, DD.MAXBYTES)) / (1024 * 1024 * 1024),6) MAX_BYTES
  67.           FROM SYS.DBA_DATA_FILES DD
  68.          GROUP BY DD.TABLESPACE_NAME) D
  69.  WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
  70.  ORDER BY 4 DESC
  71.    
  72.  --檢查是否有表空間碎片
  73.  --FSFI%(可用破碎空間索引):數值最大值為100,表示完全沒有破碎的空間。數值越低表明破碎的空間越嚴重,低於30%就需要進行重整
  74. SELECT A.TABLESPACE_NAME AS "TableSpace Name",
  75.        SQRT(MAX(A.BLOCKS) / SUM(A.BLOCKS)) * (100 / SQRT(SQRT(COUNT(A.BLOCKS)))) AS "FSFI%(可用破碎空間索引)"
  76.   FROM DBA_FREE_SPACE A
  77.  GROUP BY A.TABLESPACE_NAME
  78.  ORDER BY 1;
  79.  
  80. --每月資料庫增長報告
  81. SELECT TO_CHAR(A.CREATION_TIME, 'RRRR Month') AS "Month",
  82.        SUM(A.BYTES) / 1024 / 1024 / 1024 AS "Growth in GB"
  83.   FROM SYS.V_$DATAFILE A
  84.  GROUP BY A.CREATION_TIME
  85.  ORDER BY TO_CHAR(A.CREATION_TIME, 'RRRR Month');
  86.  
  87. --每月表空間增長報告
  88. SELECT A.TS# AS "TableSpace No",
  89.        B.NAME AS "TableSpace Name",
  90.        SUM(A.BYTES) / 1024 / 1024 / 1024 AS "Growth in GB"
  91.   FROM SYS.V_$DATAFILE A, SYS.V$TABLESPACE B
  92.  WHERE A.CREATION_TIME > SYSDATE - 365
  93.    AND A.TS# = B.TS#
  94.  GROUP BY A.TS#, B.NAME, TO_CHAR(A.CREATION_TIME, 'RRRR Month')
  95.  ORDER BY A.TS# ;

  96. --表分析,並將分析的結果放入到chained_rows表中
  97. CREATE TABLE system.chained_rows(
  98. owner_name VARCHAR2(30),
  99. table_name VARCHAR2(30),
  100. cluster_name VARCHAR2(30),
  101. partition_name VARCHAR2(30),
  102. subpartition_name VARCHAR2(30),
  103. head_rowid ROWID,
  104. analyze_timestamp DATE
  105. ) ;
  106. --以下為分析的語句
  107. SELECT 'analyze table ' || OWNER || '.' || TABLE_NAME || ' list chained rows into system.chained_rows ;'
  108.   FROM DBA_TABLES
  109.  WHERE OWNER IN ('ZLHIS', 'ZLTOOLS');
  110.  
  111. --Oracle資料庫的構成
  112. 資料檔案 data file
  113. 線上重做日誌檔案 online redo log file
  114. 控制檔案 control file
  115. 初始引數檔案 initialization parameter file
  116. 密碼檔案 password file
  117. 歸檔日誌檔案 archive log file
  118. 警告日誌檔案 alert log file
  119. 跟蹤檔案 trace file

  120. --新增一個日誌檔案
  121. ALTER DATABASE ADD LOGFILE GROUP 4 ('C:\app\Administrator\oradata\CeShiKu\REDO4.LOG') SIZE 50m ;

  122. --新增一個日誌檔案到組中(不需要指定大小,自動按照組中其他成員的大小)
  123. alter database add logfile member 'C:\app\Administrator\oradata\CeShiKu\REDO1A.LOG' to group 1;

  124. --刪除日誌檔案(狀態為inactive,否則需要切換日記)
  125. alter system switch logfile ;
  126. alter database drop logfile group 1 ;

  127. --初始化引數分為動態引數和靜態引數
  128. --動態引數使用alter system之後不需要重啟資料庫即可生效;
  129. --靜態引數修改後需要重啟資料才能生效;

  130. --透過spfile建立pfile
  131. create pfile='c:\pfile.ora' from spfile ;

  132. --建立表空間
  133. CREATE TABLESPACE USERSDATA
  134. DATAFILE 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\USERS_DATA_01.DBF' SIZE 100M
  135. EXTENT MANAGEMENT LOCAL UNIFORM SIZE 10M ;

  136. CREATE TABLESPACE USERSDATA
  137. DATAFILE 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\USERS_DATA_01.DBF' SIZE 100M
  138. EXTENT MANAGEMENT LOCAL AUTOALLOCATE ;

  139. --刪除表空間
  140. DROP TABLESPACE USERSDATA INCLUDING CONTENTS AND DATAFILES [cascade constraints] ;

  141. --建立undo表空間
  142. CREATE UNDO TABLESPACE UNDOTBS2
  143. DATAFILE 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\UNDOTBS02.DBF' SIZE 700M ;

  144. --建立臨時表空間
  145. CREATE TEMPORARY TABLESPACE TEMP02
  146. TEMPFILE 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\TEMP02.DBF' SIZE 100M
  147. EXTENT MANAGEMENT LOCAL UNIFORM SIZE 10M ;

  148. --表空間離線
  149. alter tablespace tablespaceName offline ;

  150. --表空間線上
  151. alter tablespace tablespaceName on ;

  152. --表空間增加一個資料檔案
  153. alter tablespace tablespaceName
  154. add datafile 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\USERS_DATA_01.DBF' size 100m ;

  155. --增加或減少表空間的大小
  156. alter tablespace tablespaceName
  157. datafile 'C:\APP\ADMINISTRATOR\ORADATA\CESHIKU\USERS_DATA_01.DBF' resize 200m ;

  158. --修改資料檔案的大小
  159. alter database datafile 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\SYSAUX01.DBF' resize 1000m;

  160. --設定undo的時間
  161. SQL>alter system set undo_retention=1800 scope=both ;(both=spfile+memory)

  162. --如何遷移系統資料庫檔案到其他地方
  163. 1.SQL>shutdown immediate ;
  164. 2.SQL>host copy c:\app\Administrator\oradata\CeShiKu\system01.dbf d:\oradata\system01.dbf;
  165. 3.SQL>startup mount ;
  166. 4.SQL>alter database rename file 'c:\app\Administrator\oradata\CeShiKu\system01.dbf' to 'd:\oradata\system01.dbf';
  167. 5.SQL>alter database open ;

  168. --如何遷移非系統資料檔案到其他地方
  169. 1.SQL>alter tablespace usertbs offline ;
  170. 2.SQL>host copy c:\app\Administrator\oradata\CeShiKu\usertbs01.dbf d:\oradata\usertbs01.dbf ;
  171. 3.SQL>alter database rename file 'c:\app\Administrator\oradata\CeShiKu\usertbs01.dbf' to 'd:\oradata\usertbs01.dbf';
  172. 4.SQL>alter tablespace usertbs online ;

  173. --open_cursors的解釋
  174. open_cursors:控制每個session最多能夠同時開啟的cursor的數量,當超過這個cursor數量時,就會出現ora-01000錯誤;
  175. --下面的語句用於查詢該引數的設定值以及曾經達到的最大值
  176. select max(a.value) as highest_open_cur,p.value as max_open_cur
  177. from v$sesstat a,v$statname b,v$parameter p
  178. where a.statistic# = b.statistic#
  179. and b.name = 'opened cursors current'
  180. and p.name = 'open_cursors'
  181. group by p.value ;

  182. SQL語句在Oracle中的分類:
  183. 1.簡單SQL語句(single SQL):無巢狀,整個語句只有一層select。
  184. 2.複雜SQL語句(complex SQL):有巢狀:使用了檢視,子查詢,集合操作等的SQL語句。

  185. --傳統資料庫的型別
  186. 1.OLTP:ONLINE TRANSACTION PROCESSING:線上事務系統
  187. 2.OLAP:ONLINE ANALYTICAL PROCESSING:線上分析系統

  188. --block的大小是由 db_block_size 的大小決定的
  189. show parameter db_block_size ;

  190. --系統檢查點
  191. alter system checkpoint ;

  192. --日記切換
  193. alter system switch logfile ;

  194. --重新整理系統緩衝區(注意在生產環境中不要輕易使用)
  195. alter system flush buffer_cache ;

  196. --檢視隱含引數_use_single_log_writer的值,判斷是否使用多個log_writer程式
  197. --_use_single_log_writer:false - 否 ADAPTIVE - 自適應
  198. select a.ksppinm,b.ksppstvl,a.ksppdesc
  199. from sys.x$ksppi a, sys.x$ksppcv b
  200. where a.indx = b.indx
  201. and a.ksppinm like '%log_writer%' ;

  202. --windows系統中啟用多執行緒的模式
  203. alter system set threaded_execution=true scope=spfile ;

  204. --Oracle刪除口令檔案的後果
  205. Oracle可以正常啟動,但是在授權sysdba的時候會報錯
  206. SQL>grant sysdba to system ;
  207. ORA-01994: GRANT 失敗: 口令檔案缺失或已禁用

  208. --檢視具有sysdba/sysoper許可權的使用者列表
  209. select * from v$pwfile_users ;

  210. --Oracle11g及以上隱藏了一個關於Oracle_Base的引數,查詢語句如下:
  211. SELECT x.ksppinm NAME, y.ksppstvl VALUE, x.KSPPDESC PDESC
  212.   FROM SYS.x$ksppi x, SYS.x$ksppcv y
  213.  WHERE x.indx = y.indx
  214.    AND x.ksppinm LIKE '%oracle_base%';

  215. --ADR(automatic diagnostic repository)自動診斷庫的資訊,可以透過以下語句獲得:
  216. select * from v$diag_info ;

  217. --如何檢視隱藏引數
  218. SELECT i.ksppinm name,
  219.        i.ksppdesc description,
  220.        CV.ksppstvl VALUE,
  221.        CV.ksppstdf isdefault,
  222.        DECODE(BITAND(CV.ksppstvf, 7),1,'MODIFIED',4,'SYSTEM_MOD','FALSE') ismodified,
  223.        DECODE(BITAND(CV.ksppstvf, 2), 2, 'TRUE', 'FALSE') isadjusted
  224.   FROM sys.x$ksppi i, sys.x$ksppcv CV
  225.  WHERE i.inst_id = USERENV('Instance')
  226.    AND CV.inst_id = USERENV('Instance')
  227.    AND i.indx = CV.indx
  228.    AND i.ksppinm LIKE '/_%' ESCAPE '/'
  229.  ORDER BY REPLACE(i.ksppinm, '_', '') ;
  230.  
  231.  --如何清除緩衝區中的資訊
  232.  alter system flush buffer_cache ;
  233.  alter system flush shared_pool ;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28878983/viewspace-2135816/,如需轉載,請註明出處,否則將追究法律責任。

相關文章