You are on page 1of 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

Ti u ha cu hi
Bi: Ng Trn Thanh Tho

t vn
Cc ngn ng bc cao ni chung v ngn ng con d liu ni ring khi thc hin trong my u mt rt nhiu thi gian. Do , trc khi thc hin cc cu lnh thuc cc ngn ng cn thit phi bin i hp l v dng tng ng, tc l dng cho cng mt kt qu, gim thi gian tnh ton. Vic lm c gi l "ti u ha" (Optimiztation ). Vic ti u ha khng nht thit phi c ti u trn mi kh nng c th c ca cc cch ci t cc cu hi. C th tha hip gia thut gii phc tp, hoc / v tn km khng gian lu tr vi vic tit kim thi gian x l. Chng ta bit rng, CSDL c th l rt ln, hng chc ngn (nh h qun l cn b - cng chc) hay hng trm ngn (nh vic qun l cc i tng chnh sch - c cng Cch mng), hoc hng triu bn ghi (nh CSDL qun l dn c thnh ph H Ch Minh) v.v... Nhng bi ton ny, nu mi thao tc trn mt bn ghi chng ta tit kim c q thi gian th mt cu truy vn trn 1 bng c th tit kim c 10 n * q thi gian, y n l s bn ghi ca bng. Chng ta hy th tng tng, vi bng lu tr cc bn ghi c di c nh v nhn khu, trong vng 1 giy my c th x l cc nhanh 1000 bn ghi, th x l 4.850.000 nhn khu ca bng, my phi tn 4.850 giy! Qu l mt thi gian kh c th chp nhn cho vic ch i nhn kt qu ca mt cu lnh truy vn ! Ni chung, trong vic ti u ha x l thng tin, ngi ta u tin vic ti u ha v thi gian hn so vi vic ti u ha lu tr d liu. Trong mt s trng hp, ngi ta cn phi "hy sinh" c dng chun (Normal Form) t tc x l nhanh hn. Trong v d v h CSDL qun l CBVC nu ti mc 5.3 trong chng V, (v d 5.3.4), mi khi cn xem xt n h s lng ca mt CBVC ngi ta u phi tham kho qua bng NGCH-BC-LNG thng qua php kt t nhin vi bng CBVC trn cc thuc tnh Ngch v Bc . Nu trong danh sch CBVC chng ta b sung ct H-s-lng na th dn ti vic trng lp (v d tha) thng tin - nguy c dn n vic mt tnh nht qun d liu - ng thi khng m bo dng chun tt cho bng ny v buc phi b sung RBTV : CBVC [Ngch,Bc, H-s-lng ] NGCH-BC-LNG [Ngch,Bc, H-slng ]. B nh th cp (bng t, a t ...) ca my tnh gi y khng cn l vn lm mi ngi phi bn tm khi xy dng cc h CSDL ln. Dung lng mt a cng t ti hng chc Giga bytes. Tuy nhin vn c th tn ti nguy c thiu khng gian tnh ton cho my nu khng lu tm n vic ti u ha cc cu hi trc khi a vo my v yu cu my thi hnh. Mt nguyn nhn dn ti iu ny l vic thc hin cc php
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 1 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

kt ni v php tch -cc ca cc quan h. Chng ta bit, tch -cc ( trnh by ti im 5.2.4, mc 5.2,) v cc php kt (q -Join , Natural Join , Equi Join , Outer Join - trnh by ti im 5.3.3 mc 5.3 v mc 5.4 ca Chng V) i hi kh nhiu khng gian lu tr v thi gian x l. Cho R (A1, A2, ..., An) v S (B1, B2, ..., Bm) l hai quan h nh ngha trn 2 tp thuc tnh { A1, A2, ..., An } v { B1, B2, ..., Bm }. Nu R c N1b gi tr v S c N2 b gi tr th tch -cc s cho kt qu l mt bng mi gm m + n thuc tnh vi N1 * N2 b gi tr. Ch cn mi quan h c 10 thuc tnh cha s lng b gi tr kh khim tn, 1000 b, th tch -cc to ra bng trung gian c s thuc tnh l 20 v s lng b gi tr l 1.000.000. Nu mi field ch rng 5 bytes thi (m trn thc t th rt t c nhng quan h nh nh vy) th quan h trung gian ny "ngn" mt 1/10 Giga bytes ! ng nhin l, thi gian truy nhp CSDL v truy xut a t to ra ngn y bn ghi cng khng phi l t. S bng n s lng v "bnh trng" kch thc (chiu di) bn ghi ny t chng ta vo vic phi nghin cu ti u cu hi trc khi a vo my tnh ton. Chng ny ch yu trnh by mt vi phng php ti u ha cc biu thc quan h, c bit l x l biu thc c lin quan ti php kt ni v tch -cc. Sau s trnh by chi tit mt phng php ti u ha cho mt lp ph cp cc biu thc quan h. Ngun ti liu tham kho ch yu da trn cc l thuyt v v d minh ha trong chng VI ca PGS.PTS.L Tin Vng [8].

