oracle學習筆記

oracle學習筆記
 
一、改善數據庫性能
1、        用explain plan 來統計檢查sql 語句的性能
具體操作:
生成plan_table表
通過運行 rdbms\admin\utlxplain.sql
explain plan set statement_id =’statement1’ into plan_table
for  select * from table_name ;

select statement_id, operation, options position from plan_table
注:position 是對開銷得描述
    operation 操作對象
        option
2、用set autotrace
set autotrace on
set autotrace off

set autot on
set autot off

3、        選擇優化sql 語句的方法
1)        基於代價的優化
兩個參數frist_rows 和 all_rows
用修改optimizer_mode 參數的方法:
optimizer_mode 參數在init.ora中。
optimizer_mode 參數 還可以設置爲 choose .
用修改會話的方法:
                alter session set optimizer_goal = frist_rows
                alter session set optimizer_goal = all_rows
用修改提示的方法:
        select         --+frist_rows
*  from table_name where “條件”
                或
        select         /*+frist_rows*/
*  from table_name where “條件”
4、        以上是基於代價的優化,還有基於規則的優化已不常用
5、        使用索引改善性能
1) 索引的建立
create [unique] index index_name
on table table_name (字段1,字段2 )
注:   帶unique選項表示建立唯一索引       
注意: 索引有單列索引,重列索引;
主鍵、候選鍵不用建索引;
                外鍵最好鍵索引;
                索引最好建在大表和查詢頻率高的唯一值的列上;
                LONG型的列不能鍵索引;
                索引不宜建的太多。
1)        索引的刪除
drop index index_name ;
6、        編寫共享池中已有的sql語句
用select sql_text from V$sqlarea 可查看共享池中已有的sql語句

二、調整數據庫性能
1、        提供足夠的內存,減少I/O 操作。
設置 shared_pool_size 、db_block_size、db_block_buffer的大小。(在init.ora中)
2、        減少磁盤競爭
1)        將數據文件和日誌文件放在不同磁盤上
如        create database db_name
        datafile  ‘c:\oracle\data\ db_name01.dbf ’
        logfile   ‘d:\ oracle\data\ log_name01.dbf’
        archivelog
2)        將索引和數據建在不同磁盤上
將索引表空間,數據表空間建在不同的磁盤上,分別基於這兩個表空間建表和索引。
                Create table        table_name
                (        NO                 number(5)                primary key,
                        name         varchar2(10)
)
tablespace        tablespace_in_c;

create  index  index_name
on table_name(NO)
tablespace   tablespace_in_d;
3、        調整回滾段       
建回滾段:
create public rollback segment rollback_name
                tablespace  RBS
                optimal  to 500k;
        注:回滾段建好後和表空間一樣處於脫機狀態,用修改表空間命令將其聯機
修改回滾段:
        alter rollback segment rollback_name
                online| offline
                storage ……
                shrink to  …… ;
使用回滾段:
        savepoint  savepoint_7 ;
        rollback  to savepoint_7;       
       
4、        調整網絡多線程服務器
1)        設置網絡協議的進程調度個數
網絡協議的進程調度個數參數
mts_dispatachers = ‘tcp,5’,’ipc,5’
         mts_max_dispatachers = 30
修改網絡協議的進程調度個數參數
         alter system mts_dispatchers  ‘tcp,8’, ‘ipc,8’ ,
2)        觀察V$dispatacher 數據字典
select  name, network, busy, idle from   V$dispatacher ;
3)        設置、修改服務器進程個數
mts_servers = 5
mts_max_servers = 20
                                alter system mts_servers  8 ;
4)        減少檢測點
log_checkpoint_interval = 1200
log_checkpoint_timeout = 0
5)        啓動檢測點監控進程
checkpoint_process         = true

5、增加日誌組文件
1)        create database db_name
datafile ‘c:\oracle\data\db_name.dbf’
logfile group 1 ‘c:\oralce\log01.dbf’
logfile group 2 ‘d:\oralce\log01.dbf’
logfile group 3 ‘e:\oralce\log01.dbf’
archivelog ;
2)        alter database db_name
add logfile group 4 ‘f:/oracle/log4.daf’        ;
3)        alter database db_name
drop        logfile group 4 ;
三、加快數據收索速度
1、        建立索引
create [unique] index  index_name
                on table_name (字段1、字段2 )
                tablespace tablespace_name ;
