Vous êtes sur la page 1sur 189

HE QUAN TR C S D LIEU

Chng 0 Gii thieu

GV: Pham Th Bach Hue


Email: ptbhue@fit.hcmus.edu.vn

Gii thieu

Thi lng:
So tiet LT: 45 (3TC)
So tiet TH: 30 (1TC)
So tn ch: 4 TC
Thc hanh tren SQL Server.
Giao tiep qua he thong phuc vu TTX
cua trng.

1
Muc ch mon hoc
Cung cap kien thc ve nguyen ly lam
viec cua mot HQT CSDL e:
Hieu va can thiep (neu c) vao qua trnh
quan ly d lieu cua HQT CSDL nham lam
cho HQT CSDL ap ng yeu cau cong viec
mot cach hieu qua hn.

Vai tro mon hoc


1. CSDL
Hieu cach to chc d lieu.
Biet cach lam cho d lieu ung an.
Biet truy xuat d lieu.
2. CSDL nang cao
Biet thiet ke c s d lieu (ung chuan).
3. He quan tr CSDL
Tnh nang cua 1 CT quan ly CSDL.
4. Phan tch thiet ke HTTT
Van dung tong hp kien thc a hoc nham
xay dng mot ng dung (application).

2
Kien thc nen tang
Mon hoc hoc trc: c s d lieu.
Yeu cau ve kien thc: cau truc d lieu,
he ieu hanh, toan ri rac.

Noi dung
Chng I: Tong quan ve mot HQT CSDL

Chng II: Quan ly giao tac va truy xuat ong thi

Chng III: Phuc hoi d lieu khi co s co An toan d lieu

Chng IV: Lu tr d lieu va cac phng thc truy xuat

Chng V: X ly va toi u hoa cau truy van

3
Quy nh
Cach tnh iem:
7 iem ly thuyet + 3 iem thc hanh.
Hnh thc thi:
Thi trac nghiem, c s dung tai lieu.

Tai lieu tham khao

1. Elmasri & Navathe, Fundamentals of database


systems, Pearson Education Inc., 4th edition, 2004.
2. A. Silberschatz, H. F.Korth, S. Sudarshan, Database
System Concepts, McGraw-Hill, 2002.
3. T. Connolly, C. Begg, Database Systems, Addison
Wesley, 3rd edition, 2002.
4. H. Garcia-Molina, J. D. Ullman, J. Widom, Database
systems: The complete book, Prentice Hall, 2002.

4
Het chng gii thieu.

5
Chng 1
Tong quan ve mot he quan tr
c s d lieu

GV: Pham Th Bach Hue


Email: ptbhue@fit.hcmus.edu.vn

Nhac lai noi dung mon hoc

Chng I: Tong quan ve mot HQT CSDL


Chng II: Quan ly giao tac va truy xuat ong thi
Chng III: Phuc hoi d lieu sau s co An toan d lieu
Chng IV: Lu tr d lieu va cac phng thc truy xuat
Chng V: X ly va toi u hoa cau truy van

1
Muc tieu
zGii thieu kien thc tong quan lien quan en mot HQT
CSDL.

Noi dung

1. Gii thieu ve HQT CSDL.


2. Lch s phat trien cua cac HQT
CSDL.
3. Kien truc mot HQT CSDL.
4. Phan loai cac HQT CSDL.

2
Nhac lai
z Mot so ac tnh cua CSDL

Mot CSDL la mot tap hp d lieu co quan he logic


vi nhau.

CSDL the hien mot the gii thc thu nho.

CSDL c thiet ke, cai at va khai thac cho muc


ch cu the.

Co kch thc va o phc tap khac nhau.

c duy tr thu cong hoac bang may tnh.

Nhac lai
z ac tnh cua d lieu trong mot CSDL

Co the loai bo s trung lap.

Co the am bao tnh nhat quan va ung an.

De truy xuat.

Co the chia se d lieu cho nhieu ngi dung (tao


nhieu khung nhn khac nhau).

Co the am bao an toan d lieu.

Cho phep truy xuat ong thi.

Khoi phuc d lieu khi co s co.

3
nh ngha

z Database Management System : DBMS

nh ngha: La mot chng trnh cho


phep ngi dung nh ngha, xay
dng, thao tac, chia se va bao ve d
lieu.

V du: SQL Server, Oracle, ...

Ngi dung
z Cac ngi dung lien quan en 1 CSDL
Ngi quan tr CSDL (dba).
Ngi thiet ke CSDL (Database
designer).
Ngi dung.
Phan tch vien he thong va Lap trnh
vien ng dung.

4
Dung HQT CSDL
z Co khi khong nen dung HQT CSDL do:
Chi ph cao cho phan cng, phan mem,
huan luyen.
He thong cham do tnh tong quat cua HQT
khi nh ngha, x ly d lieu, quan ly bao
mat, ieu khien ong thi, quan ly khoi
phuc d lieu,
z Dung file e lu thong tin khi:
CSDL n gian, khong thay oi thng
Khong can nhieu ngi dung truy cap vao
d lieu.

Lch s phat trien cua HQT CSDL

z Tuy vao s phat trien cua ng dung s dung HQT


CSDL
1960s 1980s: Mo hnh phan cap, mo hnh
mang, dung file.
Cuoi 1970s 1980s: RDBMS.
1980s: ng dung hng oi tng.
1990s: cac ng dung Web va nhu cau trao oi d
lieu tang cao d lieu XML. CSDL a phng
tien, khai khoang d lieu, d lieu thi gian thc,
... cau truc d lieu phc tap, kieu d lieu mi,
phng thc thao tac tren kieu d lieu thay oi,
cach thc lu tr va cau truc ch muc thay oi, ...

5
Cac thanh phan cua HQT CSDL
Chng trnh ng dung
Giao tiep chng trnh
Quan ly an toan
Quan lyphuc hoi d lieu
Quan lygiao tac
Quan lytruy xuat ong thi
Quan ly lu tr

Giao tiep chng trnh

z Ngon ng nh ngha d lieu (DDL- Data


Definition Language) dung e tao cau truc
CSDL (tao bang, ch muc)

z Ngon ng thao tac d lieu (DML- Data


Manipulation Language): them, xoa, sa, tm
kiem d lieu, c dung trong cac chng
trnh ng dung
z Giao dien HQT CSDL
Menu-based, cho web client, giao dien o
hoa, giao dien form, giao dien danh rieng
cho DBA.

6
Quan ly an toan

z Phan chia quyen truy xuat d lieu, bao


ve d lieu, ngan nga cac truy xuat
khong hp le.
z V du: CSDL quan ly d lieu ve ang
ky hoc phan va iem so cua sinh vien:
Sinh vien co the nhap mi hoc phan
d nh hoc.
Giao vien co the them mi 1 hoc
phan d nh m.
Sinh vien khong the sa iem thi.

Phuc hoi d lieu

z Phuc hoi d lieu sau khi co s co xay


ra. Cac thao tac cua cac giao tac cha
c hoan tat se c undo (da vao
log file).
z V du: He thong quan ly d lieu cua 1
ngan hang phai co c che x ly phu hp
khi mat ien: giao dch a hoan tat th
phai c ghi nhan, giao dch khong
hoan tat th khong c ghi nhan.

7
Quan ly giao tac

z Giao tac (transaction) la 1 tap hp co th


t cac thao tac truy xuat d lieu tren
CSDL thanh 1 n v cong viec logic
(xem la 1 thao tac nguyen to), chuyen
CSDL t trang thai nhat quan nay sang
trang thai nhat quan khac.

z Quan ly giao tac ieu hanh va phoi hp


viec thc hien cac giao tac tren CSDL.

Quan ly giao tac (tt)

Ket qua tng


ng

DB DBMS
Server

Client Client Client

Scheduler

8
Quan ly x ly ong thi

z n v canh tranh truy xuat la giao tac.

z ieu khien canh tranh truy xuat nham


phat hien va ngan nga cac tnh trang
lam cho mot giao tac khong the tiep
tuc thc hien mot cach bnh thng.
V du: Hai khach hang cung mua 1 ve
may bay.

Quan ly lu tr

z Giao tiep vi he ieu hanh e truy xuat


va lu tr d lieu.

z HQT CSDL se quyet nh s dung loai


phng thc truy xuat trong khi he ieu
hanh se thc hien viec truy xuat d
lieu.

9
Meta data (Data Dictionary)

z Meta data con c goi la sieu d


lieu, la d lieu e quan ly d lieu.
z Lu tr cau truc cua cac CSDL, cac
ngi dung, mat khau, quyen truy
xuat, ch muc, ...

Kien truc cua DBMS


Application Database
Naive Users Casual Users Administrator
Programmers

Data Manipulation Call Language Data Definition


Language Interface Language
Forms Application DML CLI DDL
Front ends Interface

SQL Commands
5

DBMS Query
Plan Executor Parser
Concurrency Evaluation
Control Engine
Operator Evaluator Optimizer

DDL
Files/ Access Methods Compiler
Transaction
4
Recovery
Manager

2 Manager
Buffer Management
Lock
Manager 3
Disk Management

System
Index files Data files
Catalog

10
Phan loai HQT CSDL
z Theo mo hnh d lieu
Mang
Phan cap
Quan he
Hng oi tng
XML
z Theo kien truc DB
n ngi dung
Khach / Chu (Client/ Server)
Phan tan
z Theo loai d lieu
D lieu Multimedia
D lieu thi gian thc

z ...

Het chng 1.

11
Chng 2

Giao tac ieu khien ong thi

GV: Pham Th Bach Hue


Email: ptbhue@fit.hcmus.edu.vn

Nhac lai noi dung mon hoc


Chng I: Tong quan ve mot HQT CSDL
Chng II: Quan ly giao tac va truy xuat ong thi
Chng III: Phuc hoi d lieu sau s co An toan d
lieu
Chng IV: Lu tr d lieu va cac phng thc truy
xuat
Chng V: X ly va toi u hoa cau truy van

2
Muc tieu
Biet x ly cong viec tren CSDL theo giao tac.
Hieu va/ hoac van dung cac ky thuat ieu
khien ong thi.

Noi dung
1. Dan nhap
2. Giao tac
3. Giao tac truy xuat ong thi
4. Lch thao tac
5. ieu khien ong thi dung ky thuat khoa
6. Mc co lap cua giao tac
7. Deadlock
8. Cach s dung cac phng thc khoa
9. ieu khien ong thi dung ky thuat nhan thi
gian
10. ieu khien ong thi dung phng phap kiem tra
hp le

4
1. DAN NHAP
z V sao phai thc hien giao tac?
z V sao phai ieu khien ong thi?

Dan nhap
Y tng giao tac e mo ta mot n v x ly
trong qua trnh x ly d lieu trong CSDL.
Nhieu ngi cung thao tac tren CSDL co
the gay ra tnh trang CSDL sai ieu
khien ong thi.

6
2. GIAO TAC

z nh ngha
z Tnh chat
z Viet giao tac

NH NGHA GIAO TAC

Giao tac la 1 tap hp cac thao tac co th t


truy xuat d lieu tren CSDL thanh 1 n v
cong viec logic (xem la 1 thao tac nguyen to),
chuyen CSDL t trang thai nhat quan nay
sang trang thai nhat quan khac.

8
GIAO TAC
Cho 2 quan he
LOP (MALOP, TENLOP, SISO)
SV (MASV, TENSV, MALOP)
RBTV: SISO cua 1 lp phai bang so sinh vien thuoc
lp o.
Hay viet giao tac them 1 SV vao 1 lp
Giao tac Them_SV (v_masv, v_tensv, v_malop)
Bat au GT
Neu v_malop khong ton tai
Rollback tran
Insert into SV (v_masv,v_tensv,v_malop)
Update LOP Set SoSV= SoSV + 1
Where Malop = v_malop
Cuoi giao tac Them_SV
9

Tnh chat cua giao tac: ACID


Atomic Tnh nguyen to
Khong the chia nho.
Consistency Tnh nhat quan
Chuyen CSDL t trang thai nhat quan nay sang trang
thai nhat quan khac.
Isolation Tnh co lap
Cac giao tac x ly ong thi phai oc lap vi nhng
thay oi cua giao tac khac.
Durability Tnh lau dai, ben vng
Khi giao tac hoan tat, tat ca thay oi phai c ghi
nhan chac chan len CSDL.

10
T-SQL ac trng cua giao tac
BEGIN TRANSACTION Bat au giao tac.

COMMIT Ket thuc giao tac thanh


TRANSACTION cong.

ROLLBACK Ket thuc giao tac khong


TRANSACTION thanh cong, CSDL c tra
ve tnh trang trc khi thc
hien giao tac.

11

Mot so lu y
Kiem tra loi
khong co quyen, vi pham rang buoc, deadlock
Bien toan cuc @@ERROR
=0 : khong loi, 0 : co loi
GT Khong t rollback khi gap loi trong qua trnh thc
hien
Can kiem tra gia tr bien @@ERROR sau moi cau lenh
thanh phan va x ly loi neu co
Bien @@ROWCOUNT.
Long 32 tang, lenh rollback tang bat ky lam
rollback toan bo giao tac.

12
3. GIAO TAC TRUY XUAT ONG THI
z Gii thieu
z Cac van e cua x ly ong thi

13

Ly do ieu khien ong thi


am bao nhieu giao tac thc hien ong thi ma van am bao
tnh ung an tren CSDL

Transaction
manager
Bo lap lch nhan yeu cau
Yeu cau Read/Write Read/Write t cac giao tac
va ieu khien: cho thc thi
Scheduler hoac ch hoac huy giao tac
tuy vao ky thuat ieu khien
ong thi c cai at
Reads & Writes

Buffers

14
Cac van e cua x ly ong thi
1. Mat d lieu cap nhat (Lost update)
TH1:

T1 T2
Begin Tran
Read A
Begin Tran
Read A
A:=A+10
Write A

A:=A*100

Write A

Commit Tran

Commit Tran

15

Cac van e cua x ly ong thi


1. Mat d lieu cap nhat (Lost update)
TH2:

T1 T2
Begin Tran
Begin Tran
Write A

Write A

Commit Tran

Commit Tran

16
Cac van e cua x ly ong thi
2. oc d lieu cha commit (Uncommit data, Dirty read)

T1 T2
Begin Tran
Read A
A:=A+10
Write A
Begin Tran
Read A
Print A
Commit Tran
Rollback Tran

17

Cac van e cua x ly ong thi


3. Thao tac oc khong the lap lai (Unrepeatable data)
T1 T2
Begin Tran
Read A
Begin Tran
Read A
A:=A+10
Write A
Commit Tran
Read A
Commit Tran

18
Cac van e cua x ly ong thi
4. Phantom
T1 T2
Begin Tran
Select * From SV
Begin Tran
Insert into SV values
(...)
Commit Tran
Select * From SV
Commit Tran

19

4. LCH THAO TAC


z Cac khai niem:
z Lch bieu
z Lch bieu tuan t
z Lch bieu kha tuan t
z Lch bieu co the phuc hoi
z Lch bieu khong rollback day chuyen
z Bo lap lch
z Nghi thc

20
Mot so khai niem
1. Hoat ong cua cac giao dch ong thi c coi la ung
an neu va ch neu tac dung cua no giong nh tac dung co
c khi cho thc hien chung mot cach tuan t.
2. Lch bieu (schedule)
n1: Lch bieu cua mot tap cac giao tac la th t trong o
cac thao tac trong giao tac c thc hien. Th t cua cac
thao tac trong lch bieu phai tuan theo ung th t cua
chung trong giao tac cho trc.
n2: Mot lch bieu la mot chuoi sap theo thi gian cac
hanh ong c thc hien bi mot hoac nhieu giao tac.

21

T1 T2 T1 T2 T1 T2
Read (A,t) Read (A,s) Read (A,t)
Read (A,t)
t:=t+100 s:=s*1
t:=t+100
Write(A,t) Write(A,s) t:=t+100
Write(A,t)
Read(B,t) Read(B,s) Write(A,t)
Read(A,s)
t:=t+100 s:=s*1 Read(A,s)
Write(B,t) Write(B,s) s:=s*1
s:=s*1
Write(A,s)
nh ngha GT Write(A,s)
Read (B,s)
Read(B,t)
s:=s*1
t:=t+100
Write(B,s)
Write(B,t)
Read(B,t)
Read (B,s)
t:=t+100
s:=s*1
Write(B,t)
Write(B,s)
Lch bieu 1
22 Lch bieu 2
Mot so khai niem
Lch bieu c goi la tuan t (serial) neu th t thc hien cac
thao tac trong lch bieu la tat ca cac thao tac cua giao tac nay
roi en tat ca cac thao tac cua giao tac khac va c nh vay.
Moi lch bieu tuan t eu am bao tnh nhat quan cho c s d
lieu.
Lch bieu c goi la kha tuan t (serializable) neu tac dung cua
no giong nh tac dung cua mot lch bieu tuan t nao o. Tc la
chung sinh ra cung mot gia tr cho moi n v d lieu.

23

Lch bieu tuan t


T1 T2

Read (A,t)

t:=t+100

Write(A,t)

Read(B,t)

t:=t+100

Write(B,t)

Read(A,s)

s:=s*1

Write(A,s)

Read (B,s)

s:=s*1

Write(B,s)

Read(A,s)
24
Lch kha tuan t
T1 T2 A B T1 T2 A B
25 25 25 25
Read (A,t) Read (A,t)
t:=t+100 t:=t+100
Write(A,t) 125 Write(A,t) 125
Read(A,s) Read(A,s)
s:=s*2 s:=s*1
Write(A,s) 250 Write(A,s) 125
Read (B,s) Read (B,s)
s:=s*2 s:=s*1
Write(B,s) 50 Write(B,s) 25
Read(B,t) Read(B,t)
t:=t+100 t:=t+100
Write(B,t) 150 Write(B,t) 125

Lch bieu khong kha tuan t Lch bieu kha tuan t nhng khong
25 phai la lch tuan t

Lch kha tuan t


Lch bieu co the la kha tuan t do hanh vi cu
the cua cac giao tac trong lch bieu.
Bo lap lch khong ieu khien ong thi da
vao hanh vi cu the cua tng thao tac trong
giao tac.
Bo lap lch da tren mot nguyen tac chung
e ieu khien ong thi.

26
Ky hieu
ri(X) : giao tac Ti oc n v d lieu X
wi (X) : giao tac Ti ghi tren n v d lieu X
Giao tac Ti gom mot chuoi cac thao tac oi

27

Mot so khai niem


Lch bieu co the phuc hoi c (Recoverable Schedule)

T1 T2 T1 T2

Begin Tran Begin Tran

Read A Read A

Write A Write A

Begin Tran Begin Tran

Read A Read A

Commit Read B

Read B Commit

Commit Commit

Khi T1 co s co, T2 a commit nen khong the thoat


tnh trang khong the phuc ho28i ung c.
Mot so khai niem
Lch bieu co the phuc hoi c (Recoverable Schedule)

Moi HQT CSDL yeu cau lch bieu phai co the


phuc hoi c.

nh ngha: Mot lch bieu co the phuc hoi


c la lch bieu ma moi cap Ti, Tj, khi Tj oc
n v d lieu va c ghi bi Ti, thao tac
commit cua Ti xuat hien trc thao tac commit
cua Tj.

29

Mot so khai niem


Lch bieu khong rollback day chuyen (Uncascading
rollback Schedule)

T1 T2 T3 Khi T1 fail, phai


rollback T2 va keo
Read A theo T3 phai b
rollback.
Read B

Write A Hien tng mot


transaction rollback,
Read A dan en mot loat cac
transaction khac phai
rollback goi la
Write A

Read A rollback day chuyen


(Cascading rollback).

30
Mot so khai niem
Lch bieu khong rollback day chuyen (Uncascading
rollback Schedule)
nh ngha: Lch bieu khong rollback day chuyen la
lch bieu ma trong o moi cap giao tac Ti, Tj, neu Tj
oc n v d lieu c viet trc o bi Ti, thao tac
commit cua Ti phai xuat hien trc thao tac oc cua
Tj.

Mot lch bieu khong rollback day chuyen la lch bieu


co kha nang phuc hoi c.

31

Mot so khai niem


Tnh tng thch cua 2 thao tac

nh ngha: Hai thao tac Oi, Oj la tng thch neu ket qua cua viec
thc hien ong thi Oi, Oj, giong nh ket qua cua viec thc hien
tuan t Oi, Oj hoac Oj, Oi
Hai thao tac tng thch th khong xung ot nhau.