Cc nguyn tc tng qut ti u ha mt cu hi.


Chng ta hy xt mt v d n gin sau y : Cho hai quan h R(A,B) vi n bn ghi v S (C,D) vi m bn ghi. Tch -cc ca R v S l mt quan h Q (A,B,C,D) c n * m bn ghi (Xem 5.2.4. Php tch -cc ca 2 quan h, mc 5,2, Chng V). Chng ta c cu hi "Ly gi tr ca thuc tnh A sao cho B=C v D=50 ". Cu hi c vit li di dng ngn ng i s quan h nh sau: ( ( R(A,B) x S(C,D) ) : (B=C D=50) ) [A] Nu a php chn D=50 vo bn trong php tch -cc s c: (( R (A,B) x ( S(C,D) : (D = 50) ) ) : (B=C) ) [A] v sau chuyn php chn B=C ca tch -cc thnh php "kt ni bng" chng ta thu c: R rng, php tnh cui cng s tn km thi gian hn rt nhiu. -C th l : Ch chn trn quan h S nhng b c gi tr D=50 th s b ly ra s t hn ton b s b ca c quan h trn S. S b c chn ra xong t S mi em kt ni vi quan h R. Php kt ni ny ch chn ra b no thuc R m c gi tr ti B l bng b c gi tr ti C thuc S mi c ly ra kt li vi nhau. iu hon ton nhanh hn l ly tch -cc ca R x S ri mi chn trong kt qu nhng b c gi tr ti B bng gi
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 2 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

tr ti C. Vic bin i cu hi thnh cu hi tng ng nh v d nu trn l mt minh ha cho vic gim bt thi gian tr li cu hi bng cch gim bt s ln cn truy nhp ti b nh th cp da trn nguyn tc thc hin php chn cng sm cng tt. Trnh t thc hin cc php tnh s ng mt vai tr quan trng qu trnh t chc cu hi. J. D. Ullman [5] trong cc kt qu nghin cu cng b ln u tin ca mnh trnh by 6 chin lc tng quan cho vic ti u ha cu hi nh sau: -. Thc hin php chn cng sm cng tt. Bin i cu hi a php chn vo thc hin trc nhm lm gim bt kch c ca kt qu trung gian v do vy chi ph phi tr cho vic truy nhp b nh th cp cng nh lu tr ca b nh chnh s nh i. -T hp nhng php chn xc nh vi php tch -cc thnh php kt ni. Nh bit, php kt ni, c bit l php kt ni bng (Equi Join ) c th c thc hin t tn km hn nhiu so vi php tch -cc trn cng cc quan h. Nu kt qu ca tch -cc RxS l i s ca php chn v php chn lin quan ti cc php so snh gia cc thuc tnh ca R v S th r rng php tch -cc l php kt ni. -T hp dy cc php ton quan h mt ngi nh cc php chn v php chiu. Mt dy cc php mt ngi nh php chn hoc php chiu m kt qu ca chng ph thuc vo cc b ca mt quan h c lp th c th nhm cc php li. -Tm cc biu thc con chung trong mt biu thc. Nu kt qu ca mt biu thc con chung (tc l biu thc xut hin nhiu hn mt ln) l mt quan h khng ln v n c th c c t b nh th cp vi t thi gian th nn tnh ton trc biu thc ch mt ln. Nu biu thc con chung c lin quan ti mt php kt ni th trong trng hp tng qut khng th thay i c n bng cch "y" php chn vo trong. iu ng quan tm l, cc biu thc con chung c tn s xut hin ln thng c biu din trong cc VIEW (khung nhn) ca ngi s dng, bi v, thc hin cc cu hi cn phi thay th n bng mt biu thc c nh cho VIEW. - Tin x l cc quan h / bng (Table Preprocessing). C hai vn quan trng cn x l trc cho cc quan h l sp xp trc cc b gi tr theo th t vt l v sp xp lgc - tc l thit lp cc bng ch mc (Index ) cho cc bn ghi. Khi vic thc hin cc php ton c lin quan ti hai quan h (cc php ton hai ngi) s nhanh hn rt nhiu. - nh gi trc khi thc hin tnh ton.