2、        若表經常被刪除、修改 應刪除並重建索引
3、        人工強制索引
select  *  /*+ index (字段1) */  from table_name  
4、        建數據簇
create cluster cluster_name (id interger )
                                size 50
                                hash is id
                                hashkey 500 ;
create table table_name
( 定義子段 …… ) cluster cluster_name ()
5、        並行處理
並行處理機制適用於多機處理機制,它能使用分類、連接、表查詢、創建索引等操作並行處理,這對於大型表,尤其是多個大型表的處理十分有效。
Parallel_max_servers = 50
Parallel_min_servers = 5
Parallel_server_idle_time = 10 (單位是分鐘)
並行度= Parallel_default_size
                 = 表中的行數/ Parallel_default_scansize
最大並行度 = Parallel_default_max_scan = 50           

創建表時加上parallel 選項:
                create table table_name
                (        字段1,
                        ……
)
parallel  15

                        select --+parallel ( table_name, 50)
from  table_name ;
                        select --+noparallel ( table_name)
from  table_name ;
                對索引並行處理:
                alter table table_name
                        enable primary key
                        using index parallel
                alter table table_name
                        enable primary key
                        using index noparallel

四、防止訪問衝突
1、        加鎖
lock table  table_name | view_name
                        in lock_type  mode  [ nowait ]
其中lock_type 包括 row share , row exclusive ,share updata , share, exclusive, share row exclusive .



[2002-3-12]
在數據庫中對oracle數據庫對象的常用操作
一、對象:        表、視圖、快照、索引、簇、序列、同義詞、數據庫鏈、存儲過程、函數、觸發器和包、聚集。

1、        table :
建表:
create table table_name         (
                field_name_1  type( length ) [not null] ,
                ……
                unique ( )
        primary key ( )
                foreign key( )  references  other_table.field on delete/updata cascade
……
) tablespace  tablespace_name;


create table  table_name as
         select * from other_table

查詢
select field1, field2,…… from table_name
where          條件
group by  field
having    條件
orader by  field  asc| desc        

刪除:
drop table table_name ;
修改:
alter table table_name
                add (field_1  type  [not null ],
                        ……)
alter table table_name
                modify (field_1  type  [not null ],
…… )
        insert into table_name (field1,field2,field3 )
values(val1,val2,val3);

insert into table_name select * from table2

update table_name field set field = values where ……

alter table table_name  add  constraints  pk_name  primary key (“field_name”) ;

alter table table_name  drop constraints  pk_name;

alter table child_table_name add constraints fk_name foreign key ( “child_field_name.field ”) references parent_table_name ( parent_table_name ) on delete cascade ;

alter table child_talbe_name drop constrints fk_name cascade ;

2、        視圖
建視圖
create view view_name as
                        select * from table_name  
                刪除視圖
                drop view view_name
3、        快照
建快照
create snapshot snapshot_name
build immediate
using index tablespace ‘tablespace_name’ storage (initial 12k next 12k pctincrease 10)
refresh [complete/force/fast] with [rowed/primary key]
using local rollback segment “rbs0”
on [commit/demand]  for update
as select * from table_name

4、        索引
create index index_name on table table_name(field1,field2) ;
                drop  index index_name ;
5、        簇
create cluster cluster_name (val type )
size  100
storage (  )
[index][hashkeys 200] ;

                create table tab_1 (
field1  type
……)
cluster cluster_name ;

6、        序列
create sequence sequence_name
increment by 1
start with 1
maxvalue 1024
minvalue 1
nocycle/cycle
cache 22
noorder/order
7、        同義詞
create synonym synonym_name for view/table ;
create synonym synonym_name for view/table@
8、        數據連接
Create database link link_name connect to user_name identified by password using ‘nstude’                       
9、        存儲過程  
create[or replace] procedure procdure_name
( <參數1> in/out <type> ,
……       
)
is | as
內部變量1        type;
內部變量2        type;
begin
……
end

運行:        execute procdure_name
查看參數                print  參數1