T1 T2 T1 T2 T1 T2
Read (A) Read (A) Read (A)
Read(A) Read(B) Write(B)

Hai thao tac khong tng thch nhau th xung ot nhau.

T1 T2 T1 T2 T1 T2
Read (A) Write(A) Write(A)
Write(A) Read(A) Write(A)

32
Mot so khai niem
Tnh kha hoan v cua 2 thao tac

nh ngha: Hai thao tac Oi, Oj la kha hoan v


neu ket qua cua viec thc hien Oi, Oj hay Oj, Oi
la nh nhau.

Hai thao tac tng thch th kha hoan v.

33

Mot so khai niem


Hai thao tac truy xuat tren cung n v d lieu, ma
tran tng thch

Read A Write A
Read A 1 0
Write A 0 0

Cac thao tac truy xuat tren cac n v d lieu khac


nhau th tng thch va kha hoan v.

34
Mot so khai niem
Vi mot lch bieu S1 cho trc, ta co the lap lai viec
hoan oi v tr cua hai thao tac khong xung ot lien
ke, cho en khi at c 1 lch bieu tuan t S2, neu
co the. Khi o, lch bieu S1 ban au la lch bieu kha
tuan t.

Moi tac ong en CSDL hoan toan khong thay oi


khi ta thc hien hoan oi v tr cac thao tac khong
xung ot nhau.

35

Mot so khai niem


Hai lch bieu S1 va S2 la tng ng xung
ot neu ta co the chuyen lch bieu S1 thanh
lch bieu S2 bang mot/mot so cac thao tac
hoan oi cac thao tac khong xung ot ke
nhau.

Mot lch bieu la kha tuan t xung ot neu no


tng ng xung ot vi mot lch bieu tuan
t.

36
Xet tnh kha tuan t cua S
S S
T1 T2 T3 T4 T1 T2 T3 T4

1.Read A 1.Read A

2.Read A 3.Write B

3.Write B 2.Read A

4.Write A 4.Write A

5.Read B 5.Read B

6.Read B 7.Read A

7.Read A 8.Write C

8.Write C 6.Read B

9.Write A 9.Write A

Lch bieu S tng ng xung ot vi S nen S la kha tuan t xung


ot S kha tuan t
37

Mot so khai niem

Kha tuan t xung ot la ieu kien u e am


bao tnh kha tuan t. Ngha la, mot lch bieu
kha tuan t xung ot la mot lch bieu kha tuan
t.
Khong oi hoi mot lch bieu phai kha tuan t
xung ot e la lch bieu kha tuan t, nhng
kha tuan t xung ot la ieu kien e nhng bo
lap lch trong cac he thong thng mai bao
am tnh kha tuan t.

38
Mot so khai niem
Kha tuan t xung ot khong la ieu kien can cho tnh kha tuan
t.
S1:w1(Y);w1(X);w2(Y);w2(X);w3(X)
S2:w1(Y);w2(Y);w2(X);w1(X);w3(X)
S1: T3 ghi X, T2 ghi Y
S2: T1, T2 ghi tren X nhng T3 ghi e, T2 ghi Y.
Hai lch bieu tren cho gia tr tren X va Y giong nhau.
S1 tuan t
S2 kha tuan t
nhng khong kha tuan t xung ot do khong the chuyen S2 ve 1
lch bieu tuan t bang cach hoan oi.

Kha tuan t
Kha tuan t
xung ot

39

Mot so khai niem


o th u tien (o th ung o) e kiem tra mot lch
bieu la kha tuan t xung ot hay khong.
Input: Lch bieu S.
Output: S kha tuan t?
Cho S la lch bieu gom cac thao tac cua 2 giao tac Ti
va Tj. Ti u tien hn Tj (Ti <Tj ) neu ton tai cap thao
tac Oin Ti, Ojm Tj khong kha hoan v va Oin thc
hien trc Ojm.
o th u tien co nut la cac giao tac, cung co hng i
t nut i en nut j neu Ti < Tj.
Lch bieu la kha tuan t xung ot neu o th u tien
khong co chu trnh.
Neu o th u tien co chu trnh, lch bieu la khong kha
tuan t xung ot.
40
V du
S1:r2(A);r1(B);w2(A);r3(A);w1(B);w3(A);r2(B);w2(B)
T1 T2 T3
r(A)
r(B)
w(A)
r(A)
w(B)
w(A)
r(B)
w(B)

1 2 3

Lch bieu a cho kha tuan t, tng ng vi lch


tuan t T1< T2 < T3
41

V du
S:r2(A);r1(B);w2(A);r2(B);r3(A);w1(B);w3(A);w2(B)

T1 T2 T3
r(A)
r(B)
w(A)
r(B)
r(A)
w(B)
1 2 3 w(A)
w(B)

Lch bieu a cho khong kha tuan t xung ot.


42
Mot so khai niem
Khi thc hien ong thi cac transaction phat sinh cac
van e nh:
Lch thao tac khong kha tuan t.
e giam bt nhng van e nay, ta co 2 cong cu:
Bo lap lch.
Nghi thc.

43

Mot so khai niem


Bo lap lch (schedulers): se tien hanh lap lch cac
thao tac (thao tac se c thc hien trc, thao tac
nao se c thc hien sau).
Bo lap lch giai quyet cac yeu cau ung o, ep cac
transaction phai ch trong trng hp khong ap
ng c yeu cau lock hoac huy bo cac transaction.

Bo lap lch la thanh phan cua he quan tr CSDL, co


vai tro lam trong tai phan x cac yeu cau co xung
ot.

44
Mot so khai niem
Nghi thc (Protocol): la mot han che tren
chuoi cac bc nguyen t ma mot giao dch co
the thc hien.
V du: Nghi thc giai quyet van e deadlock (yeu
cau cac giao tac lock cac n v d lieu theo 1 th
t co nh nao o), nghi thc am bao tnh kha
tuan t (chien lc lock 2 pha).

45

5. IEU KHIEN ONG THI DUNG KY THUAT


KHOA
Ky thuat khoa n gian: Lock (A), Unlock (A).
Nghi thc khoa 2 giai oan (2PL).
Ky thuat khoa oc/ Ghi: Rlock(A), Wlock(A),
Unlock (A).
Tang cap (upgrading).
Khoa cap nhat (update lock).
Khoa tang/ giam (increment lock)
Khoa tren vdl co kch thc khac nhau.
Nghi thc khoa tren vdl to chc phan cap.

46
Ky thuat khoa n gian
Bo lap lch dung ky thuat khoa yeu cau:
Phai khoa va nha khoa ngoai viec oc/Ghi d lieu.
Viec dung khoa phai tuan theo 2 ieu kien sau:
1. Tnh nhat quan cua giao tac:
a) Giao tac ch co the oc hoac ghi tren n v
d lieu neu trc o co yeu cau lock tren n
v d lieu va cha nha lock.
b) Neu giao tac a lock tren vdl th sau o phai
unlock.
2. Tnh hp le cua lch bieu:
Khong the co 2 giao tac ong thi khoa tren
1 vdl.

47

Ky hieu
Ky hieu:

li(X): Ti yeu cau lock tren vdl X

ui(X): Ti unlock tren tren vdl X

V du: T1 va T2 nhat quan


T1:
l1(A);r1(A);A:=A+100;w1(A);u1(A);l1(B);r1(B);B:=B+100;
w1(B);u1(B);
T2:
l2(A);r2(A);A:=A*2;w2(A);u2(A);l2(B);r2(B);B:=B*2;
w2(B);u2(B);

48
T1 T2 A B
25 25
l1(A);r1(A);
A:=A+100;
w1(A);u1(A); 125
l2(A);r2(A);
A:=A*2;
w2(A);u2(A); 250
l2(B);r2(B);
B:=B*2;
w2(B);u2(B); 50
l1(B);r1(B);
B:=B+100;
w1(B);u1(B); 150

Lch bieu hp le, giao tac nhat quan nhng


lch bieu khon49
g kha tuan t

Nghi thc khoa 2 giai oan


(Two-phase lock Protocol - 2PL)
Phat bieu 2PL: Mot giao tac thc hien c che khoa 2 giai oan
khi giao tac khong thc hien lock nao na sau khi a unlock.

n v
d lieu
gi lock

BOT EOT

Phase lock Phase unlock

50
Nghi thc khoa 2 giai oan (tt)
T1 T2 T3 T4
L(A) L(B) L(B) L(A)
Read(A) Read(B) Read(B) Read(A)
L(B) L(A) B=B-50 UL(A)
Read(B) Read(A) Write(B) L(B)
B:=B+A UL(B) UL(B) Read(B)
Write(B) A:=A+B L(A) UL(B)
UL(A) Write(A) Read(A) Print(A+B)
UL(B) UL(A) A=A+50
Write(A)
UL(A)

T1, T2 thoa nghi thc T3, T4 khong thoa nghi thc


khoa 2 giai oan khoa 2 giai oan
51

2PL
Ta co the chuyen 1 lch bieu S hp le gom cac giao
tac nhat quan va thoa 2PL sang 1 lch bieu tuan t
tng ng xung ot vi S.
Chng minh bang phng phap quy nap. Ch quan tam en
thao tac Read va Write.
S: T1, T2,..., Tn. at Ti la giao tac unlock au tien, ui(X).
Ta hoan toan co the chuyen tat ca cac thao tac Read va
Write cua Ti ra au lch bieu ma khong gap phai 1 thao tac
xung ot nao.That vay:
z Xet wi(Y) nao o cua Ti. Gia s co thao tac wj(Y) cua S i
trc wi(Y):
S: ..., wj(Y)..., uj(Y), ..., li(Y), ..., wi(Y), ...
z V Ti unlock au tien, ui(X) phai ng trc uj(Y):
S: ..., wj(Y),..., ui(X),..., uj(Y), ..., li(Y), ..., wi(Y), ...hoac
S: ..., ui(X)..., wj(Y), ..., uj(Y), ..., li(Y), ..., wi(Y), ...
Ti khong thoa 2PL.
S c viet lai:
(cac thao tac cua Ti) (cac thao tac cua n-1 giao tac con lai)
Phan con lai la lch bieu hp le gom cac GT nhat quan va thoa
2PL bang quy nap ta ket luan S la kha tuan t xung ot.
52
Han che cua PP KT dung khoa
Livelock: tnh trang 1 giao tac ch hoai e
c lam viec tren 1 vdl.
Deadlock: tnh trang hai giao tac c ch
nhau mai e c lam viec tren 1 n v d
lieu ma khong co giao tac nao co the thc
hien trc.

53

2PL: Deadlock & Rollback day chuyen

T1 T2 A B
25 25
l1(A);r1(A);
l2(B);r2(B);
A:=A+100;
B:=B*2;
w1(A); 125
w2(B); 50
l1(B); Denied l2(A); Denied

Tnh trang rollback day chuyen co the xay ra vi lch bieu thoa nghi thc
khoa 2 giai oan.

54
Nghi thc khoa nghiem ngat
(Strict locking)
Khoa nghiem ngat: Tat ca cac khoa oc
quyen cua 1 giao tac bat ky phai gi cho en
khi giao tac commit hoac abort, va lenh
commit/ abort phai c ghi nhat ky tren a.
Lch bieu trong o cac giao tac tuan thu cach
khoa nghiem ngat la lch bieu nghiem ngat
(strict schedule).
Moi lch bieu nghiem ngat la lch bieu khong
rollback day chuyen.
Moi lch bieu nghiem ngat la kha tuan t.
z V lch bieu nghiem ngat tng ng vi lch bieu tuan t
theo th t ma GT commit.

55

Cac bien the cua 2PL


Conservative 2PL: GT phai yeu cau khoa tat
ca cac muc d lieu can thiet TRC khi GT
bat au thc hien.
Khong b deadlock, nhng khong thc te.
Strict 2PL: Sau khi GT a commit/ abort,
cac khoa Ghi mi c nha.
Co the deadlock, nhng am bao lch bieu co the
phuc hoi c. Lch nay c dung pho bien.
Rigorous 2PL: ch c nha tat ca khoa
cuoi GT.
Van phai gi Read lock en cuoi GT, giam tnh
ong thi.
56
Cac phng thc khoa
Shared lock (S)
Shared Lock Read Lock
Khi oc 1 n v d lieu, SQL Server t ong thiet
lap Shared Lock tren n v d lieu o

Co the c thiet lap tren 1 bang, 1 trang, 1 khoa


hay tren 1 dong d lieu.
Nhieu giao tac co the ong thi gi Shared Lock
tren cung 1 n v d lieu.
Khong the thiet lap Exclusive Lock tren n v d
lieu ang co Shared Lock.
Shared Lock thng c giai phong ngay sau khi s
dung xong d lieu c oc, tr khi co yeu cau gi
shared lock cho en het giao tac.

57

Cac phng thc khoa


Exclusive Locks (X)
Exclusive Lock Write Lock
Khi thc hien thao tac ghi (insert, update,
delete) tren 1 n v d lieu, SQL Server t
ong thiet lap Exclusive Lock tren n v d
lieu o.

Exclusive Lock luon c gi en het giao tac.


Tai 1 thi iem, ch co toi a 1 giao tac c
quyen gi Exclusive Lock tren 1 n v d lieu.

58
Nguyen tac khoa
1. Tnh nhat quan cua giao tac
Thao tac oc ri(X) phai i sau sli(X) hoac xli(X) ma khong
co thao tac ui(X) xen vao gia.
Thao tac ghi wi(X) phai i sau xli(X) ma khong co thao
tac ui(X) xen vao gia.
Tat ca cac lock phai c unlock tren cung vdl.
2. Giao tac thoa 2PL
Khong co thao tac ui(Y) nao i trc sli(X) hoac xli(X)
3. Lch bieu hp le
Neu co thao tac xli(X) trong lch bieu th khong the co
xlj(X) hoac slj(X) theo sau, j i ma khong co lenh ui(X)
nao gia.
Neu co thao tac sli(X) trong lch bieu th khong the co xlj
(X) theo sau, j i ma khong co lenh ui(X) gia.

59

Ma tran tng thch


S X
S yes no
X no no
V du:
T1 T2

sl1(A);r1(A);

sl2(A);r2(A);
sl2(B);r2(B);
xl1(B) denied

u2(A);u2(B);

xl1(B); r1(B); w1(B);

u1(A);u1(B);

Lch bieu dung Rlock va Wlock. LB kha tuan t xung ot,


tng ng xung ot vi lch tuan t T2 < T1.
60
Khoa tang cap
Khoa tang cap (Upgrading lock)
Shared lock: than thien
T muon oc X trc, sau o ghi X th trc tien khoa
oc tren X, sau o khi muon ghi th nang cap
(upgrade) khoa oc thanh khoa ghi.
z Giao tac hoan toan co the yeu cau lock tren cung mot
vdl vi nhieu che o lock khac nhau.
Cach nay cho phep tang tnh ong thi.

61

Khoa tang cap


T1 T2
sl1(A);r1(A);
sl2(A);r2(A);
sl2(B);r2(B);
sl1(B);r1(B);
xl1(B) denied
u2(A);u2(B);
xl1(B); w1(B);
u1(A);u1(B);

Neu T1 khoa oc quyen tren B au tien th


T1 va T2 khong the thc hien c nhieu
viec ong thi.
Upgrading lock giup tang tnh ong thi

62
Khoa tang cap & Deadlock
T1 T2
sl1(A);
sl2(A);
xl1(A) denied
xl2(A); denied

63

Cac phng thc khoa


Update Lock uli(X)
Update Lock s dung khi oc d lieu vi d nh ghi tr lai
tren n v d lieu nay.
Uli(X) ch cho phep giao tac Ti quyen oc X, khong cho phep
Ti quyen ghi tren X. Tuy nhien, ch co update lock mi co
the c nang cap len thanh khoa oc quyen sau o, khoa
oc th khong the nang cap.
Update Lock la che o khoa trung gian gia Shared Lock va
Exclusive Lock.
Update lock giup tranh deadlock ma khoa tang cap gap
phai.
Khi thc hien thao tac ghi len 1 n v d lieu th bat buoc
Update Lock phai c nang cap thanh Exclusive Lock.
Tai 1 thi iem ch cho phep 1 giao tac gi Update lock tren
1 vdl

64
Ma tran tng thch
S X U

S Yes No Yes Co the cap lock U tren


X khi a co S lock tren
X No No No X. Nhng khi a co lock
U tren X th khong the
U No No No cap bat c loai lock nao
tren X, v neu cho phep
th lock U khong bao
gi co c hoi nang cap
thanh khoa oc quyen.

65

V du
T1 T2
ul1(A), r1(A);
ul2(A), Denied
xl1(A), w1(A), u1(A);
ul2(A), r2(A);
xl2(A), w2(A), u2(A);

66
Khoa tang/giam
Increment lock ili(X) inci(X)
Y tng:
z Hanh ong tang gia tr / giam gia tr neu
dung khoa oc/ ghi th phai ch nhau.
z That ra chung co the hoan v hoac thc hien

ong thi van cho ket qua khong thay oi.


z Thay the

read (A,t); t:=t+/-c; write(A,t);


Bi INC(A,c) e lam tang tnh ong thi ma
van am bao ung an.

67

Bo sung ieu kien ng dung khoa


khi dung khoa tang/giam
Giao tac nhat quan
GT ch co the thc hien hanh ong tang gia tr X
neu no khoa tang tren vdl X. Khoa tang khong cho
phep oc hoac ghi ong thi.
Lch bieu hp le
Nhieu giao tac co the cung khoa tang tren X.
Ma tran tng thch

S X I
S Yes No No
X No No No
I No No Yes

68
V du

T1 T2
sl1(A), r1(A);
sl2(A), r2(A);
il2(B), inc2(B);
il1(B), inc1(B);
u2(A), u2(B);
u1(A), u1(B);

69

Khoa tren cac vdl co kch thc khac nhau

Ta quan tam en cau truc phan cap cua vdl.


He thong khac nhau dung cac n v d lieu
co kch thc khac nhau e khoa. VD: bo,
block, relation.
Co ng dung, co khi vdl nho se co li, co khi
vdl ln se co li hn.
V du: T oc toan bo quan he, U ghi 1 dong
cua quan he. Khi T va U thc hien ong thi
se xay ra tnh trang sai tren CSDL.
vdl ln th han che viec thc hien ong thi.
vdl nho th quan ly phc tap.
70
Cac tai nguyen co the khoa
Tai nguyen Giai thch

Database Khoa tren toan bo c s d lieu. Ch nen ap dung khi tien


hanh thay oi tren lc o cua CSDL.

Table Khoa tren 1 bang trong c s d lieu. Toan bo cac oi


tng trong bang nay, bao gom tat ca cac dong va tat ca
cac khoa trong cac ch muc trong bang, eu b khoa.

Extent Khoa tren 1 extend (= 8 trang).

Page Khoa tren 1 trang. Tat ca d lieu va cac khoa ch muc


trong trang nay eu b khoa.
Row c a vao SQL Server t version 7.0.
Khoa tren 1 dong d lieu trong 1 bang.

71

Cac tai nguyen co the khoa


Khi 1 n v d lieu con b
khoa th cac n v d lieu CSDL
cac cap cao hn se b
khoa bang khoa Intent
tng ng.
Table ... Table

page ... page

row row
row row
row row

72
Cac phng thc khoa
Intent Locks
Khong phai la 1 che o khoa rieng biet ma c s dung ket hp
vi cac che o khoa khac:
z Shared Lock => Intent Shared Lock (IS)
z Update Lock => Intent Update Lock (IU)
z Exclusive Lock => Intent Exclusive Lock (IX)
Intent lock rat co ch khi d lieu c to chc theo cau truc long
nhau hoac cau truc cay.
Intent Lock c SQL Server t ong thiet lap, khong the c
yeu cau thiet lap Intent Lock mot cach tng minh.
Khi 1 n v d lieu thanh phan b khoa, cac n v d lieu cap
cao hn cung se b khoa bang Intent Lock tng ng.
Muc ch:
z Khong cho GT khac lock vdl cap cao hn khi khong the lock
vdl cap thap hn.
z Tang toc o cua he thong v khong can kiem tra lock tat ca
cac vdl cap con khi muon khoa vdl cap cao hn.
73