http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1

3 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

Mi khi cn chn trnh t thc hin cc php ton trong biu thc, hoc chn mt trong hai i s ca mt php hai ngi, th cn tnh ton xem ch ph (Cost) thc hin cc php tnh (thng tnh theo s php ton, thi gian, hoc/v dung lng b nh cn thit so vi kch thc ca cc quan h, t xc nh c chi ph tng th phi tr cho cc cch khc nhau khi thc hin cc cu hi. Da vo cc nguyn tc nu trn, chng ta s bin i cu truy vn thnh cu hi tng ng ti u hn, vic thc hin c chi ph x l t hn. Nhng trc khi c th "ti u ha" cc biu thc, cn lm r khi nim khi no th hai biu thc c gi l tng ng. Trong phn sau s cho bit mt cch hnh thc khi nim tng ng. Biu thc tng ng. Trc ht, xem xt li khi nim v nh ngha quan h c s dng trong cc chng 2 v 3 thy s tng ng ca cc quan h da theo nh ngha. Vi cc nh ngha khc nhau chng cho cc tnh cht ton hc cng khc nhau. Nu quan nim quan h l mt tp hp cc b (k - b) vi k c nh, khi hai quan h l tng ng khi v ch khi chng c cng mt tp cc b. Vi quan nim quan h l tp cc nh x t tp tn thuc tnh vo tp gi tr, th khi hai quan h l bng nhau nu chng c cng mt tp nh x. nh ngha th nht c th bin i sang nh ngha th hai bng cch thay i tn thuc tnh thnh tn cc ct trong bng v, ngc li i t nh ngha th hai sang nh ngha th nht bng cch c nh th t cho cc thuc tnh. Sau y s s dng nh ngha th hai, ngha l, quan h l mt tp nh x t tp thuc tnh vo tp cc gi tr. Mt ngn ng truy vn hin hu lun lun i hi phi bit tn ca cc ct trong mt quan h. Do , tn cc thuc tnh trong mt quan h phi l mt bin trong mt biu thc v cng th hin kt qu ca biu thc. + Biu thc trong ngn ng i s quan h c cc hng thc l bin quan h (relation variables) R1,...., Rn; cc quan h hng (constant relation), c xc nh nh l mt nh x t cc k-b ca cc quan h (r1, ..., rk) trong ri l quan h trn lc ri v thay th ri vo Ri khi nh gi biu thc. Hai biu thc E1 v E2 c gi l tng ng (Equivalent), vit tt l E1 E2, nu chng biu din cng mt nh x, ngha l, nu chng ta thay th cng cc quan h cho tn cc lc tng ng hai biu thc E1 v E2, th chng s cho ra cng mt kt qu. Vi nh ngha tng ng ny chng ta c mt php chuyn dch i s thng thng sau y: Cc quy tc lin quan ti php kt v tch -cc. Quy tc giao hon ca php kt ni v tch -cc Nu E1 v E2 l hai biu thc quan h v F l iu kin trn cc thuc tnh ca E1 v E2 th:

http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1

4 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

// Tnh giao hon ca kt E1 * E2 E1 * E2 // Tnh giao hon ca kt bng E1 x E2 E1 x E2 // Tnh giao hon ca tch -cc. Chng ta chng minh quy tc giao hon ca php kt ni. Gi s E1 c cc thuc tnh A1, ..., An; E2 c cc thuc tnh B1, ..., Bm v cc thuc tnh A, B khng nht thit phi l phn bit. Gi r1 v r2 l hai quan h tng ng ca E1v E2. Gi tr ca E1 E2 l tp cc nh x a t A1 ... An, B1 ... Bm vo tp gi tr sao cho cc nh x m 1 r1 v m 2 r2 tha mn iu kin: a [Ai] = m 1[Ai], i = 1,2,...,n a [Bi] = m 1[Bi], i = 1,2,...,m v iu kin F l ng khi thay a [C] cho mi thuc tnh C trong F. Nu biu din E2 E1 cng nh trn, chng ta d dng nhn thy hai php kt trn cho chnh xc cng mt tp kt qu. Do vy hai biu thc l tng ng. Ch : Nu quan nim quan h l tp cc b (c th t thuc tnh c nh) th php q kt, kt t nhin v tch -cc khng th giao hon c v th t cc thuc tnh trong quan h kt qu b thay i. - Quy tc kt hp ca php kt ni v tch -cc. Nu E1, E2 v E3 l cc biu thc quan h: F1, F2 l iu kin th:

(E1 * E2) * E3 E1 * (E2 * E3) (E1 * E2) x E3 E1 x (E2 x E3) Vic kim tra tnh tng ng ca cc quy tc trn kh d dng. Cc quy tc lin quan ti php chn v php chiu Dy cc php chiu c th t hp li thnh mt php chiu, biu din theo cc trng hp sau: Dy cc php chiu (E [B1B2 ... Bm]) [A1A2 ... An] E [A1 ... An] y, cc thuc tnh A 1, ..., An phi nm trong tp cc thuc tnh B1, ... , Bm. Ng ngha ca vic bin i tng ng ny l: Nu thc hin mt php chiu biu thc
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 5 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

quan h E trn tp cc thuc tnh B, ri sau thc hin tip php chiu trn tp con cc thuc tnh A B ca quan h va tm c, th kt qu ca dy php chiu ny hon ton tng ng vi mt php chiu biu thc quan h E trn tp thuc tnh A. Tng t, dy cc php chn c th t hp thnh mt php chn kim tra tt c cc iu kin cng mt lc v c biu din nh sau: - Dy cc php chn: ( ((E : (f1)) : f2) : ... ) : fn E : (f1 f2 ... fn) Ng ngha : Vic ln lt thc hin cc php chn trn quan h kt qu ca mt php chn trc i vi biu thc quan h E l tng ng vi vic chn trn E cc b gi tr tha mn ng thi tt c cc iu kin chn f1, f2 ... fn. - Giao hon php chn v php chiu: (E [A1... An] : (f))(E : (f)) [A1 ... An] Mt cch tng qut hn, nu iu kin chn f lin quan ti cc thuc tnh B1, ... Bm m khng nm trong tp thuc tnh A1, ... An th: (E [A1 ... An]) : (f) (E [A1 ... An B1 ... Bm ]) : (f)) [A1 ... An] -Giao hon php chn v tch -cc: Nu tt c cc thuc tnh ca F l thuc tnh ca E1 th: (E1 x E2) : (f) (E1 : (f)) x E2 T d dng suy ra rng, nu F c dng f = f1 L f 2 trong f1 ch lin quan ti cc thuc tnh ca E1; f2 ch lin quan ti cc thuc tnh ca E2 , th c th s dng cc lut v c: (E1 x E2) : (f) (E1 : (f1)) x (E2 : (f2)) Hn na nu f1 ch lin quan ti cc thuc tnh ca E1, nhng f2 lin quan ti cc thuc tnh ca c E1 v E2 th: (E1 x E2) : (f) ((E1 : (f1)) x E2) : (f2) -Giao hon php chn v mt php hp: Nu c biu thc E = E1 E2; c th gi thit thm rng, cc thuc tnh ca E1 v E2 c cng tn nh ca E hoc t nht mi thuc tnh ca E l ph hp vi mt thuc tnh duy nht ca E1 v mt thuc tnh duy nht ca E2 . Khi :

