Академический Документы
Профессиональный Документы
Культура Документы
DB2 Day 7
DB2
DB2
TRIGGERS. PERFORMANCE TUNING.
UTILITIES
DB2
Triggers
A trigger is a set of actions that will be executed when a defined event occurs. The triggering event can be the following SQL statements
INSERT UPDATE DELETE Triggers are defined for a specific table and once
defined, a trigger is automatically active.
DB2
Triggers
Trigger information is stored in the catalog
tables SYSIBM.SYSTRIGGERS SYSIBM.SYSPACKAGE
Uses of Triggers
DB2
Trigger Activation
A trigger can be activated in one of the two
ways
DB2
Before Trigger
An before trigger
Will activate for each row in the set of affected rows before the triggering SQL
DB2
After Trigger
An after trigger
Will activate for each row in the set of affected rows after the triggering SQL statement has executed successfully. Therefore the trigger body is seeing the table as being in a consistent state. (All transactions have been completed)
DB2
Example
EMP Empno Empname
Dept
Deptno
Deptname
NoofEmp
Deptno
Salary
DB2
Example
We want to check that whenever a new
employee joins the company, his salary should be >=5000 actually happens in the table
DB2
10
DB2
Example
11
Insert into emp Values(E001,ABCDE,D001,4500) As soon as this statement is given, we get the following error message SQLCODE = -438 ERROR =APPLICATION RAISED ERROR SQLSTATE= 75001 DIAGNOSTIC TEXT: QTY SHOULD BE GREATER THAN OR EQUAL TO 0
DB2
12
Example Contd.
Once the insertion is successful the NoofEmp
in the DEPT table should be increased depending on the Deptno for which the employee is added
DB2
13
DB2
14
Example Contd.
Whenever we delete an Employee from the
emp table the NoofEmp in the DEPT table should be decreased depending on the Deptno from which the employee was removed
DB2
15
AFTER DELETE
ON EMP REFERENCING OLD AS O
DB2
16
DROP Trigger
When you no longer want a trigger we can drop
the trigger
DROP TRIGGER TRGAIORD
DB2
17
Performnce Tuning
Select only those columns needed in your application Avoid using select * Use as much as singleton select as possible. Avoid Open the cursors only when needed Close the cursors as soon as possible
using cursors
DB2
18
Performnce Tuning
TUNING THE SQL QUERIES
clause wherever possible
DB2
EXPLAIN Function
Obtains information on how current and
Access Path(INDEX vs Tablespace scan)
Names of indexes used DB2 sort requirements Sequence of steps for subquery processing Tablespace locking requirements
19
..
DB2
EXPLAIN Function
The explain functions puts the information in
So before invoking the EXPLAIN tool create
the plan_table.
a table called plan_table
20
LIKE abc.PLAN_TABLE
DB2
21
EXPLAIN Function
Can be applied to:
An individual SQL statement
SELECT, UPDATE, INSERT, DELETE
DB2
EXPLAIN Function
22
DB2
EXPLAIN Function
If you want to apply Explain for a plan
Then set the bind parameter EXPLAIN to yes
EXPLAIN(YES)
23
DB2
24
DB2 Utilities
Utilities are special programs that can run by
submitting JCL. They run in batch and get their control information via control statements (not through SQLs). Utilities work at the object level and not at row or data items level.
DB2
25
DB2 Utilities
Commonly used Online Utilities
Runstats Reorg Check
Copy
Mergecopy Load
DB2
26
DB2 Utilities
performance and reorganizes indexes so that they are more efficiently clustered.
DB2
Check
tables and consistency
DB2 Utilities
27
specifying the ENFORCE CONSTRAINTS option or after the partial recovery of tablespaces in a referential set
DB2
28
DB2 Utilities
COPY
Used to create an imagecopy for the complete
tablespace or a partition of the tablespace - full imagecopy or incremental imagecopy
DB2
29
DB2 Utilities
MergeCopy
The
MERGECOPY utility combines multiple incremental image copy data sets into a new full or incremental image copy data set
Load
To accomplish bulk inserts into DB2 table Can replace the current data or append to it, i.e.
LOAD DATA REPLACE or LOAD DATA RESUME(S)
DB2
Normalization
A technique that
allows the designer to detect redundancies in a given table
30
It is used to check
the data and/or design your tables
DB2
31
Process of Normalization
Building blocks
database:
Determinant Functional Full
used
to
design
the
dependency
Partial
Transitive
DB2
32
Determinant
Attribute X can be defined as determinant if it uniquely defines the attribute value Y in a given relationship or entity.
Marks Grade
DB2
33
In a given relation R,X&Y are attributes. Attribute Y is functionally dependent on attribute X if each value of X determines exactly one value of Y. This is represented as XY
Stud#
Marks
Course#
DB2
Partial Dependency
are attributes. Attribute Y is partially dependent on attribute X
34
INAME
Course#
ROOMNO
DB2
Transitive Dependency
COURSE# INAME ROOM#
35
Hence
ROOM#
transitively
depends
on
COURSE#
DB2
36
DB2
37
Unnormalized Table
STUDENT_DETAILS
101 102 101 103 104 102 105 DAVIS DANIEL DAVIS SANDRA EVELYN DANIEL SUSAN 04NOV8 6 04NOV8 6 04NOV8 6 12AUG88 22FEB86 04NOV8 6 31AUG86 M4 M4 H6 C3 B3 P3 P3
RESULT
11 NOV04 11 NOV04 22 NOV04 16 NOV 04 26 NOV 04 12 NOV04 12 NOV04 82 82 79 65 77 68 89 A C B B B B A
BASIC CHEM
11 DAYS 8 DAYS
13 DAYS 13 DAYS
DB2
38
PREREQ
MARKS
GRADE
DAVIS
82
102
DANIEL
M4
7 DAYS
82
H6 C3 B3 P3
4 DAYS BASIC CHEM 11 DAYS 8 DAYS BASIC PHYSIC S BASIC PHYSIC S 13 DAYS
79 65 77 68
B B B B
105
SUSAN
P3
13 DAYS
89
DB2
39
DB2
Table in 2NF
COURSE # M4 H6 C3 B3 P3 COURSE NAME APPLIED MATHS
AMERICAN HISTORY
40
BASIC CHEM
11 DAYS 8 DAYS
BASIC PHYSICS
13 DAYS
STUDENT NAME
DATE OF BIRTH 04NOV86 04NOV86 04NOV86 12AUG88 22FEB86 04NOV86 31AUG86 STUD 101 102 COURSE # M4 M4 MARKS 82 82 GRADE A C COURSE # M4 H6 C3 B3 DATE OF EXAM 11 NOV04 22 NOV04 16 NOV 04 26 NOV 04
101
103 104
H6
C3 B3
79
65 77
B
B B
P3
12 NOV04
102
P3
68
DB2
41
DB2
42
TABLE IN 3NF
STUD 101 102 101 STUD COURSE # M4 M4 H6 C3 B3 P3 MARKS GRADE 103 104 101 102 101 103 104 102 82 82 79 65 77 68 A C B B B B UPP BOUND 94 84 64 54 LOW BOUND 85 70 55 45 GRADE A B C D 102 COURSE # M4 M4 H6 C3 B3 P3 MARKS 82 82 79 65 77 68
DB2
43
DB2
44
deletes.
DEMERITS:
1.Data retrieval (Select) operation performance will be severely affected.