Nguyen tac
1. e at khoa S hoac X tren 1 vdl, phai bat
au tai goc.
2. Neu v tr hien tai la v tr muon khoa,
khong can i tiep. Yeu cau khoa S hoac X
tai vdl hien tai.
3. Neu vdl muon khoa con cap di, th:
1. at khoa canh bao tai node hien tai.
1. Neu yeu cau Slock th at IS tai node hien tai.
2. Neu yeu cau Xlock th at IX tai node hien tai.
2. i tiep theo hng en c vdl can tm (theo
cay con cha vdl can khoa).
4. Lap lai bc 2 va 3.
74
Cac phng thc khoa
Intent Locks

CSDL

IXLock Table ... Table

page ... page


IXLock
row row
row row
row row

XLock

75

Ma tran tng thch

IS IX S X
IS Yes Yes Yes No
IX Yes Yes No No
S Yes No Yes No
X No No No No

76
Khoa tren vdl co kch thc khac nhau

Co HQT ap dung ky thuat khoa tren kch


thc vdl ong.
HQT CSDL ho tr nhieu kch thc vdl khac
nhau. VD: record, page, file,...
Kch thc vdl c dung cho giao tac thay oi
t ong e phu hp vi giao tac.
Co the t ong nang cap khoa t vdl la record
thanh page thanh file neu giao tac ang khoa
tren mot so phan tram (kha ln) nao o cac
record hoac page cua file.

77

Kiem tra tnh kha tuan t


Vi lch bieu co Rlock va Wlock
Input: LB S gom T1, T2, ..., Tk
Output: S co kha tuan t hay khong
1. Neu Ti Rlock tren vdl X, tiep theo Tj yeu cau
Xlock tren X, th co cung Ti Tj.
2. Ti khoa Xlock tren X, Tj yeu cau Xlock tren X,
co cung t TiTj.
3. Tm la GT gi Slock tren X sau khi Ti nha khoa
oc quyen tren X, nhng trc khi Tj khoa
Xlock tren X, (neu khong co Tj th Tm la giao
tac yeu cau Slock tren X sau khi Ti nha khoa
tren X), th co cung t Ti en Tm.
4. Neu o th co chu trnh th S khong kha tuan t.
Ngc lai th S kha tuan t va co the tm c
lch tuan t tng ng.
78
Ky thuat khoa oc / Viet
T1 T2
RL(A)
Read(A)
UL(A)
RL(B) B1 T1 T2
Read(B)
UL(B)

WL(A) B2 T1 T2 G
Read(A)
A:=A+B

Write(A)
B3 G co chu trnh => S khong
UL(A)
kha tuan t
WL(B)
Read(B)
B:=B+A
Write(B)
UL(B)
79

Nghi thc khoa phan cap


Dung cho d lieu co cau truc cay.
VD: B-tree.
vdl phu hp e khoa la node.
Dung 2PL khong phu hp, v tat ca eu bat
au t root, va ch cho phep 1 giao tac
khong phai la ch oc truy cap cay tai 1 thi
iem.

80
Nghi thc khoa phan cap
1. GT co the bat au khoa tai node bat ky cua cay.
2. Muc X ch co the b khoa bi Ti neu Ti a khoa
node cha cua X trc o.
3. Co the nha khoa tren 1 node bat c luc nao.
4. GT khong the nha lock tren 1 node roi lock tr lai
node o, ngay ca khi van con khoa node cha.

Lch bieu hp le gom cac GT nhat quan va tuan


theo nghi thc khoa phan cap tren th kha tuan t.
Lch tuan t tng ng: Ti< Tj neu trong S, GT
Ti va Tj khoa cung 1 node va Ti khoa node o
trc.

81

T1 T2 T3

V du l1(A), r1(A);
l1(B), r1(B);
l1(C), r1(C)
w1(A), u1(A);
l1(D), r1(D)
w1(B), u1(B)
l2(B), r2(B);

A l3(E), r3(E);

w1(D), u1(D);

B C w1(C), u1(C);

l2(E),

D E l3(F), r3(F)
w3(F), u3(F);
l3(G), r3(G)
w3(E), u3(E);
F G
l2(E), r2(E)

w3(G), u3(G);
w2(B), u2(B);
w2(E), u2(E);
82
Kien truc bo lap lch
dung phng thc khoa
Ban than giao tac khong t yeu cau khoa.
Viec chen thao tac khoa vao trc cac thao
tac oc/ Ghi la cong viec cua Bo lap lch.
Bo lap lch nhan yeu cau read, write, commit hay
abort t giao tac.
Bo lap lch lu thong tin ve viec quan ly khoa
trong lock table.
Giao tac khong t nha khoa. Bo lap lch nha
khoa khi bo quan ly giao tac (Transaction
Manager - TM) bao cho Bo lap lch biet giao
tac a commit hay abort.

83

Kien truc bo lap lch


dung phng thc khoa

Cac giao tac

Read (A);
Write (B);
Commit(T); ...

Scheduler, Phan I

Lock
Lock (A); Read (A);
table

Scheduler, Phan II

Read (A); Write (B);

84
Nguyen tac cap khoa
1. Phan I Chon phng thc lock phu hp e chen vao trc
cac thao tac Read, Write, Increment, update. Co nhng
phng thc khoa nao la tuy thuoc vao HQT CSDL.
2. Phan II Nhan output cua Phan I va thi hanh:
T delayed: v cha c ap ng yeu cau lock, se chen cac
thao tac cua giao tac o vao hang ch.
T not delayed: yeu cau lock c ap ng:
Neu la thao tac truy cap CSDL th c HQT thc hien.
Neu la thao tac lock, Bo lap lch kiem tra lock table xem co the
cap lock khong.
z Neu c, cho phep lock va cap nhat lock table vi lock va cap.
z Neu khong, them 1 muc vao lock table, T delayed, cho en khi lock
c cap.
3. Khi T commit hoac abort, TM bao cho Phan I biet va nha tat
ca cac lock gi bi T. Neu co GT nao ang ch nhng lock
nay, Phan I se bao cho Phan II biet.
4. Khi nhan thong bao ve cac khoa tren X a nha t Phan I,
Phan 2 cap khoa cho T ang ch tren X, T thi hanh tiep cho
en khi thi hanh xong hoac tiep tuc ch v khoa cha the
c ap ng.

85

Bang quan ly khoa (Lock table)


Element Info
Group mode: U

Waiting: Yes
A
List

Tran Mode Wait? Tnext Next

T1 S No

T2 U no

T3 X yes

86
Bang quan ly khoa
N: la mot quan he lu thong tin ve 1 n v d lieu
va thong tin cac khoa ang yeu cau tren vdl o.
Group mode: cha khoa nghiem ngat nhat trong cac
khoa ang yeu cau tren A. Can c vao khoa nay e
grant/ deny yeu cau khoa tren A. Vi mo hnh
Shared-Exclusive-Update th group mode se la:
S neu ch co mot/ mot so shared lock ang c gi.
U neu co 1 update lock va co the co shared lock.
X neu co 1 exclusive lock va khong co lock nao khac.
Waiting bit: cho biet co t nhat 1 GT ang ch lock
tren A.
List: cac GT ang gi lock tren A hoac ch lock tren
A:
Ten GT ang gi lock hoac ang ch.
Mode cua lock.
Tnh trang la ang gi lock hay ang ch lock.
87

Quan ly yeu cau lock


T yeu cau lock A:
Neu cha co muc A tren lock table
Tao 1 muc mi va yeu cau c ap ng.
Ngc lai
Dung thong tin group mode e quyet nh cap lock hay
khong. Neu khong xung ot th cap, neu xung ot se
denied va them vao list ten giao tac T, kieu lock, va Wait?
= Yes
Quan ly unlock
T unlock A:
z Xoa T trong danh sach lien quan en A.
z Cap nhat lai Group mode cho phu hp.
z Cap mot/ mot so lock cho cac GT ch
First-come-first-served: no starvation.
Priority to shared lock: cap tat ca cac shared lock ang
ch, sau o mi en update lock (neu co). Ch cap
exclusive lock neu khong co T nao ch vi mode khac. Co
starvation, khi ch U hoac X lock.
Priority to updating: u tien cac GT gi Ulock va ang ch
Xlock.
88
6. MC CO LAP CUA GIAO TAC

89

Cac mc o co lap
(Isolation levels)
Mc o co lap cua 1 giao tac quy nh mc o nhay
cam cua 1 giao tac oi vi nhng s thay oi tren
CSDL do cac giao tac khac tao ra.
Mc o co lap cua giao tac quy nh cach thc lock
va thi gian gi lock tren n v d lieu ma giao tac
co truy cap.
Cac mc o co lap c SQL Server ho tr:
Read Uncommitted.
Read Committed (default).
Repeatable Read.
Serializable.

90
Cac mc co lap

Moi transaction eu lam viec 1 trong 4


mc o co lap neu tren.
Lenh T-SQL thiet lap mc o co lap:

SET TRANSACTION ISOLATION LEVEL


<READ COMMITTED|READ
UNCOMMITTED|REPEATABLE
READ|SERIALIZABLE>

91

Read Uncommitted
Khong thiet lap Shared Lock tren nhng n v d lieu can oc,
thiet lap Exclusive lock khi ghi.
Khong b anh hng bi nhng lock cua cac giao tac khac tren
nhng n v d lieu can oc.
Khong phai ch khi oc d lieu (ke ca khi d lieu ang b lock bi
giao tac khac).
u iem:
Toc o x ly rat nhanh.
Khong can tr nhng giao tac khac thc hien ddoc d lieu.
Khuyet iem:
Cac van e gap phai khi x ly ong thi: Dirty Reads, Unrepeatable
Read, Phantoms.
Nhan xet:
Ch nen dung e oc d lieu trong trng hp can d lieu tong
quan ve CSDL, v du nh tao nhng bao cao ve tnh hnh chung.
Khong dung khi can oc nhng so lieu chnh xac.
92
Read Committed
La MCL mac nh cua SQL Server
Tao Shared Lock tren vdl c oc, Shared Lock c giai phong
ngay sau khi oc xong d lieu => Giai quyet van e Dirty Reads
Tao Exclusive Lock tren vdl c ghi, va gi cho en het giao tac
u iem:
Giai quyet van e Lost update, Dirty Reads
Shared Lock c giai phong ngay, khong can phai gi cho en het
giao tac nen khong ngan can thao tac cap nhat cua cac giao tac
khac.
Khuyet iem:
Cha giai quyet c van e Unrepeatable Reads, Phantoms
Phai ch khi cha the c ap ng yeu cau lock tren n v d
lieu ang b gi lock bi giao tac khac.

93

Repeatable Read
Repeatable Read = Read Committed+ Giai quyet Unrepeatable
Reads
Tao Shared Lock tren vdl c oc, Shared Lock c gi cho
en het giao tac => Khong cho phep cac giao tac khac cap nhat
tren vdl nay.
Tao Exclusive Lock tren vdl c ghi, Exclusive Lock c gi
cho en het giao tac.
u iem:
Giai quyet van e lost update, dirty Read va Unrepeatable Read
Khuyet iem:
Cha giai quyet c van e Phantom.
Phai ch khi cha the c ap ng yeu cau lock tren n v d
lieu ang b gi lock bi giao tac khac.
Cac giao tac khac khong c phep cap nhat tren nhng n v
d lieu ang b gi Shared Lock.
Van cho phep Insert nhng dong d lieu thoa man ieu kien thiet
lap nhng Shared Lock => Phantoms

94
Serializable
Serializable = Repeatable Read + Giai quyet Phantom

Tao Shared Lock tren vdl c oc, Shared Lock c gi cho en


het giao tac => Khong cho phep cac giao tac khac cap nhatren vdl
nay.
Khong cho phep Insert nhng dong d lieu thoa man ieu kien thiet
lap nhng Shared Lock.
Tao Exclusive Lock tren vdl c ghi, Exclusive Lock c gi cho
en het giao tac.
u iem:
Giai quyet c ca 4 van e cua TXT.
Khuyet iem:
Phai ch khi cha the c ap ng yeu cau lock tren vdl
ang b gi lock bi giao tac khac.

95

Th nghiem cac mc co lap


Vao SQL Query Analyzer, tao 2 connection tren 2 ca so rieng
biet trong Query Analyzer, moi connection ng vi 1 giao tac.
Trong moi giao tac, s dung lenh WAITFOR DELAY e yeu cau 1
giao tac tam dng x ly. Cu phap:
WAITFOR DELAY hh:mm:ss
Gia s co bang SINHVIEN (MASV, TEN)

MASV TEN

1 Nam

2 Toan

3 Tam

96
TH1: a. Read UnCommitted & Read Committed

T1 T2

UPDATE SINHVIEN
SET TEN = Minh
WAITFOR DELAY00:00:20
BEGIN TRAN
SET TRANSACTION ISOLATION
LEVEL READ UNCOMMITTED
SELECT * FROM SINHVIEN WHERE
TEN = Minh
COMMIT TRAN

ROLLBACK TRAN

T2 cho ra tat ca cac do97ng cua bang SINHVIEN

TH1: b. Read UnCommitted & Read Committed

T1 T2

UPDATE SINHVIEN
SET TEN = Minh
WAITFOR DELAY00:00:20
BEGIN TRAN
SET TRANSACTION ISOLATION
LEVEL READ COMMITTED
SELECT * FROM SINHVIEN WHERE
TEN = Minh
ROLLBACK TRAN COMMIT TRAN

T2 khong cho dong nao cua bang SINHVIEN


98
TH2: a. Read Committed & Repeatable Read

T1 T2

BEGIN TRAN
BEGIN TRAN
SET TRANSACTION ISOLATION
LEVEL READ COMMITTED
SELECT TEN FROM SINHVIEN
WAITFOR DELAY 00:00:20
UPDATE SINHVIEN
SET TEN= Minh
COMMIT TRAN
SELECT TEN FROM SINHVIEN
COMMIT TRAN

Ket qua 2 cau SELECT cua T1 la khac nhau.


99

TH3: a. Repeatable Read & Serializable


T1 T2

BEGIN TRAN
BEGIN TRAN
SET TRANSACTION ISOLATION
LEVEL REPEATABLE READ
SELECT TEN FROM SINHVIEN
WAITFOR DELAY 00:00:20
INSERT INTO SINHVIEN
VALUES (4,Tuyet)
COMMIT TRAN

SELECT TEN FROM SINHVIEN


COMMIT TRAN

Ket qua 2 cau SELECT cua T1


100 la khac nhau.
TH3: a. Repeatable Read & Serializable
T1 T2

BEGIN TRAN
BEGIN TRAN
SET TRANSACTION ISOLATION
LEVEL SERIALIZABLE
SELECT TEN FROM SINHVIEN
WAITFOR DELAY 00:00:20
INSERT INTO SINHVIEN
VALUES (4,Tuyet)
COMMIT TRAN

SELECT TEN FROM SINHVIEN


COMMIT TRAN

Ket qua 2 cau SELECT cua T1 la nh nhau.


101

Kiem tra mc co lap?


Read Uncommitted Read committed Repeatable read Serializable
(MCL1) (MCL2) (MCL3) (MCL4)

1. Khoa ghi? 4. Khoa ghi? 7. Khoa ghi? 10. Khoa ghi?


2. Khoa c? 5. Khoa c? Thi 8. Khoa c? Thi 11. Khoa c?
3. Cn xy ra tnh gian khoa c? gian khoa c? Thi gian khoa
trng g ca 6. Cn xy ra tnh 9. Cn xy ra tnh c?
KT? trng g ca trng g ca XLT? 12. Cn xy ra
XLT? tnh trng g
ca XLT?
3.1 Lost Update (TH1) 6.1 Lost Update (TH1) 9.1 Lost Update (TH1) 12.1 Lost Update
(TH1)

3.2 Dirty read 6.2 Dirty read 9.2 Dirty read 12.2 Dirty read

3.3 Repeatable read 6.3 Repeatable read 9.3 Repeatable read 12.3 Repeatable
read

3.4 Phantom 6.4 Phantom 9.4 Phantom 12.4 Phantom

102
7. DEADLOCK
z Khai niem
z Phat hien deadlock
z Ngan nga deadlock

103

Deadlock
Deadlock la tnh trang trong o nhng giao tac co lien
quan khong the thc hien tiep cac thao tac cua no ma
phai ch nhau mai.

ay la tnh trang khong mong muon cac he quan tr


CSDL ieu khien ong thi dung c che khoa.

Cac tnh huong xay ra Deadlock:


Cycle deadlock
Conversion deadlock

104
Cycle deadlock
T1 gi X-lock tren TAB1, T2 gi X-lock tren TAB2
T1 yeu cau X-Lock tren TAB2 => T1 ch T2
T2 yeu cau X-Lock tren TAB1 => T2 ch T1

Giao tac T1 Giao tac T2

Gi X-lock Gi X-lock
tren bang tren bang
TAB1 TAB2

Deadlock
Yeu cau X- Yeu cau X-
lock tren lock tren
bang TAB2 bang TAB1

105

Conversion Deadlock
Giao tac T1 va T2 cung gi S-Lock tren 1 tai nguyen R
Giao tac T1 yeu cau X-Lock tren R => T1 ch T2
Giao tac T2 yeu cau X-Lock tren R => T2 ch T1
Giao tac T1 Giao tac T2

Gi SLock Gi SLock

Yeu cau Yeu cau


XLock XLock

Deadlock

106
Giai phap cho Deadlock
Giai quyet Deadlock
Huy tat ca=> khong phai la cach giai quyet tot.
Huy giao tac gay ra deadlock. Giao tac nao gay ra?
z Dung thi gian qua han (timeout), gii han thi gian giao tac
trang thai kch hoat, thi gian timeout nho qua hoac ln qua eu
khong co li.
z Dung o th ch.

Ngan nga Deadlock


Sap xep cac n v d lieu theo 1 th t co nh va cac giao
tac yeu cau lock tren chung theo th t nay.
z Cac transaction ch lan nhau => deadlock. Cac transaction ch
theo 1 chieu nhat nh => ngan nga deadlock
Do tm deadlock dung nhan thi gian:
Thuat toan WAIT - DIE
Thuat toan WOUND - WAIT

107

o th ch
Khi co tnh trang Deadlock xay ra, he thong huy tnh trang
Deadlock, thc hien luc runtime.
Dung o th ch e phat hien deadlock

Cho S la lch thao tac cua cac giao tac T1, T2, ..., Tn.
o th co nh la cac giao tac
Cung co hng Ti Tj neu Tj phai ch Ti
o th co chu trnh Deadlock

e giai quyet: Huy nh (ng vi giao tac) co nhieu cung vao


ra nhat.

108
V du
T1 T2 T3
Rlock(A)

Rlock(C)

Wlock(E)

Wlock (B)

Rlock(B)

Rlock(B)

Wlock(C)

Wlock(E)

Rlock(D)
Wlock(C)
... ... ...

Co xay ra Deadlock khong?


109

Do tm Deadlock dung nhan thi gian

Nhan thi gian nay ch dung cho viec do tm


deadlock, khong giong nh nhan thi gian
dung cho viec ieu khien ong thi, mac du
he thong co the ang dung pp ieu khien
ong thi da tren nhan thi gian.
Nhan thi gian dung cho viec do tm dead
lock khong thay oi khi giao tac rollback.
Nhan thi gian cho biet thi iem T ang
ch lock tren vdl gi bi giao tac khac.

110
Thuat toan WAIT - DIE
Ti, Tj co timestamp tTi,tTj. Ti yeu cau lock tren 1 vdl ang b gi lock
bi Tj
If tTi < tTj then
Ti ch
Else
Rollback Ti, Ti bat au lai
EndIf

T1 T2 T1 T2

T1 ch T2
rollback

111

Nhan xet TT WAIT- DIE


u tien cho cac transaction gia (bat au trc, timestamp
nho)
Transaction thc hien trc ch phai ch khi tranh chap
lock vi transaction c thc hien sau.
Transaction thc hien sau phai b rollback va thc hien lai
khi tranh chap lock vi transaction c thc hien trc.

112
Thuat toan WOUND - WAIT
Ti, Tj co timestamp tTi,tTj. Ti yeu cau lock tren 1 vdl ang b gi lock
bi Tj

If tTi < tTj then


Rollback Tj
Else
Ti phai ch
EndIf

T1 T2 T1 T2

T2
rollback 113 T2 ch

Nhan xet TT WOUND - WAIT


u tien cho cac transaction gia (bat au trc,
timestamp nho)
Transaction thc hien trc khong bao gi b rollback
hay ch khi tranh chap lock vi 1 transaction thc
hien sau nhng ang gi lock.
Transaction thc hien sau phai ch khi tranh chap
lock vi transaction thc hien trc va phai b
rollback khi b transaction thc hien trc tranh chap
lock.