http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1

6 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

(E1 E2) : (f) (E1 : (f))(E2 : (f)) Nu tn cc thuc tnh ca E1 v hoc E2 khc vi tn thuc tnh ca E th trong f v phi ca cng thc trn cn thay i s dng tn cho ph hp. -Giao hon php chn v mt php hiu tp hp (E1 - E2) : (f) (E1 : (f)) - (E2 : (f)) Nh nu trong lut , nu tn cc thuc tnh ca E 1 vE2 l khc nhau th cn thay th
cc thuc tnh trong f v phi biu thc tng ng tng ng vi E1. Ch rng, php chn (E2 : (f)) c th l khng cn thit. Trong nhiu trng hp, vic thc hin php chn (E2 : (f)) trc s c hiu qu hn l tnh ton trc tip vi E2 v kch c quan h lc s b i rt nhiu.

Cc quy tc nu trn ni chung l y php chn xung trc php kt ni v php kt ni thng thc hin lu nh php tch Cc. Quy tc y php chn xung trc php kt ni suy ra t quy tc v . Quy tc y php chiu xung trc php tch cc hoc php hp cng tng t nh quy tc v . Ch l khng c phng php tng qut cho vic y php chiu xung trc php hiu cc tp hp. -Giao hon mt php chiu vi mt php tch -cc: Gi E1, E2 l hai biu thc quan h, A1 ... An l tp cc thuc tnh trong B1, ... Bm l cc thuc tnh ca E1, cc thuc tnh cn li C1, ..., Ck thuc E2. Khi : (E1 x E2) [A1 ... An] E1 [B1 ... Bm] x E2 [C1 ... Ck] -Giao hon mt php chiu vi mt php hp: (E1 E2) [A1 ... An] E1[A1 ... An] E2[A1 ... An] Nh nu trong lut , nu tn cc thuc tnh ca E 1 v / hoc E2 l khc vi cc thuc tnh trong E1 E2 th cn thay A1 ... An bn v phi bng cc tn ph hp. V d v mt thut ton ti u ha biu thc quan h. Ti y c th p dng cc quy tc nu trong mc 8.2 c th ti u ha cc biu thc quan h. Biu thc "ti u" kt qu phi tun theo cc nguyn tc nu phn 8.1 mc d rng cc nguyn tc khng c ngha l bo m ti u cho mi trng hp tng ng. Lu rng, lun lun y php chn v php chiu xung mc cng su cng tt trong cy biu din biu thc quan h nhm to nn mt dy cc php chn cng nh php chiu t c th t chc thnh mt php chn theo sau mt php chiu. Nhm cc php chn v php chiu li trong mt nhm thc hin trc cc php tnh hai ngi nh php hp, tch -cc, hiu tp hp v.v... C mt s trng hp c bit xy ra khi mt php tnh hai ngi c cc hng thc
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 7 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

