2020年12月24日 星期四

對物件的影響程度 Repair Chained Rows (蟾蜍) < Alter Table Move < Rebuild Table (蟾蜍)

 

對物件的影響程度

Rebuild Table (蟾蜍) > Alter Table Move (reorg) > Repair Chained Rows (蟾蜍)

1. Rebuild Table (蟾蜍) : Create Indexes, Compile Dependencies, Analyze Table

2. Alter Table Move (reorg): Rebuild Indexes, Analyze Table

3. Repair Chained Rows (蟾蜍): Analyze Table




SQL> create table tbl_1224 as select * from employees;


Table created.


SQL> desc tbl_1224;

 Name                                      Null?    Type

 ----------------------------------------- -------- ----------------------------

 EMPLOYEE_ID                                        NUMBER(6)

 FIRST_NAME                                         VARCHAR2(20)

 LAST_NAME                                 NOT NULL VARCHAR2(25)

 EMAIL                                     NOT NULL VARCHAR2(25)

 PHONE_NUMBER                                       VARCHAR2(20)

 HIRE_DATE                                 NOT NULL DATE

 JOB_ID                                    NOT NULL VARCHAR2(10)

 SALARY                                             NUMBER(8,2)

 COMMISSION_PCT                                     NUMBER(2,2)

 MANAGER_ID                                         NUMBER(6)

 DEPARTMENT_ID                                      NUMBER(4)



SQL> CREATE INDEX idx_tbl_1224_01 ON tbl_1224

(EMPLOYEE_ID);  2


Index created.


SQL> select EMAIL from tbl_1224;


EMAIL

-------------------------

DOCONNEL

DGRANT

JWHALEN

MHARTSTE

PFAY

SMAVRIS

HBAER

SHIGGINS

WGIETZ

SKING

NKOCHHAR



SQL> CREATE OR REPLACE FORCE VIEW vw_1224

AS

    select EMAIL from tbl_1224;  2    3


View created.


SQL> select * from vw_1224;


EMAIL

-------------------------

DOCONNEL

DGRANT

JWHALEN

MHARTSTE

PFAY

SMAVRIS

HBAER

SHIGGINS

WGIETZ

SKING

NKOCHHAR



SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and object_type ='VIEW';   2    3


OWNER

------------------------------

OBJECT_NAME

--------------------------------------------------------------------------------

OBJECT_TYPE         STATUS

------------------- -------

HR1

VW_1224

VIEW                VALID




SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and status <> 'VALID';

  2    3

no rows selected




--Alter Table Move 不會讓 depenedncies 的狀態成為 INVALID


SQL> alter table tbl_1224 move;


Table altered.


SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and status <> 'VALID';   2    3


no rows selected


SQL> select

  a.owner       OWNER,

  a.INDEX_NAME  NAME,

  a.TABLE_NAME  TABLE_NAME,

  a.STATUS      STATUS

from all_indexes a

where A.OWNER = 'HR1'

and A.STATUS <> 'VALID';  2    3    4    5    6    7    8


OWNER                          NAME

------------------------------ ------------------------------

TABLE_NAME                     STATUS

------------------------------ --------

HR1                            IDX_TBL_1224_01

TBL_1224                       UNUSABLE



SQL> select * from vw_1224;


EMAIL

-------------------------

DOCONNEL

DGRANT

JWHALEN

MHARTSTE

PFAY

SMAVRIS

HBAER

SHIGGINS

WGIETZ

SKING

NKOCHHAR



SQL> ALTER INDEX IDX_TBL_1224_01 REBUILD;


Index altered.




-- TOAD Rebuild Table 會讓 depenedncies 的狀態成為 INVALID


SQL> SET LINESIZE 200

SQL> ALTER TABLE HR1.TBL_1224 RENAME TO TBL_1224_X;


Table altered.


SQL> CREATE TABLE HR1.TBL_1224

(

  EMPLOYEE_ID     NUMBER(6),

  FIRST_NAME      VARCHAR2(20 BYTE),

  LAST_NAME       VARCHAR2(25 BYTE)             NOT NULL,

  EMAIL           VARCHAR2(25 BYTE)             NOT NULL,

  PHONE_NUMBER    VARCHAR2(20 BYTE),

  HIRE_DATE       DATE                          NOT NULL,

  JOB_ID          VARCHAR2(10 BYTE)             NOT NULL,

  SALARY          NUMBER(8,2),

  COMMISSION_PCT  NUMBER(2,2),

  MANAGER_ID      NUMBER(6),

  DEPARTMENT_ID   NUMBER(4)

)  2    3    4    5    6    7    8    9   10   11   12   13   14

 15  ;


Table created.


SQL> INSERT /*+ APPEND */

  2  INTO HR1.TBL_1224 INS_TBL

 (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL,

  PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY,

  3    4    5    COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID)

SELECT

  EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL,

  PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY,

  COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID

FROM HR1.TBL_1224_X SEL_TBL;

  6    7    8    9   10  COMMIT;

214 rows created.


SQL>


Commit complete.


SQL>

SQL>

SQL>

SQL> DROP INDEX HR1.IDX_TBL_1224_01;


Index dropped.