114
Cac pp quan ly deadlock
PP dung nhan thi gian T ch
Wait - Die Wound-Wait
GT rollback thi Neu GT yeu cau lock gan o th co the rat ln,
iem xay yeu cau thi iem GT bat au, t phan tch va tm chu
lock, la giai oan khi xay ra tnh trang GT trnh se ton nhieu thi
sm, nen co the co gia khong yeu cau c gian.
nhieu GT b rollback lock gi bi GT tre hn, v
hn, va GT rollback vay rollback t xay ra.
thng thc hien t GT b rollback a thc
cong viec hn. hien nhieu viec trc khi
b rollback.
Co khi khong co deadlock xay ra van yeu cau GT Ch yeu cau 1 GT
rollback. rollback khi thc s GT
gay ra deadlock
u tien GT gia, GT gia hn se kill giao tac
tre hn. am bao moi GT eu hoan tat, khong
co tnh trang starvation.

De cai at hn o th ch Cai at kho, ac biet oi


115 vi he thong phan tan.

8. CACH S DUNG CAC PHNG THC


KHOA

116
Chien lc s dung lock
Cac phng thc lock:
FASTFIRSTROW
HOLDLOCK
NOLOCK
PAGLOCK
READCOMMITTED
READPAST
READUNCOMMITTED
REPEATABLEREAD
ROWLOCK
SERIALIZABLE
TABLOCK
TABLOCKX
UPDLOCK
XLOCK

117

V du
SELECT COUNT(*) FROM SINHVIEN WITH (TABLOCK,
HOLDLOCK)

Cac kieu dung chung:


(TABLOCK, XLOCK): khoa oc quyen tren bang.
(ROWLOCK, XLOCK): khoa oc quyen tren dong.

118
V du Gia s STT
GP (STT, NGAY CAP, SOXE, LYDOCAP) ln nhat hien tai =10

So th t bat au t 1, tang dan, khong nhay so


U1 goi CGP U2 goi CGP
Thao tac Thu tuc CGP
S1
S1 Begin Tran
S1
S2 M= Select STT from GP
where STT = (Select S2
max (STT) from GP) 10
S2
10
S3
S3 Insert into GP values 11
(M+1,...) S3
Loi!
11
S4
S4 Commit tran
S4

119

Giai phap KT
GP (STT, NGAY CAP, SOXE, LYDOCAP)

Thao Thu tuc CGP U1 goi CGP U2 goi CGP


tac
S1
S1 Begin Tran
S1
S2 M= Select STT from GP with
(rowlock, xlock) where STT = S2 10
(Select max (STT) from GP)
S2 U2 ch

S3 11
S4
S3 Insert into GP values (M+1,...)
S2 11
S4 Commit tran S3
3 12
S4

120
Bai tap
Moi khi chuyen e
co ma la MAC
Cho CSDL sau: c m, so sv
SV (MASV,TENSV,MANGANH) ang ky khong vt
qua SOSVT
NGANH (MANGANH, TENNGANH)
C (MAC, TENC, SOSVT)
NG_C (MANGANH, MAC)
C_MO (MAC, NAM, HOCKY) Cho biet sinh vien
theo hoc nganh
K (MASV, MAC, NAM, HOCKY) co ma la
MANGANH phai
hoc nhng chuyen
e g

1 bo cua C_MO
Sinh vien ch c
cho biet chuyen e
ang ky cac chuyen
co ma la MAC
e co m
c m trong nam
SV khong c k
NAM, hoc ky
qua 3 chuyen e
HOCKY
trong 1 hoc ky.
121

Giao tac ang ky hoc chuyen e


1. Thu tuc ang_ky_C (@msv, @mc, @nam, @hk)
2. @ng = nganh ma sv ma la @msv theo hoc (SV)
3. @mc thuoc ds cac c ma nganh @ng phai hoc? (NG_C)
Neu khong thoa => Bao loi va Rollback tran
4. KT C @mc co m trong hk @hk, nam @nam
Neu khong m => Bao loi va Rollback tran
5. @max = so sv toi a co the cho k hoc c ma @mc (C)
6. @sosv = em so sv a k c @mc, vao nam @nam, hoc ky @hk
7. Neu @sosv < @max
Insert into K (@msv, @mc, @nam, @hk)
Ngc lai => Bao het cho va Rollback tran
8. Ket thuc thu tuc

a ra giai phap sao cho he thong lam viec ong thi hieu qua.
(Gt thong ke, cap nhat, them mi, ang ky ong thi)

122
Thao luan
v cac gt n gian: them, cap nhat, xoa
v cac giao tac thong ke.
v giao tac ang ky.
Khoa oc quyen tren cac bang lq?
Ap dung mc co lap Serializable?

Hay cho biet giai phap?

123

9. IEU KHIEN ONG THI DUNG KY


THUAT NHAN THI GIAN

124
Y tng
Gan nhan thi gian cho giao tac, ghi lai
nhan thi gian cua giao tac cuoi cung
oc/ghi tren vdl.
Quan ly ong thi theo nguyen tac: am
bao viec thc hien lch bieu a cho tng
ng vi lch tuan t theo th t nhan thi
gian cua giao tac.

125

Nhan thi gian (timestamp) cua giao tac