10、        分區
create table table_name(
field1  type ,
field2  type                )
partition by range (field1)
        ( partition part1 values less than (value )
                tablespace        tableapace_name1,         
         partition part1 values less than (value )
                tablespace        tableapace_name2,         
         partition part1 values less than (value )
                tablespace        tableapace_name3
)         

11、        函數
create [or replace] function function_name
( <參數1> in/out <type> ,
……       
)
return  type
is | as
內部變量1        type;
內部變量2        type;
begin
……
end

execute        value := function_name(參數1) ;



12、        觸發器



13、        包






二、數據庫安全
1、        角色
create role  role_name not identified
create role  role_name identified by password
create role  role_name identified externally
create role  role_name identified globally
grant 權限        to role_name ;


2、        用戶
建立用戶:
create user user_name
profile “default”
identified by “password”
default tablespace        tablespace_name
temporary tablespace  temp_tablespace_name
account unlock
修改用戶:
alter user user_name        

三、數據存儲
1、        表空間
create tablespace tablespace_name
logging datafile  'D:\ORACLE\ORADATA\ORASTUDY\TEST.ora' size 5M
default storage  (initial 10k next 10k pctincrease 20)

alter tablespace  tablespace_name default storage (initial 40k)
alter database rename file ‘path\filename.ora’to ‘path1\filename1.ora’
alter database datafile ‘path\filename.ora’ resize 7m
alter database tablespace_name minimum extent 25k
alter database  tablespace_name online
alter database  tablespace_name  offline [normal/temporary/immediate]
注:normal/temporary/immediate 三個參數的選擇參考  p295


2、        回滾段
create rollback segment rollback_name tablespace “RBS”
storage ( INITIAL 10K NEXT 10K OPTIMAL 13K MINEXTENTS 5 MAXEXTENTS 120)
alter rollback  segment rollback_name online
alter rollback  segment rollback_name offline

注 表空間建好後是離線的,需要將其改爲在線。


3、        歸檔重做日誌組
增加日誌組
alter database add logfile group n (’path\logn.ora’)size 1024k
修改日誌組
alter database rename file (’path\logn.ora’)
        to  (’other_path\ other_logn.ora’)
增加日誌組成員
alter database add logfile member ‘path\name_file.ora’to group n


四、備份與回覆
        exp       
        imp
(一)冷備份:
1、        關閉數據庫
2、        創建備份路徑
3、        copy 數據文件、控制文件、系統初始化文件。
4、        打開數據庫。
(二)熱備份:
1、        轉換到歸檔模式下 。
connect  internal
archive log start
查看歸檔模式的狀態:archive log list
        2、置爲備份狀態。
Alter tablespace        temp        begin        backup
3、copy        數據文件
        $        copy  path1\name1                        path2\name2 ;
5、        結束備份
alter tablespace temp                end         backup
6、        設置系統檢測點
alter system        checkpoint
        6、備份控制文件
                alter database backup controlfile  to  path\name
        7、關閉archive log
                archive log stop


(三)數據回覆
                用        V$log_history         可查看日誌文件信息:
                RECID                  
                STAMP                  
                THREAD#                    線程號
                SEQUENCE#        序列號      
                FIRST_CHANGE#    第一次變化號      
                FIRST_TIME       第一次變化時間      
                NEXT_CHANGE#     最後一次變化時間      
        1、不完全回覆
        1)基於時間點的不完成回覆
                connect internal
                startup mount  
                recover database        until time ‘02/19/99 12:43:55’;
                alter database open
        2)基於變化號碼的不完成回覆
                connect internal
                startup mount       
                recover        database until change  12345 ;
                alter database open
        3)基於停止的恢復
                connect internal
                startup mount
                recover        database  ;
(當提示:<ret>= suggest | filename | auto | cancel )作出相應的選擇
                alter database open ;

2、        完全恢復
connect internal
startup mount
recover database        
alter database open
3、        僅對錶空間回覆
recover tablespace tablespace_name
alter database open
4、        聯機恢復
當數據庫工作在archive模式下,當system表空間沒有損壞,方可進行聯機恢復。
Alter        tablespace tablespace_name offline ;
Recover tablespace tablespace_name ;
Alter tablespace tablespace_name online ;
5、        回滾段恢復
create rollback segment rollback_name
tablespace  tablespace_name ;

savepoint savepoint_8

rollback to savepoint_8
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章