cha php chn v / hoc php chiu c p dng i vi l ca cy biu din biu thc. Khi cn xem xt cn thn tc ng ca php tnh hai ngi v mt s trng hp cn phi lin kt php chn hoc php chiu vi php hai ngi . Kt qu u ra (Output) ca thut ton l mt chng trnh bao gm cc bc nh sau: -p dng ca mt php chn hoc mt php chiu n gin. -p dng ca mt php chn v mt php chiu hoc - p dng ca mt tch -cc, php hp hoc php hiu tp hp cho hai hng thc m trc cc php chn hoc cc php chiu c p dng cho mt hoc c hai hng thc. Hy xt mt CSDL qun l th vin bao gm cc quan h sau y: -. SCH (Tn-sch, Tc-gi, Nh_XB, M-sch): l quan h v cc loi sch trong th vin. -NH-XUT-BN (Nh_XB, a-ch, Thnh-ph ): quan h v nh xut bn. -C-GI (Tn-G, ch-G, Tph-G, S-th ) : quan h v c gi - MN-SCH (S-th, M-sch, Ngy-mn): quan h s theo di mn. lu tr thng tin v sch c th gi thit thm rng c mt khung nhn (VIEW) theo di cc sch c mn, TD-MN, bao gm mt s thng tin b sung v sch c mn, l kt qu ca kt ni t nhin ca quan h SCH, C-GI v MNSCH, chng hn c xc nh qua biu thc quan h: ((SCH x C-GI x MN-SCH) : (f)) [{S}] y: f ::= (C-GI.S - t h = MN-SCH.S-th ) (SCH.M-sch = MNSCH.M-sch). v S l tp cc thuc tnh: S = { Tn-sch, Tc-gi, Nh_XB, SCH.M-sch, Tn_G, ch-G, Tph-G, C-GI.S-th, Ngy-mn }. -Cu hi: Cho danh sch nhng cun sch cho mn trc ngy 27/03/1999. Biu thc quan h c vit nh sau: (TD-MN : (Ngy-mn < 27/03/1999 ) ) [ Tn-sch ] Hnh cy ca biu thc trn c biu din bng hnh 8.1. Xin lu l, cc php ton nm phi di l cc php ton c thc hin trc cc php ton phi trn ca
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 8 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

cy.