Nhan thi gian cua giao tac T, TS(T), c
phat sinh bi bo lap lch.
Dung mot gia tr so gan vao cac giao tac e ch
thi iem bat au cua giao tac.
z S dung ong ho he thong hoac bo em e tao nhan
thi gian.
Nhan thi gian co tnh chat duy nhat va tang dan.
(Ti<Tj th TS(Ti) < TS(Tj)
Neu TS(Ti) < TS(Tj) th bo lap lch phai am
bao rang lch bieu a cho phai tng ng
vi lch bieu tuan t Ti < Tj.

126
Nhan thi gian cua n v d lieu

Cac n v d lieu c gan cho 1 gia tr so


goi la nhan thi gian cua n v d lieu.
RT(X), thi iem oc X, la nhan thi gian ln nhat
cua giao tac a oc X.
WT(X), thi iem ghi X, la nhan thi gian ln nhat
cua giao tac a ghi X.
C(X), commit bit cua X, = true neu giao tac va
mi thc hien write(X) a commit.
z C(X) nham tranh tnh trang mot giao tac T oc d lieu ghi
bi giao tac U nhng sau o U abort.

127

Nguyen tac
Truy xuat theo th t nhan thi gian
Khi T oc X th WT(X) phai < TS(T).
Khi T ghi X th WT(X) va RT(X) phai < TS(T).
Abort T neu khong tuan theo th t, T khi tao lai
vi nhan thi gian mi.

128
Van e 1: oc qua tre
T muon oc X
Nhng TS(T) < WT(X)=TS(U) : X a c ghi
sau khi T a c khi tao.
U writes X

T reads X

U bat au

T bat au

129

Van e 2: Ghi qua tre


T muon ghi X
Nhng WT(X) < TS(T) < RT(X): co mot giao
tac khac le ra nen oc gia tr ghi bi T
nhng a oc gia tr khac
U reads X

T writes X

U bat au

T bat au
130
Van e 3: Dirty read
T oc X, X c ghi lan cuoi bi U.
Nhng T va oc X xong, U abort
Tr hoan viec oc cua T cho ti khi U
commit/abort.
Da vao commit bit e nhan biet.
U writes X

T reads X

T bat au

U bat au U abort
131

Van e 4
U bat au sau nhng lai ghi trc
Khi T muon ghi th khong can lam g ca (Qui tac ghi Thomas)
Neu U abort va T commit
Gia tr cuoi cua X le ra la gia tr do T ghi nhng a b bo
qua.
Khi U abort, at C(X) = false, chep lai gia tr cu cua X va
WT(X)
U writes X

T writes X

U bat au

T bat au T commit U abort

132
Nguyen tac
Khi T yeu cau Read/Write, bo lap lch phan
hoi:
ap ng yeu cau.
Abort T va khi tao T vi nhan thi gian mi
(Rollback).
Tr hoan T va sau o quyet nh Abort T hay ap
ng yeu cau

133

Thuat toan ieu khien


1. T yeu cau Read X
Neu TS(T) >= WT(X)
z Neu C(X) = TRUE, ap ng yeu cau
Neu TS(T) > RT(X)
RT(X) := TS(T)
Ngc lai khong thay oi RT(X)
z Neu C(X) = FALSE, tr hoan T cho en khi C(X) tr
thanh TRUE hay giao dch a ghi X abort.
Neu TS(T) < WT(X)
z Rollback T (oc qua tre)

134
Thuat toan ieu khien
2. Giao dch T yeu cau Write X
Neu TS(T) >= RT(X) and TS(T) >= WT(X)
z Ghi gia tr mi tren X
z at WT(X) := TS(T).
z at C(X) := FALSE.
Neu TS(T) >= RT(X) and TS(T) < WT(X)
z Neu C(X) = TRUE, bo qua viec ghi cua T.
z Neu C(X) = FALSE, tr hoan T cho en khi C(X) = TRUE hoac
cac giao tac ghi tren X a abort.
Neu TS(T) < RT(X)
z Rollback T (Ghi qua tre)
3. Neu T commit, bo lap lch at tat ca cac vdl X ma
T ghi gia tr C(X):=TRUE. Cac GT ang ch tren X
co the tiep tuc.
4. Trng hp T rollback, cac GT khac ang ch tren
X yeu cau oc/ ghi lai.
135

V du
T1 T2 T3 T4 A B C

420 400 425 415 tr=tw=0 tr=tw=0 tr=tw=0

ReadA tr=415

ReadA tr =420

WriteB tw=415

WriteA tw=420

ReadB T2
rollback

ReadB tr=425

ReadA

WriteC

WriteA tw=425

136
V du
T1 T2 T3 T4 A B C

510 550 575 500 tr=tw=0 tr=tw=0 tr=tw=0

ReadA tr=500

ReadA tr =510

WriteB tw=500

WriteA tw=510

ReadB tr=550

ReadB tr=575

ReadA tr=550

WriteC Tw=550

WriteA tw=575

137

PP nhan thi gian a phien ban


La mot bien the cua pp nhan thi gian.
Ngoai phien ban mi nhat cua vdl c lu
lai, con co cac phien ban trc o.
Muc ch: giup cho thao tac oc khong bao
gi lam giao tac rollback.
Hoi: Khi nao th thao tac oc lam giao tac
rollback?
PP nhan thi gian a phien ban se lam giao tac T
thay v abort v ly do tren se tiep tuc oc phien
ban (cua vdl can oc) phu hp vi nhan thi
gian cua T.

138
PP nhan thi gian a phien ban
Moi vdl X co nhieu phien ban X1, X2, ..., Xk.
oi vi moi phien ban, lu lai:
Gia tr cua phien ban Xi,
Nhan thi gian oc cua Xi :rT (Xi), la nhan thi
gian ln nhat trong cac GT a oc Xi.
Nhan thi gian ghi cua Xi wT(Xi), la nhan thi
gian cua giao tac a tao ra phien ban Xi.
Khi T c phep ghi tren X, mot phien ban
mi Xk+1 c tao, va:
rT (Xk+1) = wT(Xk+1) = TS(T).
Khi T c phep oc tren Xi, th:
rT (Xi) = max(rT (Xi), TS(T))
139

PP nhan thi gian a phien ban


e bao am kha tuan t, co 2 nguyen tac sau phai
c am bao:
1. T yeu cau ghi tren X:
Trong cac phien ban cua X co nhan thi gian ghi <= TS(T),
chon phien ban Xi co wT(Xi) ln nhat:
z Neu rT (Xi) > TS(T) th cho T rollback.
z Neu rT (Xi) <= TS(T), th:
Neu TS(T) = wT(Xi) th gia tr cua Xi b ghi e.
Neu TS(T) > wT(Xi) th tao phien ban mi Xj, rT (Xj) = wT(Xj)
=TS(T)
2. T yeu cau oc tren X:
Trong cac phien ban cua X co nhan thi gian ghi <= TS(T),
chon phien ban Xi co wT(Xi) ln nhat:
z Tra ve gia tr Xi cho giao tac T.
z rT (Xi) = max (rT (Xi), TS(T))
140
PP nhan thi gian a phien ban
Neu T rollback th tnh trang rollback day
chuyen co the xay ra.
e lch bieu la phuc hoi c, khi cac GT ghi
tren cac vdl ma T a oc commit th T mi
c phep commit.

141

PP nhan thi gian a phien ban


T1 T2 A0 BA0 1 A2 A3
RTS=0 RTS=0
TS(T1)=100 TS(T2)=200 WTS=0 WTS=0

Read(A) WTS=0
RTS=100

Write(A) RTS=0
WTS=100

Read(A) RTS=200
WTS=100

Write(A) RTS=0
WTS=200

Read(B) RTS=200
WTS=0
RTS=200
Read(B) WTS=0

Write(A) RTS=0
WTS=200

Read(A) RTS=200
WTS=100

142
10. ieu khien ong thi dung phng phap
kiem tra hp le (Validation Technique)

143

Phng phap kiem tra hp le


KT khoa va KT nhan thi gian la cac PP
ieu khien ong thi bi quan: gia s cac GT
se ung o va tranh s ung o nay.
KT khoa & KT nhan thi gian ieu khien
thc hien ong thi theo cu phap, v phan
biet oc/ghi.
PP kiem tra hp le la PP ieu khien ong
thi lac quan.

144
Phng phap kiem tra hp le
C cho GT thc hien.
Cap nhat cua GT ch tren bien cuc bo, khong cap
nhat trc tiep tren CSDL cho en khi GT ket
thuc.
Khi GT thi hanh xong, ky xac nhan kiem tra cac
cap nhat cua GT co vi pham tnh kha tuan t
khong.
z Neu khong, GT commit va CSDL c cap nhat that s
t bien cuc bo.
z Neu vi pham, GT abort va khi ong lai.

145

Phng phap kiem tra hp le


GT trai qua cac ky:
1. Ky oc (Read phase):
z Ti oc cac muc dl can thiet.
z Cac cap nhat ch thc hien tren bien cuc bo, khong phai
tren CSDL.
2. Ky Kiem tra hp le (Validation phase):
z Kiem tra co vi pham tnh kha tuan t hay khong e cap
nhat CSDL.
Neu vi pham th rollback.
Neu khong th lam tiep ky 3.
3. Ky ghi (Write phase):
z Ghi t bien cuc bo xuong CSDL.

146
Phng phap kiem tra hp le
Ghi nhan cac nhan thi gian:
START(Ti) thi iem Ti start
VAL(Ti) thi iem Ti thc hien xong phase
validate
Finish(Ti) thi iem T thc hien xong phase
finish.

147

Phng phap kiem tra hp le


Bo lap lch theo doi 3 tap hp:
START = {T, T a start, nhng cha validate
xong} & START (T)
VAL = {T, T validate xong nhng cha xong phase
finish} & START (T) & VAL(T)
z VAL(T) la thi iem e can c vao o ieu khien tuan
t.
FIN = {T, T thc hien xong phase finish} &
START(T) & VAL(T) & FIN(T)
z Bo lap lch khong quan tam en GT T sao cho FIN(T) <
START(U), U la giao tac ang trang thai kch hoat.

Start Validate Finish


148
Kiem tra hp le
Gia s co GT U:
U VAL hoac U FIN
FIN(U) > START (T), ngha la U khong finish trc khi T start.
RS(T) WS(U) = {X} : co the U write X sau khi T read X. Ta
khong chac T a oc gia tr U ghi trc o hay khong, tot hn
het cho T rollback e tranh rui ro T va U khong tuan theo th
t tuan t.
Start

Validate

U Finish

T
149

Kiem tra hp le
Gia s co GT U:
U VAL, ngha la a validate xong
FIN(U) > VAL (T), ngha la U khong finish trc khi T bat
au giai oan validate.
WS(T) WS(U) = {X} : co the T se ghi X trc khi U ghi
X. Ta khong chac ieu nay co xay ra hay khong, tot hn
het la rollback T e khong vi pham tnh tuan t.
Start

Validate
U
Finish

T
150
V du
U: RS(U)={B} W: RS(W)={A,D}
WS(U)={D} WS(W)={A,C}

T: RS(T)={A,B} V: RS(V)={B}
WS(T)={A,C} WS(V)={D,E}

Hay ieu khien ong thi dung phng phap kiem tra hp le cho 4 GT tren.
151

V du (tt)

U co hp le?
V tai thi iem U kiem tra hp le th cha co
giao tac nao hp le nen khong can kiem tra. U
kiem tra hp le thanh cong.

152
V du(tt)

T co hp le?
Khi T kiem tra hp le, U a hp le nhng cha ket thuc. V
vay ta kiem tra tap cac n v d lieu oc va ghi cua T co
giao vi tap vdl ghi cua U hay khong. That vay:
z RS(T) WS(U) = {A, B} {D} =
zWS(T) WS(U) = {A, C} {D} =
Vay T hp le.
153

V du (tt)

V co hp le?
Khi V kiem tra hp le, U a hp le va ket thuc, T a hp le
nhng cha ket thuc. V V bat au trc khi U ket thuc nen
ta phai kiem tra:
z RS(V) WS(U) = {B} {D} =
z RS(V) WS(T) = {B} {A, C} =
z WS(V) WS(T) = {D, E} {A, C} =

Vay V hp le.
154
V du (tt)

W co hp le?
Khi W kiem tra hp le th U, T, V a kiem tra
hp le, W bat au sau khi U ket thuc. Khi o T va
V van cha ket thuc. Ta phai kiem tra:
z RS(W) WS(T) = {A, D} {A, C} = {A}
z RS(W) WS(V) = {A, D} {D, E} = {D]
z WS(W) WS (V) = {A, C} {D, E} = W khong hp le.
155

Nhan xet chung


Lu tr:
z PP lock: lock table, khong gian lu tr t le vi
so lng vdl b lock.
z PP nhan thi gian: can khong gian lu tr cho

nhan thi gian oc va ghi cua tng vdl, bat


ke co ang c truy cap hay khong.
z Validation: can lu lai nhan thi gian va read

set, write set cho tng giao tac ang c kch


hoat. PP nay can khong gian nhieu hn v theo
doi ca nhng giao tac a commit nhng cha
thc hien ghi.
Noi chung, khong gian lu tr gan nh t le vi so
lng vdl c truy cap bi cac giao tac ang
trang thai kch hoat.
156
Nhan xet chung
Ve kha nang giao tac hoan tat ma khong b tr hoan:
Hieu qua cua cac phng phap con tuy thuoc vao s anh
hng lan nhau cua cac giao tac (truy cap cung vdl) la nhieu
hay t.
z PP lock: tr hoan GT nhng tranh c tnh trang rollback,
ngay ca khi s anh hng lan nhau gia cac GT ong thi
la nhieu. PP nhan thi gian va pp Kiem tra hp le khong
tr hoan GT nhng co the gay ra tnh trang GT rollback
lang ph tai nguyen.
z Neu s anh hng lan nhau gia cac GT ong thi la nho
th ca PP nhan thi gian lan Kiem tra hp le eu khong
gay ra rollback. Khi o ca hai lam viec tot hn va ton t
chi ph hn PP lock.
z Khi rollback xay ra, pp Nhan thi gian nhan biet sm hn
va se rollback sm hn pp Kiem tra hp le.

157

Phu luc
Lch bieu dung ky thuat nay la khong rollback
day chuyen v thao tac ghi c thc hien sau
khi giao tac khac commit.

158
TT NHAN THI GIAN

1. T yeu cau Read X


Neu TS(T) >= WT(X)
T oc X
RT(X) := max (RT(X), TS(T))
Neu TS(T) < WT(X)
Rollback T (oc qua tre)
2. Giao dch T yeu cau Write X
Neu TS(T) >= RT(X) and TS(T) >= WT(X)
T ghi X
at WT(X) := TS(T)
Neu TS(T) < RT(X) or TS(T) < WT(X)
Rollback T (Ghi qua tre)
159

Nhan thi gian a phien ban


1. T yeu cau ghi tren X:
Trong cac phien ban cua X co nhan thi gian ghi
<= TS(T), chon phien ban Xi co wT(Xi) ln nhat:
z Neu rT (Xi) > TS(T) th cho T rollback.
z Neu rT (Xi) <= TS(T), th:
Neu TS(T) = wT(Xi) th gia tr cua Xi b ghi e.
Neu TS(T) > wT(Xi) th tao phien ban mi Xj, rT (Xj)
= wT(Xj) =TS(T)
2. T yeu cau oc tren X:
Trong cac phien ban cua X co nhan thi gian ghi
<= TS(T), chon phien ban Xi co wT(Xi) ln nhat:
z Tra ve gia tr Xi cho giao tac T.
z rT (Xi) = max (rT (Xi), TS(T))

160
Het chng 2.

161
KHOI PHUC D LIEU &
AN TOAN D LIEU

Noi dung
Phan 1: Khoi phuc d lieu
1. Muc ch.
2. Cac loai s co.
3. Cac khai niem lien quan khoi phuc d lieu
Tap tin nhat ky GT (transaction log).
iem lu tr (checkpoint).
4. Cac ky thuat khoi phuc d lieu.

1
V du
2 tai khoan A = 1000 , B = 2000
T chuyen 50 t A sang B.
He thong tnh trang:
Ghi A:= A-50
Cha ghi B:= B+50
Khi o mat ien!
Khi co ien tr lai
Neu cho T thc hien lai th A = 900
Neu khong cho T thc hien lai th A = 950 va B =2000.
Vay phai thc hien khoi phuc he thong nh the nao?
3

Mc tieu khoi phuc DL

Khoi phuc c s d lieu la tien trnh phuc hoi


c s d lieu ve tnh trang nhat quan cuoi
cung trc khi co s co xay ra.
Viec khoi phuc d lieu c thc hien bi bo
quan ly khoi phuc d lieu (RM Recovery
Manager).
Khoi phuc d lieu t ong giup giam thieu
viec yeu cau ngi s dung thc hien lai
cong viec (khi xay ra s co).
4

2
Mc tieu khoi phuc DL
Giao tac la n v c ban khi khoi phuc CSDL.
Trong 4 tnh chat cua GT(ACID), RM bao am
2 tnh chat, o la tnh nguyen to (Atomic) va
tnh ben b (Durability).

Cac loai s co
S co cua giao tac
Giao tac b rollback do deadlock hay do bo lap lch yeu
cau (thc hien lai).
Khi xay ra s co giao tac, he thong van bnh thng.
Tan suat: vai lan/phut.
S co he thong
He thong khong the tiep tuc thc hien c na.
Nguyen nhan co the do loi trong bo x ly, b mat ien
hay do loi cua phan mem.
Khi xay ra s co he thong, ch mat nhng thong tin tren
bo nh chnh.
Tan suat: vai lan/thang. 6

3
Cac loai s co
S co thiet b lu tr (media failure)
V du: a b h, au oc b h,
Khi xay ra s co thiet b lu tr, co the b mat 1 phan
hay toan bo d lieu
Tan suat: vai lan/nam
Loi phan mem ng dung
Loi logic cua chng trnh truy cap CSDL, lam cho
viec thc hien giao tac khong thanh cong.

Nhan xet
Du bat c nguyen nhan g, ta can xem xet 2
kha nang khi phuc hoi s co:
1. Mat d lieu tren bo nh chnh (database
buffer).
2. Mat d lieu tren bo nh phu.

4
Sao chep d lieu
DBMS cung cap c che cho phep sao chep c
s d lieu phong khi CSDL b s co.
Sao chep toan bo hoac ch sao chep nhng thay
oi ke t lan sao chep cuoi.
Giai quyet c trng hp mat d lieu tren bo
nh phu.

Nhc li
D lieu tren buffer c ghi (flush) ben b xuong a
moi khi:
Mot cau lenh cu the c thc hien (v du commit).
Hoac ghi t ong moi khi buffer ay.
Viec ghi tng minh t buffer xuong a goi la force-
writing.
Trong khoang thi gian t khi thao tac ghi tren buffer
xay ra en khi thao tac flush t buffer xuong bo nh
phu, neu s co xay ra th RM phai xac nh trang thai
cua GT thc hien thao tac ghi tai thi iem xay ra s
co.
Neu GT a commit, RM phai redo nhng cap nhat cua GT
(rollforward) e am bao tnh ben b.
Neu GT cha commit, RM phai undo (rollback) nhng thay
oi cua GT tren CSDL nham am bao tnh nguyen to. 10

5
V du

S c
T1

T2

T3

T4

T5

Khi s co xay ra, T2 , T3, T4 a commit nen RM phai ghi nhan nhng thay oi cua
chung len CSDL khi ht khi ong lai.
T1 va T5 phai c undo
11

Viec truy xuat d lieu


D lieu c oc t a hoac ghi vao a tnh
theo n v la khoi (block).
Physical block: khoi d lieu c lu tren a.
Buffer block: khoi d lieu c lu tam thi tren
main memory.

Input(A)

A
B
Output(B) B

Disk 12
Main memory

6
Viec truy xuat d lieu
Read (X): gan X cho bien cuc bo xi
Neu khoi DL co cha X cha co trong buffer, thc
hien Input (X)
Gan gia tr X (cha tren buffer block) cho bien cuc
bo xi
Write (X): gan xi cho X
Neu khoi DL co cha X cha co trong buffer, thc
hien Input(X)
Gan gia tr xi cho X (tren buffer block co cha X)
Viec oc/ Ghi tren d lieu c thc hien
gian tiep qua buffer. 13

Quan ly buffer
Buffer:
D lieu mat khi co s co he thong.
Khong gian han che.
Chien lc thay the e nh ra vung trong tren
buffer dung e nap d lieu mi.
FIFO.
LRU.

14

7
Steal & No-force
RM dung hai giai phap sau e ghi d lieu t buffer xuong a:
Steal policy: buffer manager ghi t buffer xuong a trc khi GT
commit. Ngc lai la no-steal, ngha la khong ghi g ca trc khi GT
commit.
Force policy: d lieu t buffer do 1 GT cap nhat lap tc c ghi xuong
a khi GT commit. Ngc lai la no-force.
Vi no-steal, khong phai undo nhng thay oi do giao tac b huy
thc hien v nhng thay oi cha c ghi xuong a.
Vi force, khong phai redo nhng thay oi thc hien bi cac GT
a commit.
Steal policy tranh tnh trang con qua nhieu d lieu tren buffer le
ra nen ghi xuong a.
No force co li khi 2 GT cung lam viec tren 1 block, GT sau
khong phai nap lai block t a len buffer.
Hau het cac DBMS dung chnh sach steal, no-force.
15

DBMS thng cung cap cac tien ch sau e


ho tr cho qua trnh phuc hoi d lieu:
C che backup.
Ghi nhat ky (log).
Checkpoint.
RM.

16

8
Log file
Log file cha cac thong tin ve moi s thay oi tren
CSDL.
Log file c dung cho x ly phuc hoi d lieu. Log file
cha cac thong tin:
Transaction record:
1. ID cua giao tac.
2. Kieu mau tin nhat ky (GT bat au, insert, update, delete, abort,
commit)
3. ID cua n v d lieu b cap nhat.
4. Gia tr cu cua n v d lieu Before Image
5. Gia tr mi cua n v d lieu After Image
6. Con tro e quan ly cac mau tin trong tap tin log.
Checkpoint record.
V lof file la rat quan trong, thng co 2 hoac 3 tap tin
log c tao ra.
17

Nhan xet
Database gom co 2 phan:
DB
Database Vat ly va DB
Buffer
Recovery
Buffer cho Database Manager
Log
Log gom co 2 phan: Log Buffer
Log Val ly va
Buffer cho Log
Khi co s co mat thong tin tren buffer cho database
phai co the da vao log file e phuc hoi d lieu.
Khi co s co mat thong tin tren buffer cho log, ngha
la thong tin ve nhng thao tac cap nhat len CSDL (cha
c thc hien that s len CSDL Vat ly) se b mat va
can phai thc hien lai cac thao tac nay. 18

9
Checkpoint
Han che cua pp khoi phuc dung log
Quet toan bo log thi gian tm kiem tang.
Khong can thc hien lai nhng giao tac a ghi chac chan
len CSDL.
Checkpoint dung e cai thien qua trnh phuc hoi.
Cac checkpoint nh ky xay ra:
Ghi tat ca log record t bo nh chnh xuong bo nh phu.
Ghi xuong CSDL tat ca nhng g thay oi tren buffer
CSDL.
Ghi checkpoint record vao log file.
RM quyet nh thi gian nh ky thc hien
checkpoint, sau m phut hoac sau t giao tac commit
ke t lan checkpoint trc.
19

V du dung checkpoint
Checkpoint

S c
T1

T2

T3

T4

T5

T2 a c ghi xuong bo nh phu nen khong can redo T2.

20

10
Cac ky thuat phuc hoi
Ky thuat phuc hoi dung cach cap nhat tr hoan
(Recovery techniques using deferred update)
Ky thuat phuc hoi dung cach cap nhat tc th
(Recovery techniques using immediate
update)

21

Ky thuat phuc hoi dung cap nhat tr hoan

CSDL khong c cap nhat cho en khi GT a


commit. (Da tren chnh sach no-steal.)
Neu GT gap s co trc khi commit th khong can
thc hien undo g ca.
Nhng can redo nhng cap nhat cho cac GT commit.
PP nay s dung log nh sau:
GT bat au, ghi nhan lai.
Khong thay oi database buffer hoac CSDL.
Ghi log record vao a, ghi dong commit cua GT.
Dung log record e cap nhat that s.
Neu 1 GT abort, bo qua cac thao tac cua GT o va khong
lam g ca.
22

11
Ky thuat phuc hoi dung cach cap nhat
tc th
Cap nhat tren CSDL moi khi co GT thc hien thay
oi CSDL, khong i en khi GT ket thuc.
Khi s co xay ra:
Va redo cac thao tac cap nhat cua GT commit.
Va undo cac thao tac cap nhat cua GT cha commit.
Dung log file nh sau:
GT bat au, ghi nhan lai.
Ghi nhan lai cac thao tac ghi cua GT vao log file.
Ghi nhan lai thao tac ghi tren database buffer.
Database buffer c ghi xuong a khi en luc phai ghi.
Ghi nhan commit, neu GT commit.

23

Nghi thc WAL


(Write Ahead Log)
D lieu t DB Buffer co the c ghi nhan len DB
Vat ly trc khi giao tac c that s commit hay
rollback. Vay neu giao tac phai rollback th can
tien hanh viec undo da vao thong tin ghi tren log
file (vat ly) => Can phai cap nhat tren log file vat
ly trc khi cap nhat len CSDL vat ly
Phai viet vao log file vat ly trc khi viet vao
CSDL vat ly (Write Ahead Log Protocol).

24

12
Nghi thc UNDO
Thc hien cho nhng giao tac cha ket thuc nhng
xay ra s co hay nhng giao tac b rollback

Tnh trang mi Tnh trang cu


UNDO
cua CSDL cua CSDL

DB log
Before Image

25

Nghi thc REDO


Thc hien cho nhng giao tac a commit nhng
cha c ghi nhan tren CSDL Vat ly

Tnh trang cu Tnh trang mi


REDO
cua CSDL cua CSDL

DB log
After Image

26

13
Phuc hoi bnh thng
Sau 1 cai dng bnh thng cua he thong, 1 iem
checkpoint c ghi vao log file nh la mau tin
cuoi cung cua log file
Khi he thong c khi ong lai, neu mau tin cuoi
cung trong log file la checkpoint th thu tuc phuc
hoi bnh thng c goi (noi chung la khong phai
thc hien thao tac undo hay redo nao ca)

27

Phuc hoi khi co s co


Neu mau tin cuoi cung cua log file la checkpoint th khong
can xet tiep
Ngc lai, xac nh checkpoint cuoi cung trong log file.
Xac nh 2 nhom giao tac:
Nhom 1: Giao tac a commit trc khi xay ra s co he thong
Nhom 2: gom 2 loai
o Giao tac cha c commit trc khi xay ra s co he thong
o Giao tac b rollback trc khi xay ra s co he thong
Vi cac giao tac thuoc nhom 1: Ap dung nghi thc Redo
Vi cac giao tac thuoc nhom 2: Ap dung nghi thc Undo

28

14
Mot so quy c
Undo cac thao tac co dau
Can undo that s tren CSDL vat ly da
vao before image
[] Khong can undo tren CSDL vat ly v
thao tac nay sau checkpoint cuoi cung,
nhng thay oi ch mi tren log file
Redo cac thao tac co dau

29

V du BOTi
U1(i)
Bat au giao tac Ti
Cap nhat lan 1 cua Ti
BOTi+1 Bat au giao tac Ti+1
U1(i+1) Thao tac cap nhat th 1 cua giao tac Ti+1
Checkpoint
BOTi+2 Bat au giao tac Ti+2
U1(i+2) Thao tac cap nhat th 1 cua giao tac Ti+2
U2(i) Thao tac cap nhat th 2 cua giao tac Ti
Commit Ti Commit Ti
U2(i+1) [] Thao tac cap nhat th 2 cua giao tac Ti+1
BOTi+3 Bat au giao tac Ti+3
U1(i+3) [] Thao tac cap nhat th 1 cua giao tac Ti+3
U2(i+3) [] Thao tac cap nhat th 2 cua giao tac Ti+3
U2(i+2) Thao tac cap nhat th 2 cua giao tac Ti+2
Commit Ti+2 Commit Ti+2
U3(i+1) [] Thao tac cap nhat th 3 cua giao tac Ti+1
30
S co he thong xay ra

15
Shadow paging
Mot phng phap phuc hoi khac (pp log) la
dung trang bong.
Suot mot qua trnh song cua 1 giao tac, co 2
bang c duy tr:
Bang trang hien hanh (current page table): se b
thay oi khi T thc hien ghi.
Bang trang bong (shadow page table): bang sao
cua table trc khi T thc hien.
Khi giao tac khi ong, 2 page table nay giong
nhau.
31

Shadow paging

Shadow page table Current page table

page on disk
32

16
Het chng 3 phan 1.

33

17
Chng 3
Phc hi d liu v An ton d liu
PHN 2 AN TON D LIU

GV: Phm Th Bch Hu


Email: ptbhue@fit.hcmus.edu.vn

Ni dung

Cc PP iu khin truy cp (Access control)


DAC.
MAC.
RBAC.

1
C 3 kiu iu khin truy cp
DAC (Discretionary Access Control)
Cho bit ch th no c th truy cp kiu g n cc i
tng CSDL.
C nhng nguyn tc 1 ch th c th ty cp quyn
hay ly li quyn cho/ t 1 ch th khc.
MAC (Mandatory Access Control)
nh trc cc nguyn tc ch th (thuc 1 lp) truy cp
trc tip hoc gin tip n cc lp d liu.
RBAC (Role-based Access Control
Vai tr l 1 tp cc quyn. Khng thc hin cp quyn cho
tng ch th m gn cho ch th 1 vai tr, khi ch th s
c tt c cc quyn thuc vai tr .

DAC

2
DAC

iu khin truy cp nhim l iu khin vic


truy cp ca ch th vo i tng thng qua
nh danh ca ch th v cc lut nh trc.
C ch ny c gi l nhim l do n cho
php ch th c th cp quyn cho ch th khc
truy cp cc i tng ca n.

AC cc HQT CSDL thng mi


Tt c cc HQT CSDL thng mi u ci t
DAC.
Cc m hnh cp quyn theo c ch DAC hin ti
u da trn m hnh System R.
System R: do Griffiths v Wade pht trin vo
1976, l mt trong nhng m hnh ra i u tin
cho h qun tr c s d liu quan h.
System R: da trn nguyn l y quyn qun tr
cho ngi s hu.
6

3
System R

Cc i tng c qun l trong m hnh l


table v view.
Cc phng thc truy cp (privilege) l select,
insert, update, delete, drop, index (ch cho
table), alter (ch cho table).
H tr lm vic trn group, khng h tr role.
Dng cu lnh GRANT cp quyn, c ty
chn GRANT OPTION.

System R
S y quyn c th hin thng qua GRANT
OPTION, nu cp quyn cho 1 user bng cu
lnh c GRANT OPTION th user ngoi
vic c th thc hin quyn c cp, cn c
th cp quyn cho cc user khc.
1 user ch c th cp quyn trn 1 quan h cho
trc nu user l ngi s hu quan h hoc
user c ngi khc cp quyn vi cu
lnh c tu chn l GRANT OPTION.

4
Cu lnh GRANT
GRANT PrivilegeList| ALL[PRIVILEGES]
ON Relation | View
TO UserList | PUBLIC
[WITH GRANT OPTION]

C th cp quyn (privilege) trn table v view.


Privilege p dng trn c table (hoc view).
i vi quyn cp nht (update privilege), cn
phi ch nh quyn ny p dng trn ct no.
9

Cu lnh GRANT V d
A: GRANT select, insert ON NHANVIEN TO B
WITH GRANT OPTION;
A: GRANT select ON NHANVIEN TO C
WITH GRANT OPTION;
B: GRANT select, insert ON NHANVIEN TO C;

C c quyn select (t A v B) v quyn insert (t B).


C c th cp quyn select cho cc user khc, nhng C
khng th cp quyn insert.

10

5
Cu lnh GRANT
Vi tng user, h thng ghi nhn li:
A: tp hp cc quyn m user s hu.
B: tp hp cc quyn m user c th y quyn cho ngi
khc.

Khi 1 user thi hnh cu lnh GRANT, h thng s


Tm phn giao ca tp B vi tp quyn trong cu lnh.
Nu phn giao l rng, th cu lnh khng c thi hnh.

11

Cu lnh GRANT V d

A: GRANT select, insert ON NHANVIEN


TO C WITH GRANT OPTION;
A: GRANT select ON NHANVIEN TO B
WITH GRANT OPTION;
A: GRANT insert ON NHANVIEN TO B;
C: GRANT update ON NHANVIEN TO D
WITH GRANT OPTION;
B: GRANT select, insert ON NHANVIEN
TO D;
12

6
Cu lnh GRANT V d

Trong v d trn, hy cho bit:


Cu lnh no c thc thi hon ton?
Cu lnh no khng c thc thi?
Cu lnh no thc thi mt phn?
TR LI:
3 cu lnh u tin thc thi hon ton v A l ngi s hu table.
Cu lnh th 4 khng thc thi v C khng c quyn update trn table.
Cu lnh th 5 thc thi mt phn, B c quyn select, insert nhng B
khng c grant option cho quyn insert nn D ch nhn c quyn
select.

13

Cu lnh Revoke
REVOKE PrivilegeList|
ALL[PRIVILEGES]
ON Relation | View
FROM UserList | PUBLIC
Cu lnh ny dng thu hi quyn cp.
User ch c th thu hi quyn m user cp.
User khng th ch thu hi grant option.
Mt ngi ch c th b thu hi quyn truy xut p khi
tt c nhng ngi cp quyn p cho h u thu hi
quyn p li.

14

7
Cu lnh Revoke V d
A: GRANT select ON NHANVIEN TO C WITH
GRANT OPTION;
A: GRANT select ON NHANVIEN TO B WITH
GRANT OPTION;
C: GRANT insert ON NHANVIEN TO D;
B: GRANT select ON NHANVIEN TO D;
C: REVOKE select ON NHANVIEN FROM D;

Sau cu lnh ny th D vn c quyn select trn bng


NHANVIEN v B vn cha thu hi quyn select ca D.

15

Cu lnh Revoke

Thu hi quyn quy (recursive revocation):


khi ngi dng A thu hi quyn truy xut trn
bng ca mt ngi B th tt c cc quyn m B
gn cho ngi khc u b thu hi.

16

8
Thu hi quyn quy

B F 70 H
10 30 40
A C
20 50 60 G
E

B
10
A 60
C G
20 50
E

17

Thu hi quyn quy

S ph v quyn truy xut m i tng b thu


hi quyn cp.
Thc t khi 1 user A thay i cng vic hay v
tr th ch mun ly li quyn truy xut ca A
m khng mun ly li cc quyn truy xut m
A cp.

18

9
Thu hi quyn quy
Thu hi quyn quy trong System R da vo
nhn thi gian mi ln cp quyn truy xut cho
ngi dng.
Mt bin th ca cch tip cn ny l khng da
vo nhn thi gian, mc ch l trnh thu hi
quyn dy chuyn.
Khi , nu C b B thu hi quyn v C li c quyn
tng ng do ngi khc cp (mc d sau )
th quyn m C cp cho ngi khc vn c gi.

19

Mt bin th ca thu hi quyn quy

B F 70 H
10 30 40
A C
20 50 60 G
E
70
F H
B 40
10
A 60
C G
20 50
E

20

10
Thu hi quyn khng dy chuyn
(Noncascading revoke)
Khi A thu hi quyn truy xut trn B th tt c
quyn truy xut m B cp cho ch th khc
c thay bng A cp cho nhng ch th
ny.

21

Thu hi quyn khng dy chuyn


Ch :
Bi v B c cp quyn truy xut trn i tng t
nhiu ch th (khc A), nn khng phi tt c cc
quyn m B cp u thay bng A cp. V A c xem
l ngi cp thay cho B khi B s dng quyn A
cp cho mnh cp cho nhng ch th khc.
A s l ngi cp cc quyn m B cp sau khi nhn
quyn t A c ch nh WITH GRANT OPTION.
Vi nhng quyn B c cp bi C A, n lt B
cp cho ngi khc th B vn l ngi cp cc quyn
ny.

22

11
Thu hi quyn khng dy chuyn
80
L M
A 40 50
20
B H
30 I 60 70
N

50 80
L M
A
20
B 70
H
30 I 60 70
N

23

Thu hi quyn khng dy chuyn


80
L M
A 40 50
20
B H
30 I 60 70
N

80
L M
A 40 50
20
B H
30 70
I
70 N

24

12
Thu hi quyn khng dy chuyn

Lu rng vi quyn m H cp cho L, sau khi


thu hi quyn, khng c thay I nh l ngi
cp v quyn ny c cp trc khi I cp
quyn cho H.

25

View v s phn quyn da trn ni dung

Trong cc RDBMS, view l mt c ch thng


c dng h tr vic iu khin truy cp
da trn ni dung
Dng cc v t (predicate) gii hn ni dung
d liu cn cp quyn.
Ch nhng b ca quan h tha mn v t c
xem l cc i tng cp quyn.

26

13
View v s phn quyn da trn ni dung

Vic iu khin truy cp da trn ni dung


trong cc RDBMS c thc hin nh sau:
nh ngha 1 view dng cc v t chn ra cc
dng d liu mun cp cho ch th S.
Cp cho S cc quyn trn view.

27

View v s phn quyn da trn


ni dung
V d: gi s ta mun cp quyn cho user B
truy cp ch nhng nhn vin c lng t hn
20000:
CREATE VIEW V_NHANVIEN AS
SELECT * FROM NHANVIEN
WHERE LUONG < 20000;
- GRANT Select ON V_NHANVIEN TO B;

28

14
Cc bc x l truy vn
Parsing
Catalog lookup
Authorization checking
View Composition
Truy vn trn view s c chuyn thnh truy vn trn bng c s thng
qua bc ny.
Kt qu s da trn v t ca cu truy vn v v t nh ngha nn view.
B: SELECT * FROM V_NHANVIEN
WHERE CONGVIEC = Lap trinh vien;

Cu truy vn sau bc view composition:


SELECT * FROM NHANVIEN
WHERE LUONG < 20000 AND
CONGVIEC = Lap trinh vien;

Query optimization
29

Nhn xt
V vic kim tra quyn c thc hin trc bc view
composition nn quyn c kim tra s da trn view ch
khng da trn cc bng c s dng nh ngha view.
View hu ch khi cp quyn trn cc ct ch cn to view gm
cc ct m ta mun cp quyn.
View cn hu ch trong vic cp quyn trn d liu thng k
(d liu sinh ra t cc hm AVG, SUM, )
Ch th truy cp c th cp quyn truy xut hay thu hi trn
view tng t nh trn bng d liu.
Ngi dng mun to View th phi c quyn Select trn bng
d liu.
Nu ngi to View b thu hi quyn (hay cp quyn) trn bng
th cng b thu hi quyn(hay cp quyn ) trn View, v thu hi
nhng ngi dng khc c ngi ny cp quyn.

30

15
View v cp quyn da trn ni dung
Ngi to view: view definer.
Quyn m view definer c trn view ph thuc vo:
Ng ngha ca view hay cc quan h c s dng to nn view.
Quyn m view definer c trn cc bng c s.
Quyn alter v index khng th p dng cho view, nn view
definer khng bao gi c quyn ny trn view.
A: CREATE VIEW V1 (MANV, TONGTIEN)
AS SELECT MANV, LUONG+THUONG
FROM NHANVIEN WHERE CONGVIEC = Lap trinh vien

Thao tc update khng c nh ngha trn trng TONGTIEN ca view


nn A s khng th c quyn update trn field ny.

31

Phn quyn trn view

xc nh quyn m view definer c trn


view, h thng phi:
Tm giao tp quyn m view definer c trn cc
quan h c s vi tp quyn ng vi cc thao tc c
th thc hin trn view.

32

16
Quyn trn view v d

Xt quan h NHANVIEN v gi s A l ngi


to nn quan h NHANVIEN
A: GRANT Select, Insert, Update ON NHANVIEN
to D;
D: CREATE VIEW V1 AS SELECT MANV, LUONG
FROM NHANVIEN;
D: CREATE VIEW V2 (MANV, LUONG_NAM) AS
SELECT MANV, LUONG*12 FROM NHANVIEN;
D c th thc hin tt c cc quyn trn V1 nh l cc
quyn m D c trn quan h NHANVIEN, l
Select, Insert, Update.
Tuy nhin, D ch c th thc hin trn V2 quyn Select
v Update trn ct MANV.
33

Quyn trn view v d

Hon ton c th cp quyn trn view.


Quyn m user c th cp l nhng quyn m user
c with grant option trn cc quan h c s.
V d: user D khng th cp bt c quyn g trn
view V1 v view V2 m D nh ngha v D
khng c ch nh with grant option khi D c
cp quyn.

34

17
Quyn trn view v d

Xt cc cu lnh sau:
A: GRANT Select ON NHANVIEN TO D WITH GRANT
OPTION;
A: GRANT Update, Insert ON NHANVIEN TO D;
D: CREATE VIEW V4 AS SELECT MANV, LUONG FROM
NHANVIEN;
Quyn ca D trn V4 s l:
- Select with Grant Option;
- Update, Insert without Grant Option;

35

DAC Quyn khng nh & Ph nh

System R v hu ht cc HQT dng chnh sch ng.


Vi chnh sch ng, thiu quyn truy xut ng ngha vi vic khng
c quyn truy xut.
Khi ch th truy xut n 1 i tng d liu, HT kim tra
trong danh sch quyn m ch th c truy xut, nu khng c
th truy xut b t chi.
Hn ch: Vic thiu quyn truy xut khng ngn cm ch th s nhn
quyn ny t ch th khc.
V d: x khng c quyn truy xut trn i tng o, nhng trong
trng hp h thng s dng chnh sch phn chia quyn qun tr th ch
th c quyn cp quyn truy xut trn o v tnh cp quyn cho x.
Ngi ta a ra quyn ph nh gii quyt vn rng
buc ny.

36

18
DAC Quyn khng nh & Ph nh

Quyn khng nh: danh sch quyn truy xut c s


dng.
Quyn ph nh: danh sch quyn truy xut khng
c s dng.
Tuy nhin s dng Quyn khng nh v Quyn ph
nh th gy nn xung t.
V d: A c quyn WRITE trn bng NHANVIEN.
A khng c READ trn PHONGBAN.
A khng c WRITE trn thuc tnh LUONG ca
NHANVIEN.
Thng ngi ta gii quyt xung t bng cch u tin
quyn ph nh.
37

DAC Quyn khng nh & Ph nh

Quyn ph nh c thc hin nh l chn quyn.


Khi ch th b gn quyn ph nh trn i tng th
quyn khng nh trn i tng m h c trc b
chn li.
Nu sau ny ch th c rt quyn ph nh th h c
th s dng li quyn khng nh ca mnh trc .
u im:
Th nht nu v tnh gn quyn ph nh cho ngi dng th c th
thu hi li.
Th hai l c th chn quyn truy cp ca ch th trong mt thi
gian bng cch gn quyn ph nh v sau thu hi li.

38

19
Cu lnh DENY

DENY {ALL [PRIVILEGES] | permission[,n]}


{ [(column[,n])] ON { table | view} |
ON {table | view} [( column[,n])] |
{procedure | extended_procedure} }
TO security_account
V d:
DENY SELECT, INSERT, UPDATE
ON NHANVIEN
TO A, B
39

Thu hi quyn cp hoc quyn cm


(Revoking Granted and Denied Permissions)

Dng cu lnh REVOKE:


Ta c th thu hi li quyn khng nh (quyn cp
dng lnh GRANT)
Ta c th thu hi li quyn ph nh (quyn cm
dng cu lnh DENY)
Cu lnh REVOKE ging lnh DENY ch khng
cho thc hin iu g .
Cu lnh REVOKE khc lnh DENY ch REVOKE
s thu li quyn cp, cn DENY s cm mt ch th
(hoc 1 vai tr) thc hin mt quyn g trong tng
lai.
40

20
REVOKE

REVOKE { ALL | statement [ ,...n ] }


FROM security_account [ ,...n ]

VD:
REVOKE CREATE TABLE, CREATE
DEFAULT FROM Mary, John

41

DAC - Rng buc ng cnh

Thc t, ngi dng ch c php truy cp d liu


trong 1 khong thi gian nht nh .
Cn phi c mt c ch h tr vic truy xut trong
khong thi gian cho trc.
V d: c ch ch cho php nhng ngi lm vic bn thi
gian ch c php truy cp d liu vo khong t 9am n
1pm t ngy 1/1/98.
Trong hu ht cc h qun tr c s d liu chnh sch
ny thng trin khai chng trnh ng dng.
Hn ch: khi xc nhn v thay i chnh sch iu khin truy
cp, khng bo m rng chnh sch ny thc thi.
M hnh iu khin truy cp da vo thi gian c
xut gii quyt vn ny.
42

21
DAC - Rng buc ng cnh
Thi gian hiu lc:
Mi quyn truy xut u c khong thi gian hiu lc.
Khi ht thi gian hiu lc th quyn truy xut t ng b thu hi m khng
cn ngi qun tr thu hi.
Chu k s dng quyn truy xut:
Quyn truy xut theo chu k c th l quyn khng nh hay quyn ph
nh. Nu trong cng mt khong thi gian m ngi dng va c quyn
khng nh va c quyn ph nh trn cng mt i tng v cng phng
thc truy cp th u tin cho quyn ph nh.
C ch suy din da vo quy tc suy din
Quy tc suy din biu th rng buc ca cc quyn truy xut theo thi gian.
Quy tc cho php suy ra quyn truy xut mi da s tn ti hay khng tn
ti ca quyn truy xut khc trong khong thi gian xc nh.
Bng cch s dng quy tc suy din p ng c yu cu bo v d
liu mt cch ngn gn v r rng.
V d: Nu hai ngi dng lm chung mt d n th phi cng quyn truy
xut trn cc i tng.

43

DAC - Rng buc ng cnh

Quyn truy xut c nh ngha l mt b gm 5


thuc tnh auth = (s, o, m, pn, g).
Trong :
s (ch th), g (ngi gn) U (danh sch ngi dng).
m M (phng thc truy cp).
o O(i tng).
pn {+ , -}(quyn khng nh, quyn ph nh).
V d:
(B,o1, read, +, C) : C gn quyn cho B c th read i tng o1.
(B, o1, write, -, C) : C khng cho php B c quyn write trn
i tng o1.
44

22
DAC - Rng buc ng cnh

Quyn truy xut theo chu k l b ba ([begin,end], P, auth).


Trong :
- begin l ngy bt u.
- end l ngy kt thc v ln hn ngy bt u hay c th l .
- P l biu thc chu k.
- auth l quyn truy xut.
Quyn truy xut theo chu k th hin quyn truy xut c hiu
lc trong chu k P vi ngy s dng quyn ln hn hay bng tb
(ngy bt u) v nh hn hay bng te (ngy kt thc).
V d 2: A1= ([1/1/94, ], Mondays, (A, o1, read, +, B)) quyn truy
xut ny c B gn, th hin A c quyn read trn i tng
o1 vo cc ngy th hai bt u t ngy 1/1/94.

45

DAC - Rng buc ng cnh


Khi s dng quyn ph nh th c th dn n hin
tng xung t .
V d:
Gi s rng c thm quyn truy xut A2 = ([1/1/95, ],
Working-days, (A, o1, read, -, B)) c gn cng vi quyn
truy xut A1= ([1/1/94, ], Mondays, (A, o1, read, +, B)).
Lc ny bt u t ngy 1/1/95 th A va c quyn ph nh
va c quyn khng nh trn cng i tng o1 v cng
phng thc read.
Hin tng xung t c gii quyt bng cch u tin
quyn ph nh.
Do trong khong thi gian [1/1/94, 12/31/94 ] th A vn
c quyn read trn i tng o1 vo ngy th hai, tuy nhin
t ngy 1/1/95 th A khng c quyn read trn i tng
ny vo cc ngy lm vic k c th hai.
46

23
DAC - Rng buc ng cnh
Quy tc suy din c nh ngha l b ba ([begin, end], P, A <OP> A )
Trong :
begin l ngy bt u.
end l ngy kt thc v ln hn ngy bt u hay c th l .
P l biu thc chu k,
A l quyn truy xut.
A l biu thc Bool ca cc quyn truy xut.
OP l mt trong cc ton t WHENEVER, ASLONGAS, UPON.
Ng ngha ca tng ton t trong quy tc suy din:
([begin, end], P, A WHENEVER A ) : quyn truy xut A c hiu lc vo thi
im t chu k P v t [ tb, te] khi A c hiu lc.
V d:
A1 = ([1/1/95,1/1/96], Working-days, (M, o1, read, B))
R1= ([1/1/95,], Summer-time, (S, o1, read, +, Bob) WHENEVER (M, o1, read, +, B)).
S ch c read trn i tng o1 vo thi im ma h, t ngy 1/1/95 khi
M c read trn i tng ny.

47

DAC - Nhn xt
u im:
DAC linh ng trong chnh sch nn c hu ht cc
HQT CSDL ng dng.
Khuyt im:
Khng th iu khin c dng thng tin (information
flow control) c th chng li tn cng dng Trojan
Horse.

48

24
RBAC (Role based Access Control)
Hu ht cc HQT CSDL u h tr RBAC.
RBAC c th dng kt hp vi DAC hoc MAC
hoc c dng c lp.
a s cc HQT CSDL ch h tr RBAC n gin
(flat RBAC).

49

Vai tr v nhm
Mc c bn, vai tr c th c xem tng ng nh
nhm.
Mt c quyn c th c gn cho mt hay nhiu nhm
hoc mt hay nhiu vai tr, v mt nhm hay vai tr th
c kt hp vi mt hay nhiu c quyn.
Vic gn mt ngi dng cho mt nhm hay mt vai tr cho
php ngi dng thc thi nhng c quyn ca nhm hay
vai tr .
im khc nhau chnh gia nhm v vai tr l nhm th
coi nh c trng mt tp hp ngi dng v khng l tp
hp quyn hn. Mt vai tr th mt mt l tp hp ngi
dng v mt mt l tp hp quyn hn. Vai tr l i tng
trung gian mang hai tp hp ny li vi nhau.

50

25
RBAC
c p dng vo u nhng nm 1970s.
Khi nim chnh ca RBAC l nhng quyn hn c lin kt vi nhng vai
tr.
Khi s lng ch th v i tng ln s lng quyn hn c th tr nn v
cng ln.
Nu ngi dng c nhu cu cao, s lng cp v thu hi quyn din ra thng
xuyn.
Vi RBAC th c th gii hn trc cc mi quan h vai tr quyn hn, lm
cho vic phn cng ngi dng n cc vai tr c xc nh trc d dng
hn.
Khng c RBAC s kh khn cho vic xc nh quyn hn no c quy nh
n ngi dng no.
Nhng ngi dng c ch nh nhng vai tr thch hp. iu ny lm n
gin cho vic qun l quyn hn.
Trong mt t chc, nhng chc nng cng vic khc nhau c phn thnh
nhng vai tr v ngi dng c ch nh vai tr da vo trch nhim v nng
lc ca h.

51

RBAC Cc m hnh
M hnh RBAC gm 4 m hnh: RBAC0 , RBAC1 , RBAC2 ,
RBAC3.
RBAC0 l nn tng.
RBAC1 thm vo khi nim k tha ca h thng phn cp vai tr. Vai tr c th
k tha quyn hn t vai tr khc.
RBAC2 thm vo cc rng buc.
RBAC3 l tng hp ca 3 m hnh.

52

26
RBAC Cc m hnh
M hnh nn tng RBAC0 th di cng, n l yu
cu ti thiu cho bt k h thng no c h tr RBAC.
M hnh RBAC1 , RBAC2 c pht trin t m hnh
RBAC0 nhng c thm cc im c trng cho tng
m hnh.
RBAC1 thm vo khi nim ca h thng phn cp vai tr
(cc trng thi trong vai tr c th tha k quyn hn t
vai tr khc).
RBAC2 thm vo cc rng buc (p dng rng buc c
th tha nhn cu hnh ca cc thnh phn khc nhau ca
RBAC). RBAC1 , RBAC2 khng lin quan nhau.
RBAC3 l m hnh tng hp ca ba m hnh RBAC0 ,
RBAC1 v RBAC2.

53

V d

54

27
V d

55

V d

56

28
MAC

MAC (Mandatory Access Control)

57

MAC

iu khin truy xut da trn s phn lp ch


th truy xut v i tng d liu.
MAC c dng trong mi trng cn tnh cht
an ninh cao: nh chnh ph, qun i,
MAC c ORACLE ci t.

58

29
MAC
i tng d liu (Object): tables, views, tuples.
Ch th truy cp (Subject): users, user programs.
Security class (or level, or labels)
Top Secret (TS), Secret (S), Confidential (C), Unclassified
(U), where TS > S > C > U
Mi ch th v mi i tng c xp vo mt class.
No read up: Ch th S c th c i tng O nu Class(S)
>= Class(O).
No write down: Ch th S c th Ghi i tng O nu
class(S) <= Class (O).
Tuy nhin, thc t cc h thng khng cho php write up, m ch cho
php write cng mc. Hy kim tra iu ny trn Oracle?

59

MAC

Subjects Objects
writes

writes

TS
writes

writes

TS
S
Information flow

C
reads
reads

C
reads

U
reads

60

30
MAC Nhn xt
Nguyn l v n v d liu ca i tng bo mt.
L ton b CSDL, hay tp tin, cc thuc tnh hay tng
item.
Khng c k thut t ng cho vic gn nhn bo
mt.
Nhiu ngi cng truy cp ti mt thi im.
V p dng chnh sch dng thng tin nn nhng ngi c
mc bo mt cao hn b hn ch ghi xung cc hng mc
d liu c s phn loi thp hn. V d c ch th s1 v s2
c nhn(s1)>nhn(s2), mc d liu d vi nhn(d) =
nhn(s2), v lut thng mi cho rng ghi d liu ln d
ca s2 cn s chp thun ca s1. iu ny th khng thch
hp cho cc ng dng thng mi ca cng ngh CSDL
MLS.

61

MAC

MAC cn c gi l iu khin truy cp a


cp (Multilevel security MLS), ng dng trn
CSDL quan h, c CSDL quan h a cp
(Multilevel Relational Model - MLR).
H thng qun l d liu p ng cc thuc tnh
ca vic bo mt a cp c thit k da trn
m hnh nn tng l Bell v LaPadula.

62

31
MLR
Trong m hnh d liu a cp, nhng mc d liu
v ch th c lp truy cp ring ca chng (hay
cc mc), v d TS(Top Secret), S(Secret),
U(Unclassified) v.v gm s phn loi v s
cho php s dng thng tin b mt(clearance).
Ch th khi truy cp b gii hn bi nhng iu
khin truy cp bt buc, l no read up, no write
down, theo m hnh ca Bell v LaPadula.

63

MLR
Mt quan h a cp c m t bi hai thnh phn:
R(A1,C1,., An,Cn, TC) trong :
Ai l mt thuc tnh trong min Di.
Ci l mt thuc tnh phn loi cho Ai; min ca n l mt tp hp ca
lp truy cp m c th c kt hp vi gi tr ca Ai.
TC l thuc tnh phn loi ca b. (TC=TUPLE-CLASS), l lp truy
cp ln nht trong cc ci.
Thuc tnh phn loi khng chp nhn gi tr rng.

Name CName Dept# CDept# Salary CDept# TC

A Low Dept1 Low 100K Low Low


B High Dept2 High 200K High High
S Low Dept1 Low 150K High High

64

32
MLR
Th hin quan h ti lp c cha tt c d liu m ch th ti lp c thy c. Do , n cha tt
c d liu m cc lp truy cp c.
Tt c cc phn t vi lp truy cp cao hn c, hoc khng th so snh c th c che giu bi
gi tr rng.

Name CName Dept# CDept# Salary CDept# TC


Bob Low Dept1 Low 100K Low Low
Sam Low Dept1 Low null Low Low

Low instance
Name CName Dept# CDept# Salary CDept# TC
Bob Low Dept1 Low 100K Low Low
Ann High Dept2 High 200K High High
Sam Low Dept1 Low 150K High High

High instance
65

MLR

Cc iu kin bt buc:
Quan h a cp phi tha mn cc iu kin sau:
Vi mi b trong mt quan h a cp, cc thuc tnh ca kha chnh
phi c cng lp truy cp.
Vi mi b trong mt quan h a cp, lp truy cp kt hp vi mt
thuc tnh khng phi l kha phi ln hn hoc bng lp truy cp
ca kha chnh.
Cc kha v a th hin:
Trong m hnh quan h chun, mi b c xc nh duy nht bi
kha ca n.
Khi kt hp vi lp truy cp, c th c ng thi cc b vi gi tr
nh nhau ti cc thuc tnh kha nhng vi s phn loi khc nhau,
hin tng ny c gi l a th hin.
66

33
MLR
a th hin:
a th hin xy ra theo hai trng thi sau:
a th hin v hnh: Khi mt ngi s dng mc thp chn d
liu vo mt field m cha d liu ti mc cao hn hay mc
khng th so snh c.
a th hin hu hnh: Khi mt ngi s dng mc cao chn
d liu vo mt field m cha d liu ti mc thp hn.
Name CName Dept# CDept# Salary CDept# TC
A Low Dept1 Low 100K Low Low
B High Dept2 High 200K High High
S Low Dept1 Low 150K High High
B Low Dept1 Low 100K Low Low

Cc b kha l B l a th hin
67

MLR
a th hin v hnh:
Gi s mt ngi s dng mc thp yu cu chn mt b vi
kha chnh ging nhau ti mt b tn ti mc cao hn; DBMS
c ba la chn:
1. Thng bo cho ngi dng rng mt b vi kha chnh ging nhau tn ti
mc bo mt cao v t chi chn vo.
2. Thay th b tn ti mc cao hn vi b mi c chn mc thp hn.
3. Chn b mi mc thp hn m khng thay i b tn ti mc cao hn (tc l
thc th a th hin).
Chn 2) cho php ngi s dng mc thp ghi d liu m anh ta khng nhn
thy v v vy lm mt i tnh ton vn.
Chn 3) l mt la chn hp l; v tm quan trng ca n l gii thiu mt thc
th a th hin.

