Академический Документы
Профессиональный Документы
Культура Документы
as
l
C
1
s
Tamilselvan G
on
c
a
h
n
Be otec atio
Inf rpor
Co
www.oracleact.com
2007-12-25
Contents
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Table Types Normal (heap) table, Index Cluster, Hash Cluster, IOT, Partition,
Global Temporary Table, External Table
Table Access When to use Full Table Scan, NO ROT
Avoiding accidental table scans Queries involved NOT EQUAL,
Searching for NULL values, Using a function on the indexed column.
Functional Indexes
IOT Including clause, over flow
Using Concatenated Index
Index Merges Hint AND_EQUAL(tablename, index1, index2,)
Searching for Ranges Unbounded Range Scan ( < or >) ; Bounded Range Scans
( between 2 values) ; Range Look Up ( value between lowvalue_column and
highvolue_column)
Queries Involving OR Useful Hint USE_CONCAT.
INLIST Iterator
Fast Full Index Scan
Bind Variable and 5 Percent Guess
Optimizing Full Table PCTFREE and PCTUSED
Update Statement
www.oracleact.com
2007-12-25
Table Types
1.1 Relation Table Basic Structure to hold relational data
Heap Table
Index Cluster
Hash Cluster
Partition
External Table
1.2 Object Table Uses Object Type for a column definition
INDEX CLUSTER
2007-12-25
www.oracleact.com
2007-12-25
2007-12-25
BLOCK#
---------3643
3644
DEPTNO DEPTNAME
---------- -----------------------------10
Sales
20
Marketing
www.oracleact.com
BLOCK#
--------3643
3643
3644
3644
EMPNO ENAME
---------- --------------1001 Tamil
1002 Tandon
1003 Muthu
1004 Ravi
g
Sharin s
SALARY DEPTNO of Block
------------------g2
n
o
m
a
1000
10
es
l
b
a
t
2000
10
3000
4000
20
20
2007-12-25
1.
2.
3.
.
www.oracleact.com
2007-12-25
HASH CLUSTER
www.oracleact.com
2007-12-25
HASH CLUSTER
www.oracleact.com
2007-12-25
When a column has low selectivity and it is being used in the WHERE
clause
It is really very hard to determine whether full table scan is appropriate or not.
There are many factors involved row length, number of rows in the table,
whether LOB is stored in-line or out-of-line, number of rows selected etc
If the table and indexes are analyzed, then CBO will decide whether a full table or
index lookup is appropriate based on the statistics available at that time
and predicates in the query.
www.oracleact.com
10
2007-12-25
However, there are some situation, you may want to use full table
scan, then you just add a hint /*+ full(table_name) */ . Use
parallel hint, if the table is really very big, /*+ full(table_name)
parallel(table_name, degree_of_parallelism) */ .
Note: When the value of bind variable is not known, Oracle assumes 5 % of the table rows
will be returned. (Card =500). The table has 10,000 rows.
www.oracleact.com
11
2007-12-25
12
2007-12-25
3.3
Function on Indexed Column
A function on a indexed will force the CBO to opt for full table scan.
SQL>select * from ssn_region where to_char(low_ssn_num) = :b1 ;
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=1 Bytes=27)
1 0 TABLE ACCESS (FULL) OF 'SSN_REGION' (Cost=6 Card=1 Bytes=27)
Note: Rewrite the query.
Example: to_char(low_ssn_num) = :b1 is same as
low_ssn_num = to_number(:b1)
4. FUNCTIONAL INDEXES
SQL> select email_status, count(*) from cust_order group by email_status;
EMAIL_STATUS
COUNT(*)
------------------------------ ---------Not Sent
299
Sent
23770
SQL> alter session set QUERY_REWRITE_ENABLED=TRUE;
Session altered.
SQL> alter session set QUERY_REWRITE_INTEGRITY=TRUSTED;
Session altered.
www.oracleact.com
13
2007-12-25
14
2007-12-25
15
2007-12-25
www.oracleact.com
NUM_ROWS LAST_ANALYZED
---------- -------------------400
18-FEB-2005 14:07:49
16
2007-12-25
www.oracleact.com
17
2007-12-25
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=17)
1 0 SORT (AGGREGATE)
2
www.oracleact.com
18
2007-12-25
www.oracleact.com
19
2007-12-25
PCTTHRESHOLD 2
INCLUDING COUNTRY_NAME
OVERFLOW
TABLESPACE USERS
PCTUSED 40 PCTFREE 10
INITRANS 1 MAXTRANS 255
STORAGE ( INITIAL
16K
NEXT
100M
MINEXTENTS 1
MAXEXTENTS
2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1 BUFFER_POOL DEFAULT )
LOGGING
NOPARALLEL;
Note: PCTTHRESHOLD integer specifies percentage of space reserved
in an IOT. Any portion of the row that exceeds the specified threshold
is stored in the overflow area. PCTTHRESHOLD must be a value from
1 to 50.
www.oracleact.com
20
2007-12-25
There are many benefits having a fact table defined as IOT. They are:
1 The space used for PK is not required. Hence, multiple I/Os on Index and data are
not required.
2 Since most of the fact table has date (or time key) as the first column in the table
as well as first column in the index, that is always used in the WHERE clause, that
leads to speed of retrieving the data.
3 IOTs can be partitioned
Disadvantages are:
1 Physical ROWID is NOT maintained on IOT. The table is left with only logical
ROWID.
In case of block corruption, and you want to extract maximum number of rows
from the table, you have to use PK only, not the min or max of ROWID.
2 PK constraint can not be dropped
3 Unique constraints are not allowed
4 Distribution and replication are not allowed
Carefully evaluate the requirements, particularly ad hoc queries.
If the table requires bit-mapped indexes on columns such as Male/Female, Status
(Active/Inactive), YES/NO etc, then IOT is NOT a good choice.
Also, be careful when you use INCLUDE CLAUSE AND OVERFLOW.
www.oracleact.com
21
2007-12-25
www.oracleact.com
22
2007-12-25
www.oracleact.com
23
2007-12-25
24
2007-12-25
Query -2
select * from ssn_region
where 701010998 between low_ssn_num and high_ssn_num ;
Rows Row Source Operation
------- --------------------------------------------------0 TABLE ACCESS BY INDEX ROWID OBJ#(68289)
0 INDEX RANGE SCAN OBJ#(68294) (object id 68294) --- index used (SSN_REG_HIGH)
Query -3 Dropped Index SSN_REG_HIGH
select * from ssn_region
where 701010998 between low_ssn_num and high_ssn_num
call
count
cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.01
0.00
0
0
0
0
Execute 1
0.00
0.00
0
0
0
0
Fetch
1
0.02
0.03
45
47
0
0
------- ------------- ---------- ---------- ---------- ---------- ---------total
3
0.03
0.04
45
47
0
0
Rows Row Source Operation
------- --------------------------------------------------0 TABLE ACCESS FULL OBJ#(68289)
Note: When the input value is not in the range, then Oracle chooses FULL table scan.
www.oracleact.com
25
2007-12-25
Queries Involving OR
2
3
4
5
6
7
1
2
3
4
3
6
www.oracleact.com
26
2007-12-25
www.oracleact.com
27
2007-12-25
10
Inlist Iterator
Query -1
SQL> select * from t1
where object_name IN ( 'T1', 'T1_IDX_1', 'T1_IDX_2' ) ;
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE (Cost=7 Card=6 Bytes=576)
1 0 INLIST ITERATOR
2
3
1
2
www.oracleact.com
28
2007-12-25
10
Inlist Iterator . Contd
Query 2
SQL> select * from t1
where object_name IN ( 'T1', 'T1_IDX_1', 'T1_IDX_2' ) or
object_id between 69000 and 70000 ;
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE (Cost=8 Card=7 Bytes=672)
1
2
3
4
5
6
7
8
9
10
11
12
www.oracleact.com
29
2007-12-25
30
2007-12-25
13
31
2007-12-25
12
32
2007-12-25
12
33
2007-12-25
13
PCTFREE 20 Minimum % of data block free and available for future update to the
existing rows already within each block
What it means that block allows row inserts until 80 ( = 100 -20) % is occupied.
PCTUSED 40 The data segment is considered unavailable for the insertion of new
rows until the amount of used space falls to 39 % or less (assuming that the
blocks used space reached PCTFREE 80%)
In short, PCTFREE parameter is for INSERT operation where as PCTUSED for
DELETE operation.
ROW CHAINING When data in a row is too large to fit into one data block, then
Oracle allocates 2 or more data blocks for that row. This is called Row Migration.
ROW MIGRATION A row that originally fit into one data block is updated so that the
overall row length increases, and the block's free space is already completely
filled. Oracle migrates the data for the entire row to a new data block, assuming
the entire row can fit in a new block. Oracle preserves the original row piece of a
migrated row to point to the new block containing the migrated row. The ROWID
of a migrated row does not change.
www.oracleact.com
34
2007-12-25
13
name varchar2(30),
68357
www.oracleact.com
DATA_OBJECT_ID
--------------
68357
35
2007-12-25
OBJ#
---------68357
SPARE1
---------736
www.oracleact.com
36
2007-12-25
LENGTH(SOME_TEXT)
----------------1500
1500
1500
1500
1500
www.oracleact.com
37
2007-12-25
OBJ#
---------68357
SPARE1
---------32771
SPARE2
----------
www.oracleact.com
38
2007-12-25
BLOCKNUM
---------12810
12810
12810
12810
12811
12811
12811
12811
ID
--------1
2
3
4
5
6
7
8
R.
E
on
B
M
d
e
U
s
N
k ba
CK
c
O
o
L
l
eB
er b
h
p
t
t
s
ou
ow
r
h
c
4
t
Wa
ted
a
c
o
l
al
e
l
h.
c
t
a
g
r
n
O
w le
o
r
old
8 rows selected.
www.oracleact.com
39
2007-12-25
www.oracleact.com
AVG_ROW_LEN
----------14
40
2007-12-25
www.oracleact.com
41
2007-12-25
14
UPDATE Statement
Old Method
1 update emp2 a
2 set bonus = (select bonus
3
from new_bonus b
4
where b.empid = a.empid)
5 where exists (select null
6
from new_bonus b
7*
where b.empid = a.empid)
SQL> /
2 rows updated.
SQL> select * from emp2 ;
EMPID
BONUS
---------- ---------1001
501
1002
102
1003
502
www.oracleact.com
42
2007-12-25
UPDATE Statement
SQL> select * from emp2 ;
EMPID
BONUS
---------- ---------1001
101
1002
102
1003
103
SQL> select * from new_bonus;
EMPID
BONUS
---------- ---------1001
501
1003
502
New Method -1
SQL> update emp2 a set
2
a.bonus = coalesce (
3
(select b.bonus
4
from new_bonus b
5
where b.empid = a.empid),
6
a.bonus) ;
3 rows updated.
SQL> commit;
Commit complete.
SQL> select * from emp2 ;
EMPID
BONUS
---------- ---------1001
501
1002
102
1003
502
www.oracleact.com
43
2007-12-25
UPDATE Statement
New Method -2
Table altered.
Table altered.
SQL> alter table new_bonus add constraint nb_uq
unique (empid) ;
2007-12-25
Class - 1
www.oracleact.com
45
2007-12-25