Hnh 8.1: Biu din cy ca biu thc hi Thay th cc gi tr f v S vo biu thc hi c c cy biu din ca biu thc quan h nh trong hnh 8.1 -Bc th nht ca ti u l tch php chn f thnh hai php chn vi iu kin: SCH.M-sch = MN-SCH.M-sch v MN-SCH.S-th = C-GI.S-th By gi chng ta c 3 php chn. Cn "y" chng xung mc thp hn chng no cn c th c. Php chn vi iu kin Ngy-mn < 27/03/1999 c y xung di php chiu v hai php chn kia bng cch p dng cc quy tc (hoc lut) v . Php chn u c p dng cho tch -cc ((MN-SCH x C-GI) x SCH). V thuc tnh Ngy-mn trong php chn ch c quan h MN-SCH nn c th thay th: ((MN-SCH x C-GI) x SCH) : (Ngy-mn < 27/0 3/1999) bng biu thc: ((MN-SCH x C-GI) : (Ngy-mn < 27/03/1999 )) x SCH) v tip tc y xung na, cui cng ta c biu thc:
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 9 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

(((MN-SCH : (Ngy-mn < 27/03/1999 )) x C-GI) x SCH) Nh vy y c php chn theo ngy mn sch ny xung su nh c th. By gi tip tc y php chn vi iu kin Nh vy y c php chn theo ngy mn sch ny xung su nh c th. By gi tip tc y php chn vi iu kin SCH.M-sch = MN-SCH.Msch. xung mc thp nht nu c th. Khng th y php chn ny xung di tch -cc v n lin quan ti mt thuc tnh ca quan h SCH v mt thuc tnh thuc quan h MN-SCH. Do vy php chn: : MN-SCH.S-th = C-GI.S-th S-th c th y xung p dng cho tch -cc: (MN-SCH x C-GI) : ( : (Ngy-mn < 27/03/1999 ) Ch rng MN-SCH.S-th l tn mt thuc tnh ca php chn: MN-SCH : (Ngy-mn < 27/03/1999 ). -Bc tip theo : T hp hai php chiu thnh mt php chiu l [Tn-sch] nh lut . Kt qu c cho nh trong hnh 8.2. Sau p dng quy tc m rng thay th: :(MN-SCH.S-th = C-GI.S-thS-th ) v chiu [Tn_sch] nh dy php ton: [Tn-sch, SCH.M_sch, MN-SCH.m-sch ] (1) :(SCH.M_sch = MN-SCH.M-sch) (2) ri chiu ly tn sch: [Tn-sch] (3) p dng quy tc thay th biu thc u tin, biu thc s (1), ca php chiu nh php chiu: [Tn-schTn-sch, SCH.M_sch] p dng cho quan h SCHv [MN-SCH.m-sch] p dng cho hng thc phi tri ca tch -cc trong hnh 8.2. Php chiu cui v php chn c th p dng quy tc m rng c dy:
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 10 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

[ p dng cho hng thc phi tri ca tch -cc trong hnh 8.2. Php chiu cui v php chn c th p dng quy tc m rng c dy: [MN-SCH.M_schM_sch, MN-SCH.S-thS-th, C-GI.Sth] (5) : (MN-SCH.S-th = C-GI.S-th) (6) [MN-SCH.M_sch]M_sch] (7) Php chiu u, s (5), c phn tch chuyn xung tch -cc nh quy tc . Mt phn php chiu C-GI.S-th xung hng thc C-GI v l thuc tnh ca quan h ny.

Hnh 8.2 Cy vi t hp php chn v php chiu Phn cn li l php chiu ly 3 thuc tnh: [ MN-SCH.M_sch, MN-SCH.S-thS-th, Ngy-mn ] c y xung hng thc th MN-SCH. Cc thuc tnh khng ph hp s b loi b. Biu din cy cui cng ca biu thc nh trong hnh 8.3. Nhm cc php tnh bng ng mi tn gin on. Mi tch -cc c t hp vi php chn to thnh mt php kt ni bng nhau (Equi Join) rt c hiu
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 11 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

qu. c bit php chn trn quan h MN-SCH v php chiu ca C-GI ly thuc tnh S-th pha di l t hp vi tch -cc. Th t thc hin ca cy biu thc trong cc hnh 8.3, 8.2 v 8.3 l t di ln: Nhm cc php ton nm pha di c thc hin trc cc php ton phi trn.

Hnh 8.3 Cy kt qu biu din vic phn nhm cc biu thc.

Mt thut ton ti u ha cu hi trong ngn ng SQH


V d trn cho ta mt minh ha v vic chuyn i mt cu hi bng ngn ng i s quan h v dng tng ng tt hn (hay ti u hn). Phng php trn tp trung ch yu vo cc php chiu, php chn v tch -cc, vi mc ch lm sao "y" c php chn v php chiu xung mc thp nht, tc l thi hnh cc php ton ny cng sm cng tt, nu c th. Tip theo, kt hp cc php chn vi tch -cc thnh php kt t nhin lm gim cc kt qu trung gian. Ct li ca vn ti u ha chnh l vic lm gim thiu lu tr trung gian v t lm tng nhanh tc x l cu hi. Tuy nhin, thc hin c cc qu trnh ti u ha nh trn, chng ta cn lu ti th t thc hin cc php ton c th "y" cc php ton xung cc mc hp l cn thit. Bng di y cho php chng ta cch thc hin cc php bin i tng ng i vi cc php Hi (Union), Tr ( Minus), Giao (Intersect), Tch -cc (Cartesian), Chia (Division), Chiu (Projection) v Chn (Selection).
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 12 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

-(B1 ). Kt t nhin tng ng v dy php tch -cc, php chn v php chiu: Q1 (A,B) * Q2 (B,C) (Q1 x Q2 : (Q1[B]=Q2[B])) [A,B,C] -(B2 ). Php theta kt tng ng vi dy php tch -cc v php chn vi iu kin theta : Q1(A,B) Q2(C,D) (Q1 x Q2) : (Bq D) -(B3 ). Php giao (Intersect) tng ng vi phn b (Complement) ca hi hai phn b ca 2 quan h: Q1 Q2(( Q1) ( Q2)) v (B4 ) -(B4 ). Php b ca mt quan h tng ng vi tch -cc ca cc php chiu trn tng thuc tnh ca quan h tr i cc b gi tr c trong th hin ca quan h: Q(X1, X2, Xn) (Q[X1] x Q[X2] x x Q[Xn]) - Q(X1, Xn) -(B5 ). Thng ca 2 quan h tng ng vi hiu ca cc quan h trung gian sau: Q1(A,B) Q2(A) = Q1[B] - ((Q1[B] x Q2[A] - Q1(A,B)) [B] p dng cc cch bin i tng ng trn, kt hp vi cc quy tc "y" v kt hp nh trnh by trong mc 8.2, chng ta a ra thut ton tng qut ti u ha cc cu hi trong ngn ng i s quan h. -Thut ton: u vo (Input): S c php cu hi bng ngn ng i s quan h. u ra (Output): S c php ti u. -Bc 1 . p dng cc php bin i tng ng nu trong bng (B1 ) n (B5 ) trn. -Bc 2 . p dng lut bin i dy cc php chn tng ng: tch php chn thnh cc php chn con. -Bc 3 . i vi mi php chn, p dng cc lut , , v nhm y cc php ton chn xung cng su cng tt. -Bc 4 . i vi mi php chiu, p dng cc quy tc , v nhm y cc php ton chiu xung cng su cng tt. -Bc 5 .
http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1 13 / 14

Ti u ha cu hi - Th vin Hc liu m Vit Nam

-Tp trung cc php chn nhm p dng lut -p dng lut loi b bt cc php chiu v ch. -Tp trung cc php chn vi tch -cc, nu c, chuyn thnh phep kt t nhin hay theta kt bng cch p dng cc lut v . +Nhn xt: - Thut gii nu trn ch yu nhm gim khi lng d liu trung gian ch khng ch ra th t thc hin cc php kt. V d: (Q1 (A,B) * Q2 (B,C)) * Q3(A,C) (Q1 (A,B) * Q3 (A,C)) * Q2(B,C) -Thut gii ny khng cho chng ta mt kt qu ti u m n ch a ra mt gii php tt. - Cc php bin i ch da trn cc php ton c bn l Hi (Union), Tr (Minus), Giao (Intersect), Tch -cc (Cartesian), Chia (Division), Chiu (Projection) v Chn (Selection) m chng ta cn c th thc hin cc php bin i da trn cc php ton khc na.

http://voer.edu.vn/m/837905d79537f03879bd9ff45c6799f2/1

14 / 14