68

34
MLR

a th hin hu hnh:
Gi s mt ngi s dng mc cao yu cu chn mt b
vi kha chnh ging nhau ti mt b tn ti mc thp
hn; DBMS c ba la chn:
1. Thng bo cho ngi s dng rng mt b vi kha chnh tn ti
ging nhau v t chi chn vo.
2. Thay th b tn ti mc thp hn vi b mi c chn mc
cao hn.
3. Chn b mi mc cao hn m khng thay i b tn ti mc
thp hn (tc l thc th a th hin).
Chn 3) l mt la chn hp l; v tm quan trng
ca n l gii thiu mt thc th a th hin.
69

MLR
Gm 5 rng buc:
Entity integrity (tnh ton vn thc th)
Polyintantiation integrity (tnh ton vn a th hin),
Data-borrow integrity (tnh ton vn d liu-mn),
Foreign key integrity (tnh ton vn kha ngoi)
Referential integrity (tnh ton vn quan h)
V 5 cu lnh (insert, delete, select, update,
UPLEVEL) thao tc trn quan h a cp.

Tham kho: Ravi Sandhu, Fang Chen, The multilevel Relational (MLR) data Model, ACM, 1998.

70

35
Ht chng 3.

71

36
Chng 4

Lu tr d liu vt l &