SQL> CREATE INDEX HR1.IDX_TBL_1224_01 ON HR1.TBL_1224

(EMPLOYEE_ID)  2  ;


Index created.


SQL> select

  a.owner       OWNER,

  a.INDEX_NAME  NAME,

  a.TABLE_NAME  TABLE_NAME,

  a.STATUS      STATUS

from all_indexes a

where A.OWNER = 'HR1'

and A.STATUS <> 'VALID';  2    3    4    5    6    7    8


no rows selected


SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and status <> 'VALID';   2    3


OWNER                          OBJECT_NAME                                                                                         OBJECT_TYPE          STATUS

------------------------------ -------------------------------------------------------------------------------------------------------------------------------- ------------------- -------

HR1                            VW_1224                                                                                             VIEW                 INVALID


SQL> select * from vw_1224;


EMAIL

-------------------------

DOCONNEL

DGRANT

JWHALEN

MHARTSTE

PFAY

SMAVRIS

HBAER

SHIGGINS

WGIETZ

SKING

NKOCHHAR



-- 使用到 VIEW VW_1224 之後,則DB會自動COMPILE該物件



SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and status <> 'VALID';   2    3


no rows selected




-- 這是手動COMPILE該物件


SQL> ALTER VIEW "HR1"."VW_1224" COMPILE;


View altered.


SQL> select OWNER, object_name, OBJECT_TYPE , status

from dba_objects

where  owner = 'HR1' and status <> 'VALID';   2    3


no rows selected


SQL> select

  a.owner       OWNER,

  a.INDEX_NAME  NAME,

  a.TABLE_NAME  TABLE_NAME,

  a.STATUS      STATUS

from all_indexes a

where A.OWNER = 'HR1'

and A.STATUS <> 'VALID';  2    3    4    5    6    7    8


no rows selected


SQL>

Oracle database link and view status

 

結論: 

1. view 裡面有使用到 database link

2. create view 之後,drop database link,view testdb88_hr1_usertables 還是顯示為 VALID

3. 但是 select from testdb88_hr1_usertables 顯示錯誤 ORA-04063: view "HR1.TESTDB88_HR1_USERTABLES" has errors

4. 重新compile TESTDB88_HR1_USERTABLES COMPILE 時,報錯 Warning: compiled but with compilation errors

5. 這時 view status 顯示為 INVALID



CREATE DATABASE LINK HR1_TESTDB88

 CONNECT TO HR1

 IDENTIFIED BY "password"

 USING 'testdb88_DB11G';



CREATE OR REPLACE FORCE VIEW HR1.TESTDB88_HR1_USERTABLES

AS

    SELECT *

      FROM user_tables@HR1_TESTDB88;

  

  

select * from HR1.TESTDB88_HR1_USERTABLES   

  


select OWNER, object_name, OBJECT_TYPE , status

from dba_objects 

where  owner = 'HR1' and object_type ='VIEW'; 



DROP DATABASE LINK HR1_TESTDB88;


select OWNER, object_name, OBJECT_TYPE , status

from dba_objects 

where  owner = 'HR1' and object_type ='VIEW'; 



select * from HR1.TESTDB88_HR1_USERTABLES;

ORA-04063: view "HR1.TESTDB88_HR1_USERTABLES" has errors



SELECT * 

FROM user_tables@HR1_TESTDB88;

Error at line 2

ORA-02019: connection description for remote database not found




ALTER VIEW TESTDB88_HR1_USERTABLES COMPILE;

Warning: compiled but with compilation errors



select OWNER, object_name, OBJECT_TYPE , status

from dba_objects 

where  owner = 'HR1' and object_type ='VIEW'; 



OWNER       OBJECT_NAME                OBJECT_TYPE      STATUS        

--------    ------------               ------------     -------

HR1         TESTDB88_HR1_USERTABLES    VIEW             INVALID

                                                                                

1 row selected.

















2020年12月23日 星期三

何時重建索引 When to Rebuild Indexes

 

http://www.dba-oracle.com/t_oracle_analyze_index.htm

Oracle ANALYZE INDEX

Oracle Database Tips by Donald Burleson



http://www.remote-dba.net/t_tuning_index_rebuilding.htm

Index Rebuilding Techniques

Oracle Tips by Burleson Consulting



何時重建索引

如果每次訪問獲取的塊過多,則我們可能要重建索引,因為過多的塊獲取表示碎片化的B樹結構。另一個重建條件是刪除的葉子節點佔索引節點的20%以上的情況。重建的另一個原因是當任何索引顯示的深度為4或更大時。


When to Rebuild Indexes

Rebuild an Oracle index with the following command:

ALTER INDEX index_name REBUILD TABLESPACE tablespace_name;

We might want to rebuild an index if the block gets per access is excessive, since excessive block gets indicate a fragmented B-tree structure. Another rebuild condition would be cases where deleted leaf nodes comprise more than 20 percent of the index nodes. Another reason to rebuild is when any index shows a depth of 4 or greater.



使用alter index rebuild進行索引重建是非常安全的命令。如果出現任何問題,Oracle會中止該操作並將現有索引保留在原處。

Index rebuilding with the alter index rebuild is a very safe command. If anything goes wrong, Oracle aborts the operation and leaves the existing index in place.