Cc phng php truy xut

Ni dung
1. Mt s khi nim
2. Cch t chc file v phng php truy
xut
3. Index

1
Cc phng tin lu tr DL
CPU s dng thi hnh CT nhanh
Volatile storage: Cache (RAM)
mt thng tin khi Primary storage
mt ngun
Main memoryy (DRAM)
( ) CPU dng DRAM lm ni load
CT
C + DL, thi hnh
C CT

Flash memory (MP3, USB)

Secondary storage/
Nonvolatile
storage Magnetic disk On-line storage

Optical disk (CD-ROM)


Tertiary storage/
Off-line storage
Magnetic tapes
Gi thnh
Tc

Storage Capacity

nearline offline
1015 tape &
tape
optical
typiccal capacity (bytes))

1013 magnetic
disks
optical
electronic
1011
disks
online
electronic secondary
109 tape
main
107
f
from G
Gray & Reuter
R t
105
cache
103
10-9 10-6 10-3 10-0 103
access time (sec)
4

2
Storage Cost

104 cache
from Gray & Reuter
electronic
online
li
102 main
electronic tape
dollars/MB

magnetic
secondary optical nearline
100 tape &
disks optical

disks
10-2 offline

tape
10-4
10-9 10-6 10-3 10-0 103
access time (sec)

a t (Magnetic disk)

3
V cch qun l a
1 mt a chia thnh nhiu track, 1 track chia
thnh nhiu block (page). 1 cluster = n block.
Dng a t (magnetic disk) lu c s d liu
v:
Khi llng lu
l tr
t ln
l (khng
(kh th lu
l b nh
h chnh)
h h)
Lu mt cch bn b, lu di, phc v cho truy cp v
x l lp li (b nh chnh khng p ng c)
Chi ph cho vic lu tr r.
D liu trn a phi c chp vo b nh chnh
khi cn x l. Nu d liu ny c thay i th s
c ghi tr li vo a.
B iu khin a (disk controller - DC): giao tip
gia a v my tnh, nhn 1 lnh I/O, nh v
u c v lm cho hnh ng R/W din ra.
Block cng l n v lu tr v chuyn d liu.
7

Chuyn d liu
Thi gian trung bnh tm v chuyn 1 block
= s + rd + btt
Seek time (s): DS nh v u c/ ghi ng
track trung bnh khang 7
track, 7-10
10 msec (destop)
(destop), 3
3-8
8
msec (server).
Rotational delay/latency (rd): u c v tr
block cn c, ph thuc rpm, trung bnh khang 2
msec.
Block transfer time (btt): chuyn d liu, ph
th vo
thuc bl
blockk size,
i ttrack
k size,
i v
rpm.
Khi truy xut n cc block lin tip th tit
kim c thi gian.
Mt s k thut tm kim khai thc iu ny.
8

4
Mt s nguyn tc

DBS gim thiu s lng block c


chuyn gia a v MM (main memory)
gim s ln truyy xut a
g
Lu li cng nhiu cng tt cc block d liu
trn MM, tng c hi tm thy block cn truy
xut trn MM.
Buffer l thnh phn ca MM dng cha
cc bn sao (version mi hn) ca cc
block c c ln/ lu xung
a, do
Buffer manager qun l.

Lu tp tin trn a
CSDL c t chc trn a thnh mt/nhiu
tp tin, mi tp tin gm nhiu mu tin, mi mu
tin gm nhiu trng.
Mu
M tinti phi
hi c llu ttr
ttrn

a sao cho
h khi
cn th c th truy cp c v truy cp mt
cch hiu qu.
Cch t chc file chnh (Primary file organization):
cho bit cc mu tin nh v mt cch vt l nh th
no trn a,, t bit cch truyy xut chng.
g

Cch t chc ph (secondary organization/ auxiliary


access structure): truy cp cc mu tin trn file
hiu qu.
10

5
Mc ch
K thut lu tr d liu c ch cho ngi thit k
CSDL, DBA, ngi ci t HQTCSDL
Ngi thit k CSDL, DBA: bit u khuyt im ca
tng k thut lu tr thit k,
k hin thc v thao tc
CSDL trn 1 HQTCSDL c th.
c im ca a t + cch t chc file d liu trn a t
a ra cch thit k CSDL c th lu tr v khai thc
hiu qu.
HQTCSDL thng c nhiu chn la t chc DL, vic
thit k vt l cn chn k thut t chc d liu ph hp cho
yu
cu ng ddng.
Ngi ci t CSDL cn bit k thut t chc DL v
ci t ng, hiu qu cung cp cho DBA v
ngi dng y cc chn la.

11

Ni dung
1. Mt s khi nim
2. Cch t chc file v phng php truy
xut
Mu
tin, kiu
mu tin, tp tin, mu
tin c kch
thc c nh, mu tin c kch thc thay i
nh v file block trn a
File header
Thao tc trn file
Heap file, Sorted file, Hashing technique
3. Index

12

6
Mu tin (Record)
Data records data values/ items
thc th/mi quan h v cc thuc tnh ca
chng
Kiu mu tin: Record type/ Record format = tp cc tn thuc
tnh v kiu d liu
ca tng g thuc
tnh.
VD: struct NHANVIEN {char TENNV[30];
char MANV[9];
int LUONG,
char PHONG[20]}

Kiu d liu (Data type) ca tng thuc tnh cho bit gi tr m


fi ld c
field th nhn
h ly.
l
S: integer(4 bytes), long integer (8 bytes), floating point (4 bytes)
Chui (1 k t 1 byte): chiu di c nh hoc thay i
Boolean (1 byte)
Date/ time (YYYY-MM-DD: 10 byte)

13

Tp tin
T chc tp tin:
Lu nhiu mu tin, c cng kiu hoc khc kiu mu tin.
Gm cc mu tin c cng chiu di (byte) tp tin c kch
((fixed-length
thc cc mu tin c nh g record)) hoc

Gm cc mu tin c chiu di khc nhau tp tin c kch
thc cc mu tin thay i (variable-length record)
TH1: Cng kiu mu tin, c field c chiu di thay i. VD: field kiu
chui
TH2: Do khc kiu mu tin, l trng hp cc mu tin c lin quan
c gom nhm li (clustered) v lu trn cng block truy xut
nhanh.
h h
TH3: Cng kiu mu tin, c thuc tnh c th c nhiu gi tr khc
nhau.
TH4: Cng kiu mu tin, c thuc tnh c/ khng c gi tr.
Ghi ch: TH3 v TH4 khng xy ra khi lu tr d liu trn CSDL
quan h.
14

7
Tp tin
Mu tin c kch thc c nh: d truy xut

MANV TENNV LUONG MAPB

Mu tin c kch thc thay i


TH1:
Dng k t c bit (khng ln ln vi gi tr thuc tnh) kt thc cc trng
c chiu di thay i hoc
Lu kch thc tht tnh bng byte ngay trc gi tr thuc tnh.

001 Nguyen Van A 1000 LAB

TH2: Trc mi mu tin lu kiu mu tin.


TH3: Cn 1 k t c bit ngn cch gia cc gi tr lp li v 1 k t kt
thc.
TH4:
Nu s thuc tnh nhiu nhng s lng cc thuc tnh thc s c gi tr l t
th c th lu cp <field-name, field-value> thay v ch lu field-value.
Dng 3 k t c bit ngn cch: gia field-name v field-value, gia cc
field vi nhau, v kt thc record. C th dng cng 1 k t c bit cho 2 mc
ch u.
Hoc nh m cho kiu d liu ca tng field l field-type, l s nguyn chng
hn, v lu <field-type, field-value> 15

Tp tin

Ta bit: a c chia ra thnh cc block.


Thng:
Kch
thc block B > kch
thc
record R (c
( nh,
tnh bng byte, B>=R)

1 block c cha nhiu mu tin, s lng:


bfr= floor(B/R) records/block
bfr
bfr gi l blocking factor ca file
B - bfr*R l s byte khng dng trong 1 block.

16

8
Lu mu tin vo block

Unspanned

block i record 1 record 2 record 3

block i+1 record 4 record 5 record 6

Spanned
block i record 1 record 2 record 3 record 4 P

block i+1 record 4 (phn cn li) record 5 record 6 P

17

T chc file block trn a

Lin tc: Cc block ca file nh v lin tip trn


cc block ca a.
c file nhanh dng double buffering.
Trong khi CPU x l 1 block th b x l I/O c v chuyn
block k tip vo buffer khc.
M rng file kh khn.
Khng lin tc: tng block ca file cha con tr
tr ti block k tip.
D m rng file.
c file chm.
Kt
hp hai cch trn.
Mi cluster l cc disk block lin tip nhau, cc cluster
lin kt vi nhau.
Cluster cn c gi l file segment hoc extent.

18

9
File Header

Lu li thng tin cn thit c th truy cp


file:
a ch ca cc block ca file
file.
M t nh dng ca record.
Tp tin gm mu tin c chiu di c nh: Field-
length, th t field i vi unspanned record.
Tp tin gm mu tin c chiu di thay i: m kiu
cho field, k t ngn cch, m kiu cho mu tin.
Tm 1 mu tin trn a:
1 hoc 1 s block c chp vo buffer.
CTrnh tm trn buffer dng thng tin ca file header.

19

Thao tc trn file

Cc thao tc trn tp tin


Tm kim 1 mu tin theo iu kin
Thm 1 mu tin
Xa 1 mu tin
Sa 1 mu tin
Ty vo cch t chc tp tin m c cch
thao tc p
ph hp.
p

20

10
Mu tin c chiu di c nh

Xt 1 tp tin gm cc mu tin account


type deposit = record
account-number:
b char(10);
h (10)
branch-name: char(22);
balance: real;
end

Gi s
1 char : 1 byte
Real : 8 bytes
1 mu tin account : 40 bytes
40 bytes u tin l mu tin th 1
40 bytes tip theo l mu tin th 2 21

Mu tin c chiu di c nh (tt)


Mi mu tin c thm 1 bit (tng t .dbf)
=0: Xa
=1:
1: ang dng
File header cha a ch ca mu tin trng
u tin
Danh sch cc mu tin trng (free list)
Lu tr vt l ca cc mu tin account
0 1 10 11 32 33 40 41
1 A-102 Perryridge 400 1 A-305 Round Hill 350
1 A-215 Mianus 700 1 A-101 Downtown 500
1 A-222 Redwood 700 1 A-201 Perryridge 900
1 A-217 Brighton 750 1 A-110 Downtown 600
1 A-218 Perryridge 700 0
0 0
22

11
Mu tin c chiu di c nh (tt)
Hy mu tin
nh du xa vo bit thng tin
a mu tin b nh du xa vo free list

FH

1 A-102 Perryridge 400 1 A-305 Round Hill 350


0 A-215 Mianus 700 1 A-101 Downtown 500
1 A-222
A 222 Redwood 700 1 A-201
A 201 Perryridge 900
0 A-217 Brighton 750 1 A-110 Downtown 600
1 A-218 Perryridge 700 0 A-111 Redwood 800
0 0

23

Mu tin c chiu di c nh (tt)


Thm mt mu tin
Hoc thm vo nhng mu tin b nh du
xa hoc thm vo cui tp tin
Cp nht li free list
FH

1 A-102 Perryridge 400 1 A-305 Round Hill 350


1 A-111 Downtown 700 1 A-101 Downtown 500
1 A-222 Redwood 700 1 A-201 Perryridge 900
0 A-217 Brighton 750 1 A-110 Downtown 600
1 A-218 Perryridge
y g 700 0
0 0

Tm kim
Qut tun t trn tp tin
24

12
Mu tin c chiu di ng
Trong DBMS, mu tin c chiu di ng
dng
Lu tr nhiu loi mu tin trong 1 tp tin
Cc loi mu tin cha cc trng c chiu di
ng
Xt tp tin gm cc mu tin account

type account-list = record


branch-name: char(22);
account-info: array [1..n] of
record
account-number: char(10);
balance: real;
end
end

25

Mu tin c chiu di ng (tt)

Byte-String Representation
Cui mi mu tin c 1 byte k t c bit cho
bit kt thc mu tin

Perryridge A-102 400 A-201 900 A-218 700 -


Round Hill A-305 350 - Brighton A-217 750 -
Downtown A 101
A-101 500 A 110
A-110 600 -
Mianus A-215 700 -
Redwood A-222 700 -

26

13
Mu tin c chiu di ng (tt)

Byte-String Representation
S dng li khng gian trng sau khi xa 1
mu
ti
tin khng
kh hihiu qu

Dn n tnh trng phn mnh

Perryridge A-102 400 A-201 900 A-218 700 -


Round Hill A-305 350 - Brighton A-217 750 -
Downtown A 101
A-101 500 A 110
A-110 600 -
Mianus A-215 700 -
Redwood A-222 700 -

27

Mu tin c chiu di ng (tt)

Byte-String Representation
Tn nhiu chi ph khi chiu di mu tin thay i

Perryridge A-102 400 A-201 900 A-218 700 -


Round Hill A-305 350 A-202
- Brighton
950 A-217 750 -
Downtown A-101 500 A-110 600 -
Mianus A-215 700 -
Redwood A-222 700 -

28

14
Mu tin c chiu di ng (tt)
Fixed-Length Representation
S dng 1 hay nhiu mu tin c chiu di c nh
biu din cho nhng
g mu tin c chiu di ng
g
C 2 k thut
Reserved space
S dng di ln nht ca 1 mu tin no ci t cho tt c
cc mu tin cn li
di ny phi m bo khng bao gi di thm c na
Perryridge A-102 400 A-201 900 A-218 700
Round Hill A-305 350
Mianus A-215 700
Downtown A-101 500 A-110 600
Redwood A-222 700
Brighton A-217 750

29

Mu tin c chiu di ng (tt)


Fixed-Length
Representation
Pointer Anchor block

Cc mu tin c chiu di Perryridge A-102 400


ng mc xch vi nhau Round Hill A-305 350
thng qua danh sch cc Mianus A-215 700
mu tin c chiu di c Downtown A-101 500
nh Redwood A-222 700

C 2 loi blocks trong tp Brighton A-217 750

tin
Anchor block Cha A-201 900

mu tin u tin ca A-218 700

mng account-info Overflow block A-110 600

Overflow block Cha


cc mu tin tip theo
ca mng account-info
30

15
Cch t chc mu tin trn file

Khng c sp: mu tin c chn vo


bt c ch no trng trn file.
c
sp:
mu tin
ti lu
l vo ng
v tr
t
m bo th t theo mt trng no .
Bm (Hashing): nh v mu tin trn thit b
lu tr dng mt hm bm.

31

Heap file
L hnh thc lu tr d liu trong cc
record c lu khng theo th t logic
no c (m l th t thm d liu)
Thng th d liu ca mi quan h c
lu trong 1 file.
Tm kim: duyt.
Thm: nhanh.
Cch thc lu tr v thao tc d liu
d,,
ch thch hp cho tp tin c kch thc
nh, s rt chm khi tp tin c kch thc
ln.

32

16
Sequential file
L hnh thc lu tr d liu trong cc mu tin
c lu tr theo th t ca trng l search key.
Link cc mu tin quan h th t bng pointer
Thch hp cho nhng ng dng c trng lm vic
trn d liu c sp xp (theo search key)
Tm kim: duyt hoc tm tun t.
Nn lu tr vt l theo th t ca search key
gim thiu s block cn phi truy cp. Nhng:
Khi d liu ln, thao tc Insert, Delete phc tp
Insert: nh v -> insert vo overflow block ( anchor block)-> ph
v th t vt l, phi t chc li
1
1
Anchor 2
block 2
4
4 Overflow
block
3 33

Hashing file
Mt hm bm (hash function) c thit lp
trn 1 thuc tnh l search key ca quan h.
Nguyn l: lu u, tm
Chia tp tin thnh cc l (bucket) ty gi tr
ca search key. Mi l c mt s block, link
nhau bi pointer. D liu trong block c t
chc nh heap.
g cc l. Gi tr hm bm ti
B l s lng gi
g
tr tm kim l s nguyn [0,B-1] cho bit l
cha mu tin.Nu kha l chui k t, ta nh
ra nguyn tc chuyn chui k t thnh s.

34

17
Hashing file
Tm kim mu tin kha v
Tnh h(v) bit l, v thc hin tm kim trong l
ny.
Chn
Tnh h(v) bit l. Tm kim khi cui cng ca l,
nu cn ch th chn, cn khng th cp pht 1 khi
khc chn vo cui danh sch ca l h(v).
Xa/ Sa
Tm kim v sa hoc xa (nh du)
Sau khi xa, c th phi thc hin bc hiu chnh
(dn d liu trong khi) gim s lng khi trong
l ny.

35

Clustering file
10 TENPB
MANV TENNV MAPB
KT HCM
N1 A 20
MANV TENNV N2 B 10
N2 B N3 C 20
N5 E N4 D 20
N6 G N5 E 10
N6 G 10
20 TENPB
MAPB TENPB
KT HCM
10 KT HCM
MANV TENNV 20 KD HN
N1 A
N3 C
N4 D
DL lin
quan tch
DL lin quan c
bit, tn
lu cng nhau, tit
khng gian
Clustered tables kim khng gian Unclustered tables
36

18
Clustering file
1 cluster c hnh thnh t vic lu d liu ca mt vi
table chung trn mt vi block.
Cluster key l 1 hoc nhiu field chung ca cc table
tham gia vic gom nhm.
nhm Cluster key c ch nh khi
ngi dng to cluster.
Cc table ny thng c dng chung hoc kt (join
operator ZY) phc v cho nhu cu truy xut d liu.
Vic lu tr ny c ch:
Gim thi gian truy xut a v s block phi c gim
Gi tr ti field l cluster key ch c lu 1 ln
ln, bt k c bao
nhiu record table khc tham chiu n dng ny tit kim
khng gian lu tr (v to mi quan h trn d liu)
T chc dl theo kiu cluster khng nh hng g n
vic to index trn cc table tham gia to cluster.
37

S dng cluster file


Ch nh cluster giai an thit k vt l.
Chn cc table gom nhm:
Cc table ch yu phc v cho truy vn (select), t khi thm mi
(insert) hoc cp nht (update).
Cha d liu c truy vn chung hoc kt vi tn sut cao.
Chn cc field lm cluster key
Cluster key phi c cc gi tr phn bit cc record lin quan
n mi gi tr ca cluster key lp gn y 1 block d liu.
Nu c t dng qu s lm tn khng gian lu tr m hiu qu
khng ng k.
C th nh SIZE khi to cluster, l s byte trung bnh c tnh
c th lu 1 cluster
Nu c nhiu dng qu cng khng hiu qu.
Dng cluster key c qu t gi tr, vd: PHAI, s phn tc dng.

38

19
Index (1)
Dng ch mc cho file ging nh vic dng bn
lit k danh mc (catalog) trong mt th vin.
Thng tin trn catalog c sp xp tm kim
nhanh m khng phi duyt tt c.
V k thut
thut, c 2 lai index c bn:
Index sp th t (ordered indices) da trn cc gi tr
lm index.
Dng PP tm nh phn trn file index.
Index l 1 file c th t gm cc mu tin c chiu di c nh gm 2 field.
Field 1: kha tm kim.
Field 2: con tr tr n cc block.
Index dng k thut bm (hash indices)
nh
h gi
i cc
k th
thut
t dng
d iindex
d
Lai truy xut
Thi gian truy xut (access time)
Thi gian Insert / delete
Khng gian a dng cho index.
39

Index (2)
Dense / Sparse index
Dense index
File d liu c bao nhiu gi tr trn search key th trn
file index c by nhiu record
Mi record ca file index cha 1 gi tr l search key v 1
con tr tr n record u tin trn file d liu c cng
gi tr trn trng search key.
Sparse index
Cc record trn tp tin index ch ng vi mt s gi tr
trn file d liu trn trng search key (ch khng phi
tt c cc gi tr ca search key nh dense index)
tm 1 gi tr, ta tm trong tp tin index 1 mu tin sao
cho gi tr search key ln nht <= gi tr cn tm, v duyt
record xut pht t v tr u tin m pointer ch n.
40

20
V d
Dense index 1

1 5

5 5

7 7

10 7
10
10
Sparse index
1
2
1
3
5
5
7
6
10
7
9
10
41

Index (3)
File c cch t chc ring v c cch thao tc trn file
tng ng.
Bn cnh , ta cn b sung thm cu trc truy cp h tr
truy cp nhanh trn file.
Index l c ch gip HQT CSDL truy xut d liu nhanh
nhanh.
Index key dng ch 1 hoc nhiu trng (field) dng
lm index.
Simple Index: index key ch c 1 field duy nht.
Composite index: index key c nhiu 1 field, nhng <=16
V d:
NOIGD ((,TENNGD,
, , SONHA,, DUONG,, QH,TP)
Q , )
Nhu cu tm nhanh 1 a ch giao dch.
Nu index key l DUONG (simple index) th khng hiu qu
M phi dng SONHA, DUONG, QH, TP (composite index),
v trn cc field ny, gi tr l duy nht

42

21
Index(4)
Mi cu trc index kt hp vi 1 index key c th.
Truy cp n CSDL dng index, ta phi s dng 1 hoc mt
s field l index key trong mnh WHERE ca cu SQL.
Nu l composite index th nn dng nhiu hn 1 field trong
WHERE, khi truy
mnh y xut s hiu
qqu hn.
C bn, bt c field no cng c th l index v c th c
nhiu index trn cng 1 file. Vn l index c mang li hiu
qu hay khng.
Index c hiu qu hay khng cn c vo:
Lai d liu m trn thit lp index.
Gi tr trn index key c phn bit hay khng.
Lai cu SQL c dng.
Khi thi hnh 1 cu SQL nu nhiu hn 20% cc dng d liu trong
1 bng c truy cp n th vic dng index mi c li hn l
khng dng index (table scan).
Cc truy cp khc trn bng, nu cp nht nhiu trn field lm
index s lm chm h thng.
C qu nhiu index s lm chm h thng.

43

Index (5)
Cc thut ng:

Primary index

Single-level
Clustered index
index

Index Secondary index

Multi-level
index

44

22
Primary index
c to trn field lm kha sp xp cho file d liu. Th t vt l ca cc record trn
a cng da trn field ny, v trn cc record c gi tr duy nht.
Nu c nhiu record c cng gi tr trn field dng sp xp, ta s to clustering index trn
field ny.
C 1 mu tin index trong file index ng vi 1 block trong file d liu
File primary index c kch thc nh hn rt nhiu so vi file d liu.
Record u tin trong mi block ca file d liu gi l anchor record hay block anchor.
Ch c
th c 1 primary
i iindex,
d h
hoc
1 clustering
l t i iindex
d ttrn
1 fil
file d li
liu, kh
khng th c

c hai loi index ny trn cng 1 file.

File d liu

File ch mc TEN PHAI DIACHI NGSINH

An

nh
An

Bng
ng
.
.
Bng
.
Bin
Vinh
...

Bnh

Vinh

Vnh

Xun

45

Clustering index
Nu d liu trn data file c sp vt l theo field khng phi l kha (khng
duy nht i vi tng mu tin) th field l clustering field.
Clustering index c to trn clustering field tng tc tm kim cc
mu tin c cng gi tr trn clutering field.
C 1 record trn file index cha 1 gi tr ca clustering field, con tr tr n
block u tin cha g gi tr p
phn bit.

PHG HOTEN MANV PHAI

1 File d liu
2

2
File ch mc
2
1 3

2 3

3 3

4
3
5
3

5
46

23
Secondary Index
Mt secondary index cung cp thm phng tin truy cp file,
ngoi primary index ra.
c to trn field l candidate key v c gi tr duy nht trn mi record,
d liu ca data file khng c sp th t trn field ny.
Cng c th to trn field khng phi l kha v c gi tr trng nhau.
Field 1 l field d liu khng c sp th t ca data file, v cn tm
kim trn .
Field 2 l con tr tr n block u tin cha gi tr, hoc tr n record
cha gi tr.
C th to nhiu secondary index cho 1 file d liu.
TH1: to trn field c gi tr duy nht, field ny cn c gi l secondary
key. HOTEN SOGP SOXE NGAYCAP

60P1-3445
66X7-1234
52X1-1234
52X1-2345 File d liu
52X1-2345
File ch mc 53X2-0123
63P4-5678
53X2-0123
60P1-3445
63X5-0908
61P2-3121
61P2-3121
63P4-5678
52X1-1234
63X5-0908
66X7-1234 47

PHG MANV TENNV PHAI NSINH

Secondary index 3
5
1
6

File ch mc 2
3
4 File d liu
1
1
2
6
3
8
4
4
5
8
6
6
8
5
2
5

5
1
6
3

6
3
8
3 48

24
Nhn xt

File d liu sp File d liu khng sp theo


theo Index field index field

Index field lm kha Primary index Secondary index (Key)

Index field khng l kha Clustering index Secondary index (non key)

1 file c th c nhiu index v c th c nhiu nhu cu tm kim trn file.

Trong SQL Server, c th to ti a l 1 primary index v 249 secondary index.

49

Multi level Index


Index
block 0 Data
block 0

D
Data
block 1

Index
block 1

50

25
Dng index
Nn to index trn cc field c gi tr phn bit,
c truy xut n vi tn sut cao, v kt qu
cu truyy vn l nhiu dng
g d liu.

Truy vn trn mt min gi tr dng cc tan t
BETWEEN, >, >=, <, <=.
Index key l cc trng s dng cho php kt,
hoc trong mnh GROUP BY, ORDER BY.
Khng nn to clustered index trn cc trng
s thng xuyn cp nht.

51

Ht chng 4.

52

26
Chng 5

TI U HA CU TRUY VN

Mc ch
Ti u ha vn tin l tin trnh la chn k
hach thc thi cu vn tin mt cch hiu qu
nht.
Tn t ti nguyn nht.
Hi p nhanh nht.

2
Ni dung
1. Tng quan v x l truy vn
2. Ti u ha truy vn dng Heuristics
3. Ti u ha truy vn dng phng php
c lng chi ph

Cc bc x l vn tin
Query in high-level language (SQL)

Scanning, 1
parsing and
validating
Intermediate form of query
(Relational algebra expression)
Query 2
optimizer
execution plan

Query code
3
generator

Generated code

Runtime
database 4
processor
Result 4
Cc bc x l vn tin
Bc 1
Scan
Xc nh cc t kha ca ngn ng SQL, tn thuc
tnh, tn quan h.
Parse
Kim tra c php cu truy vn.
Validate
Kim tra tn thuc tnh, tn quan h c trong lc
khai bo hay khng.
Khng nhp nhng khi dng cc thuc tnh.
Kiu d liu dng so snh u hp l.
Th hin li cu truy vn: i s quan h, query
tree, query graph. 5

Tm cc b phim m din vin sinh vo nm 1960


SELECT title
Parse tree FROM StarsIn
WHERE starName IN (
<Query> SELECT name
FROM MovieStar
<SFW> WHERE birthdate LIKE %1960);

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <RelName> <Tuple> IN <Query>

title StarsIn <Attribute> ( <Query> )

starName <SFW>

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <RelName> <Attribute> LIKE <Pattern>

name MovieStar birthDate %1960


6
Chuyn Q thnh SQH
Cu truy vn c phn r thnh cc query
block (QB).
QB l n v c bn c th chuyn sang cc
biu thc SQH v ti u ha.
Mt QB cha mt biu thc n SELEC-FROM-
WHERE-GROUP BY HAVING.
Cc cu truy vn lng trong 1 cu truy vn l
cc QB c lp.
Cc ton t gom nhm (max, min, sum, count)
c th hin dng SQH m rng.
7

SELECT HONV, TENNV outer block


FROM NHANVIEN
WHERE LUONG > (SELECT MAX(LUONG)
FROM NHANVIEN
c
WHERE PHG = 5 inner block
)

Bt SQH 2 Bt SQH 1

B ti u ha truy vn (Query Optimizer - QO) s chn la k hoch thc thi


cho tng block.

8
Bc 2
DBMS ra k hoch thc hin cu truy vn
ph hp nht trong cc chin lc thc thi.
Tin trnh ny gi l ti u ha cu truy vn.
Bc 3
B pht sinh m s cho ra m thc thi cu
truy vn theo chin lc va chn.
Bc 4
Thi hnh m pht sinh.

Sp xp ngoi (external sorting)


Sp xp l thut ton chnh dng khi x l truy vn.V d ORDER BY.
Sp xp cng l bc quan trng dng cho php join, union, v bc
loi b dng trng nhau khi thc hin php chiu.
Trnh thc hin sp xp nu d liu c ch mc cho php truy cp
theo th t.
Sp xp ngoi cp n cc thut ton sp xp trn tp tin c s d
liu ln khng th cha trong b nh chnh.
Sort-Merge:
Thut ton sp xp gm 2 bc: sorting v merging.
Sp xp cc subfile (runs) ca tp tin chnh, sau trn cc sorted runs, ri
to subfile ln hn, sp xp ri li trn chng.
Kch thc ca 1 run v s lng run khi u nR ty vo s lng file
blocks b v khng gian buffer trng nB.
Nu nB = 5 v b = 1024 blocks th nR = b/nB , tc l ban u c 205 run. Sau
khi sp xp, 205 sorted run c lu trong file tm trn a.

10
Php chn
C nhiu chn la khi thc hin php chn n.
S1: Tm tuyn tnh: c tng mu tin v kim tra gi tr thuc tnh c tha
iu kin chn hay khng.
S2: tm nh phn: nu iu kin chn l php so snh bng trn thuc tnh
kha dng sp xp file, th tm nh phn s c p dng.
S3: Dng primary index hoc hash key c 1 mu tin nu php chn l
so snh bng trn thuc tnh kha khai bo l primary index hoc l
kha bm.
S4: Dng primary index tm nhiu mu tin: nu iu kin so snh l >,
>=, <, <=, trn trng kha c khai bo l primary index th dng index
tm kim trn iu kin =, sau tm thm cc mu tin tha iu kin
khng bng.
S5: Dng clustering index tm nhiu mu tin: nu iu kin chn l so snh
bng trn trng khng l kha v c khai bo clustering index.
S6: Dng secondary index trn iu kin so snh bng tm 1 mu tin
nu index field l kha hoc tm nhiu mu tin nu indexing field khng l
kha. Cch ny cng c th dng tm kim vi iu kin chn khng
phi l so snh bng.
11

Php chn
iu kin chn phc ni nhau bi AND
Nu thuc tnh trong iu kin chn phc c lin
quan n cc kiu chn n nh cp th
vn dng chng, sau kim tra kt qu tr v
c tha iu kin chn cn li trong mnh
chn phc hay khng.
Nu iu kin chn phc c lin quan n
composite index th vn dng chng trc tip.
Dng pp giao cc record pointer ca tng loi
index lin quan n iu kin chn phc nu
index ang dng gm c record pointer.
12
Php kt R A=B S
J1: Nested-loop join: i vi tng mu tin t trong R, tm tng mu tin s
trong S v kim tra xem hai mu tin c tha t[A] = s[B]?.
J2: Single-loop join: i vi tng mu tin t trong R, dng cu trc ch
mc truy cp trc tip mu tin tha iu kin kt quan h S.
J3: Sort-merge join: nu mu tin trong R v S u c sp xp vt l
trn A v B th php kt din ra rt hiu qu (nu khng th sp xp c
hai trc), c hai tp tin c duyt theo thuc tnh kt, so khp cc
mu tin cng gi tr A v B.
J4: Hash join (kt bm):dng 1 hm bm nh x cc mu tin ca R
vo cc bucket Ri da vo gi tr ca A. Cc mu tin ca S cng c
nh x vo cc bucket Si. Cc Ri v Si c duyt qua t hp cc
b thuc Hi v Si tha iu kin kt.

13

Php chiu dstt (R)


Nu dstt c cha kha ca R th s b kt
qu bng s b ca R ban u.
Nu dstt khng cha kha ca R th loi b
nhng b trng.
Sp xp kt qu ri loi b nhng b trng.

14
Php ton tp hp
Php ton hi, giao, tr i hi 2 quan h
phi kh hp, thng ci t bng cch sp
xp chng theo cng 1 thuc tnh, sau
bng 1 php duyt n gin ln 2 quan h
cng to ra quan h kt qu.
Php tch - cc tn rt nhiu chi ph v
nn trnh nu c th.

15

Cc hm kt hp
Nu tnh trn ton bng th c thc hin
bng vic duyt bng hoc dng index nu
c.
Nu tnh ton trn tng nhm (c group by)
th vic phn nhm c th thc hin bng
cch:
Sp xp.
Bm.
Nu c clustering index th ch vic tnh ton trn
tng nhm c sn.
16
Query tree
L cu trc dng cy tng ng vi mt biu
thc i s quan h.

title
starName=name

StarsIn name
birthdate LIKE %1960
17
MovieStar

K hoch thc thi truy vn


K hoch thc thi mc logic (Logical plan)
th hin mc cao v dng i s, qua cu
trc ngn ng truy vn.
K hoch thc thi mc vt l (Physical plan)
th hin cp thp v lin quan n vic thc
hin, qua cc phng php truy xut.
C nhiu k hoch thc thi truy vn mc vt
l ng vi mt k hoch thc thi mc logic
cho trc.

18
LP v PP

title
starName=name Hash join


StarsIn name SEQ scan index scan

birthdate LIKE %1960 StarsIn MovieStar

MovieStar
19

Cc lut bin i tng ng


1. c1AND c2 ANDAND cn (R) c1(c2( cn (R) ))
2. c1(c2 (R)) c2(c1(R)) giao hon ca
3. L1(L2((Ln(R)))) L1(R)
4. L1,L2, , Ln(c(R)) c(L1,L2, , Ln (R))
5. R1c R2 R2c R1 giao hon ca v x
R1xR2 R2xR1
6. c(R1 R2) (c(R1)) R2
c(R1 R2) (c1(R1)) (c2(R2)) nu c c th vit l c1 AND c2, c1 gm thuc tnh ca R1, c2 gm
thuc tnh ca R2
7. L(R1c R2) (A1, A2 ,An(R1)) c ( B1, B2 ,Bn (R2)) i ch gia v (hoc x) L = {A1, A2, An, B1,
B2, , Bn} Ai R1, Bi R2
8. v c tnh giao hon, nhng php th khng.
9. Tnh kt hp ca : , x, v : (R1 R2 ) R3 R1 (R2 R3)
10. c (R1 R2) (c (R1)) (c(R2)) i ch v (gm , v -)
11. L (R1 R2) L (R1) L (R2)
12. c(R1xR2) R1 c R2 chuyn , x sang
13. Lut DeMorgan NOT (C1 AND C2) (NOT C1) OR (NOT C2)
NOT (C1 OR C2) NOT (C1) AND NOT(C2)

20
Cc lut bin i tng ng
14. (P (R1 - R2) P (R1) R2
15. A1,, An(C(R)) A1,, An(C(A1,, An,Ap (R)))
c1(R1c2 R2) R1c1 c2 R2

21

Gii thut ti u ha bt SQH da trn Heuristics


1. Dng quy tc 1, tch cc php chn i cng nhau c th
t do di chuyn php chn xung cc nhnh ca cy.
2. Dng quy tc 2, 4, 6, 10 lin quan n tnh giao han gia
php chn v cc php ton khc di chuyn php chn
xung nhnh ca cy.
3. Dng quy tc 5 v 9 lin quan n tnh cht giao hon v
kt hp ca cc php 2 ngi, sp xp li cc nt l ca
cy, cc php chn c u tin thc hin trc.
4. Dng 12, kt hp tch -cc v php chn thnh php kt,
nu c th xem iu kin chn l iu kin kt.
5. Dng 3, 4, 7, 11 tch v y cc php chiu xung cc
nhnh.
6. Nhn bit tng nhnh biu din cho mt nhm cc thao tc
c th thi hnh bng chin lc thc hin n.
22
Ti u ha cu truy vn dng vic chn la v
c lng chi ph
c lng chi ph thi hnh mt cu truy vn
cho nhiu chin lc thc thi khc nhau v
chn ra chin lc thi hnh c chi ph thp
nht.
Chi ph cho mt chin lc bao gm:
1. Chi ph truy xut n ni lu tr th cp (vd:
a cng)
2. Chi ph lu tr d liu kt qu trung gian.
3. Chi ph tnh ton: thc hin cc thao tc
trong b nh chnh.
4. Chi ph truyn thng.
23

Ti u ha cu truy vn dng vic chn la v


c lng chi ph
c lng chi ph cho cc chin lc
truy vn khc nhau, cn lu li thng tin cn
thit trong catalog b ti u ha s dng.
S mu tin r.
Kch thc trung bnh ca tng mu tin R.
S khi b.
H s khi bfr.
Ch mc nu c loi g (primary, secondary,
clustering): s mc x (nu l multilevel index),
s block mc u tin ca index bI1
24
Ht chng 5.

25