Upload
trandiep
View
216
Download
1
Embed Size (px)
Citation preview
1
Jauna indeksa veidošana
1. Datu objekta tipa projektēšana.2. Datu objekta tipa specifikācijas definēšana.3. Datu objekta tipa ķermeņa definēšana.4. Tabula ar problēmsfēras objekta tipa kolonnu.5. Datu analīzes funkciju izveidošana.6. Funkciju piesaiste operatoriem.7. Indeksa tipam tiek definēts objekta tips ar ODCI metožu realizēšanu7.1. Indeksa tipa objekta tipa specifikācija. 7.2. Indeksa tipa objekta tipa ķermeņa veidošana. ODCI metožu implementēšana (realizēšana).
ODCIGetInterfacesODCIIndexCreateODCIIndexStart, ODCIIndexFetch ODCIIndexCloseODCIIndexInsert ODCIIndexUpdateODCIIndexDelete
8. Indeksa tipa izveide (operatoru darbība indeksatabulā).9. Indeksa izveide.10. Statistikas savākšana no pamattabulas.11. Paplašināmā optimizatora interfeisa metodes.12. Vaicājumu izpilde
2
1. Datu objekta tipa projektēšanaObjekts = paraugs (reģioni (1, 2, ... ) ar elektrības patēriņa vērtībām)
Objektu (paraugu) datu fiksēšana laika gaitā (pa stundām vai dienām, vai mēnešiem)
Kāda informācija ir vajadzīga analīzei?1. Vai objekta (parauga) X reģiona vērtība ir Y (atbilde: patiess, nepatiess).2. Vai objekta (parauga) X reģiona vērtība ir Y nav lielāka par norādīto vērtību Z (atbilde: patiess, nepatiess).3. Vai objekta (parauga) kāda no reģiona vērtībām ir vienāda ar norādīto vērtību Y (atbilde: patiess, nepatiess).4. Vai objekta (parauga) kāda no reģiona vērtībām ir lielāka par norādīto vērtību Y (atbilde: patiess, nepatiess).5. Vai objekta (parauga) kāda no reģiona vērtībām ir mazāka par norādīto vērtību Y (atbilde: patiess, nepatiess).
. . .
3
2. Datu objekta tipa specifikācijas definēšana
K1 K2 K3 K4
Objekta tips reģionu vērtību glabāšanai:1) kopējās elektroenerģijas patēriņa vērtība;2) minimālās elektroenerģijas patēriņa vērtība; 3) maksimālās elektroenerģijas patēriņa vērtība;4) katra reģiona elektroenerģijas patēriņa vērtības5) ieraksta laiks;6) metode kopējās, minimālās un maksimālās elektroenerģijas patēriņa vērtības noteikšanai.
Masīvs reģionu vērtību glabāšanai
create or replace type T_REGIONU_VERTIBAS as varray(25) of number;
create or replace type T_ENERGO_PIEPRAS as object (
KOP_PIEPRAS number,
MIN_PIEPRAS number,
MAX_PIEPRAS number,
REGIONU_VERTIBAS T_REGIONU_VERTIBAS,
LAIKS date,
MEMBER procedure APR_PIEPRAS);
4
5
3. Datu objekta tipa ķermeņa definēšana
Metode APR_PIEPRAS kalpo kopējā, minimālā un maksimālā elektroenerģijas
patēriņa aprēķināšanai un saglabāšanai objekta atribūtos.
create or replace type body T_ENERGO_PIEPRAS as
MEMBER procedure APR_PIEPRAS is
kop number;
minv number;
maxv number;
i number;
BEGIN
kop := 0;
minv := REGIONU_VERTIBAS(REGIONU_VERTIBAS.FIRST);
maxv := REGIONU_VERTIBAS(REGIONU_VERTIBAS.FIRST);
for i in REGIONU_VERTIBAS.FIRST .. REGIONU_VERTIBAS.LAST LOOP
kop := kop + REGIONU_VERTIBAS(i);
if minv > REGIONU_VERTIBAS(i) then minv := REGIONU_VERTIBAS(i);
end if;
if maxv < REGIONU_VERTIBAS(i) then maxv := REGIONU_VERTIBAS(i);
end if;
end loop;
SELF.KOP_PIEPRAS := kop;
SELF.MIN_PIEPRAS := minv;
SELF.MAX_PIEPRAS := maxv;
end;
end;
6
4. Tabula ar problēmsfēras objekta tipa kolonnucreate table ENERGO_TABULA (ID number Primary key,PARAUGS T_ENERGO_PIEPRAS);
DECLARE-- Objekta definēšanaEN T_ENERGO_PIEPRAS;BEGIN-- Datu ievade objektāEN := T_ENERGO_PIEPRAS(NULL, NULL, NULL, T_REGIONU_VERTIBAS( 5, 7,12, 7, 4, 6,10,13, 8, 5, 9,14,12, 9, 6, 8, 9,11,10, 7, 6, 7,10, 9, 5), TO_DATE('01-01-2006 01','MM-DD-YYYY HH') );-- Kopējo, minimālo un maksimālo elektroenerģijas patēriņa aprēķinsEN.APR_PIEPRAS;-- Datu ievade tabulāINSERT INTO ENERGO_TABULA VALUES (1, EN);END;
select a.*from ENERGO_TABULA a;
7
5. Datu analīzes funkciju izveidošana
F_ENERGO_VIENADS(paraugs, reģions, vērtība)
F_ENERGO_LIELAKS(paraugs, reģions, vērtība)
F_ENERGO_VIENADS_JEBK(paraugs, vērtība)
F_ENERGO_LIELAKS_JEBK(paraugs, vērtība)
F_ENERGO_MAZAKS_JEBK(paraugs, vērtība)
-- Vai norādītā enerģijas patēriņa vērtība ir vienāda ar objekta konkrētā reģiona
-- patēriņu?
CREATE FUNCTION F_ENERGO_VIENADS (OBJ T_ENERGO_PIEPRAS,
REGIONS NUMBER, VERTIBA NUMBER) RETURN NUMBER AS
BEGIN
IF REGIONS <= OBJ.REGIONU_VERTIBAS.LAST THEN
IF (OBJ.REGIONU_VERTIBAS(REGIONS) = VERTIBA) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
ELSE
RETURN NULL; -- ārpus robežām
END IF;
END;
Funkciju nosaukumi, sākās ar priedēkli „F_”.
8
6. Funkciju piesaiste operatoriem
Operatoru realizācija ir ļoti cieši sasaistīta ar paplašināmo indeksēšanu. Ja operators ir piesaistīts problēmsfēras indeksa tipam un ir vaicājuma WHERE daļā, tad indeksācijas mehānisms, balstoties uz operatora predikātu informāciju, var optimizēt vaicājumu un iedarbināt indeksēšanas mehānismu.Funkciju argumentu vērtībām jāsakrīt ar operatora argumentiem.
create operator ENERGO_VIENADS binding (T_ENERGO_PIEPRAS, NUMBER, NUMBER) return number USING F_ENERGO_VIENADS;
create operator ENERGO_LIELAKS binding (T_ENERGO_PIEPRAS, NUMBER, NUMBER) return number USING F_ENERGO_LIELAKS;
create operator ENERGO_MAZAKS binding (T_ENERGO_PIEPRAS, NUMBER, NUMBER) return number USING F_ENERGO_MAZAKS;
create operator ENERGO_VIENADS_JEBK binding (T_ENERGO_PIEPRAS, NUMBER)return number USING F_ENERGO_VIENADS_JEBK;
create operator ENERGO_LIELAKS_JEBK binding (T_ENERGO_PIEPRAS, NUMBER)return number USING F_ENERGO_LIELAKS_JEBK;
create operator ENERGO_MAZAKS_JEBK binding (T_ENERGO_PIEPRAS, NUMBER) return number USING F_ENERGO_MAZAKS_JEBK;
select a.ID
from ENERGO_TABULA a
where ENERGO_VIENADS(a. PARAUGS, 2, 7) = 1;
9
7. Indeksa tipam tiek definēts objekta tips
Pirms tiek izveidots indeksa tips, tam ir jādefinē objekta tips, kas saturēs svarīgākās
interfeisa metodes, kuras ir nepieciešamas indeksēšanas mehānisma darbībai. Dažas no
tām ir realizētas kā STATIC, dažas kā MEMBER – tas ir atkarīgs no funkcijas
specifikācijas.
MEMBER funkcijas tiek izpildītas konkrētam eksemplāram, tām ir piekļuve visiem
objektu tipa argumentiem.
STATIC funkcija var tikt izsaukta pat, ja nav izveidots objektu tipa eksemplārs.
Objektu tips satur vienu papildus mainīgo – KURSORS (NUMBER tipa atribūts). Šis
kursors tiks izmantots, lai varētu veikt problēmsfēras indeksa skenēšanu. Ar paketes
DBMS_SQL palīdzību kursors tiks saistīts ar dinamiski izveidotu vaicājumu, un šis
kursors tiks izmantots metodēs, kas atbild par datu izgūšanu no indeksa tabulas.
10
7.1. Indeksa tipa objekta tipa specifikācija
create or replace type T_ENERGO_INDEKSS as object (
KURSORS number,
static function ODCIGetInterfaces(IFCLIST OUT SYS.ODCIOBJECTLIST)
return number,
static function ODCIIndexCreate (SYS.ODCIINDEXINFO, PARMS VARCHAR2,
ENV SYS.ODCIENV) RETURN NUMBER,
static function ODCIINDEXDROP(IA SYS.ODCIINDEXINFO,
ENV SYS.ODCIENV) RETURN NUMBER ,
static function ODCIINDEXSTART(SCTX IN OUT T_ENERGO_INDEKSS,
IA SYS.ODCIINDEXINFO, OP SYS.ODCIPREDINFO, QI
SYS.ODCIQUERYINFO, STRT NUMBER, STOP NUMBER, SALPOZ NUMBER,
SALVERT NUMBER, ENV SYS.ODCIENV) return number,
static function ODCIINDEXSTART(SCTX IN OUT T_ENERGO_INDEKSS,
IA SYS.ODCIINDEXINFO, OP SYS.ODCIPREDINFO, QI
SYS.ODCIQUERYINFO, STRT NUMBER, STOP NUMBER, SALVERT
NUMBER, ENV SYS.ODCIENV) return number,
MEMBER function ODCIINDEXFETCH(NROWS NUMBER,
RIDS OUT SYS.ODCIRIDLIST, ENV SYS.ODCIENV) return number,
MEMBER FUNCTION ODCIINDEXCLOSE(ENV SYS.ODCIENV) return number,
static function ODCIINDEXINSERT(IA SYS.ODCIINDEXINFO, RID VARCHAR2,
JAUNA_V T_ENERGO_PIEPRAS, ENV SYS.ODCIENV) return number,
static function ODCIINDEXDELETE(IA SYS.ODCIINDEXINFO, RID VARCHAR2,
VECA_V T_ENERGO_PIEPRAS, ENV SYS.ODCIENV) return number,
static function ODCIINDEXUPDATE(IA SYS.ODCIINDEXINFO, RID VARCHAR2,
VECA_V T_ENERGO_PIEPRAS, JAUNA_V T_ENERGO_PIEPRAS, ENV
SYS.ODCIENV) return number);
11
Indeksa tipa objekta tipa metodes (Oracle Data Cartridge Interface (ODCI) metodes)
Indkesa metode Apraksts
ODCIGetInterfaces Tiek izsaukta kad INDEXTYPE tiek veidots ar komandu create indextype ... vai modificēts.Atgriež interfeisa versiju realizējamajam indeksam ODCIINDEX2. FUNCTION ODCIGetInterfaces(ifclist OUT ODCIObjectList)RETURN NUMBERIfclist - contains information about the interfaces it supports.Returns ODCIConst.Success on success or ODCIConst.Error on error.
ODCIIndexCreate Tiek izsaukta kad veido indeksu ar komandu crete index ... INDEXTYPE is ... parameters ...FUNCTION ODCIIndexCreate(ia ODCIIndexInfo, parms VARCHAR2, env ODCIEnv) RETURN NUMBERIzveido tabulu, kur tiek glabāti indeksējamie dati, tiek izsaukta indeksa izveides laikā. Jāparedz gadījumi, kad pamattabula nav tukša.
ODCIIndexDrop Izdzēš tabulu ar indeksējamajiem datiem. Tiek izsaukta DROP INDEX vai pamattabulas dzēšanas gadījumā.
ODCIIndexAlter Atjauno indeksa tabulu, balstoties uz izmainītajiem indeksa parametriem. Tiek izsaukta ALTER INDEX gadījumā.
ODCIIndexStart Inicializē indeksa skenēšanu iepriekšdefinētam un ar indeksa tipu sasaistītam operatoram. Nodefinē kursoru vaicājumam, kas tiek konstruēts uz operatora bāzes (gadījumā, kad vaicājumā parādās operatori, kuri var tikt analizēti ar indeksa palīdzību).
ODCIIndexFetch Atgriež ROWID katrai rindiņai, kura apmierina operatora predikātu, t.i., ar indeksa palīdzību izgūstam nepieciešamās rindiņas.
ODCIIndexClose Beidz vaicājuma izpildi, aizver kursoru.
ODCIIndexInsert Maina indeksa struktūru gadījumā, kad pamattabulā tiek ievietoti dati.
ODCIIndexDelete Maina indeksa struktūru gadījumā, kad no pamattabulas tiek izdzēsti dati.
ODCIIndexUpdate Maina indeksa struktūru gadījumā, kad pamattabulā tiek atjaunoti dati.
ODCIIndexGetMetadata Dod iespēju eksportēt un importēt ar indeksu realizāciju saistītus metadatus.
12
7.2. Indeksa tipa objekta tipa ķermeņa veidošanaODCI metožu implementēšana (realizēšana)
create or replace type body T_ENERGO_INDEKSS as
7.2.1. ODCIGetInterfaces metodes realizēšana
STATIC tipa metodi izsauc objekta tipam ne eksemplāram.
STATIC function ODCIGETINTERFACES(
IFCLIST OUT SYS.ODCIOBJECTLIST) return number is
BEGIN
IFCLIST := SYS.ODCIOBJECTLIST(SYS.ODCIOBJECT('SYS',
'ODCIINDEX2'));
return ODCICONST.SUCCESS;
END ODCIGETINTERFACES;
declarem1 SYS.ODCIOBJECTLIST;m2 number;beginm2 := T_ENERGO_INDEKSS.ODCIGETINTERFACES(m1);
DBMS_OUTPUT.Put_line(m2);
end;
13
7.2.2. ODCIIndexCreate metodes realizēšana
Attēlā redzams, kā pirmās trīs kolekcijas vērtības nonāk indeksa tabulā. Katrai vērtībai tiek norādīta:
1) reģiona pozīcija kolekcijā (RPOZ);2) reģiona patēriņa vērtība (RVERT);3) rindiņas identifikācijas numurs (RID).
14
15
STATIC function ODCIINDEXCREATE (IA SYS.ODCIINDEXINFO,PARMS varchar2, ENV SYS.ODCIENV) return number isI INTEGER; R ROWID; P NUMBER; V NUMBER; VAIC1 VARCHAR2(1000);VAIC2 VARCHAR2(1000); VAIC3 VARCHAR2(1000); KUR1 INTEGER;KUR2 INTEGER; KUR3 INTEGER; JUNK NUMBER;BEGIN-- Konstruējam SQL vaicājumu (VAIC1) indeksa tabulas izveidošanaiVAIC1 := 'create table ' || IA.INDEXSCHEMA || '.' || IA.INDEXNAME || '_EIND' || '( R ROWID, RPOZ NUMBER, RVERT NUMBER)';-- Veidojam kursoru un izpildām vaicājumuKUR1 := DBMS_SQL.OPEN_CURSOR;DBMS_SQL.PARSE(KUR1, VAIC1, DBMS_SQL.NATIVE);JUNK := DBMS_SQL.EXECUTE(KUR1);DBMS_SQL.CLOSE_CURSOR(KUR1);-- Indeksa tabulas aizpildīšanas vaicājums (VAIC2); :RR - parametrsVAIC2 := ' insert into '|| IA.INDEXSCHEMA || '.' || IA.INDEXNAME || '_EIND' ||' SELECT :RR, ROWNUM, COLUMN_VALUE FROM THE' ||' (SELECT CAST (E.'|| IA.INDEXCOLS(1).COLNAME || '.REGIONU_VERTIBAS as T_NUMTAB)' ||' from ' || IA.INDEXCOLS(1).TABLESCHEMA || '.' || IA.INDEXCOLS(1).TABLENAME || ' E' ||' where E.ROWID = :RR)';-- Veidojam kursoru otrajam vaicājumam KUR2 := DBMS_SQL.OPEN_CURSOR;DBMS_SQL.PARSE(KUR2, VAIC2, DBMS_SQL.NATIVE);-- Nolasām ROWID no pamattabulas un nododam to INSERT komandaiVAIC3 := 'select ROWID from '|| IA.INDEXCOLS(1).TABLESCHEMA || '.' || IA.INDEXCOLS(1).TABLENAME;-- Veidijam kursoru trešajam (VAIC3) vaicājumamKUR3 := DBMS_SQL.OPEN_CURSOR;DBMS_SQL.PARSE(KUR3, VAIC3, DBMS_SQL.NATIVE);DBMS_SQL.DEFINE_COLUMN_ROWID(KUR3, 1, R);JUNK := DBMS_SQL.EXECUTE(KUR3);WHILE DBMS_SQL.FETCH_ROWS(KUR3) > 0 LOOP -- iegūstam kolonnu vērtības no rindiņas DBMS_SQL.COLUMN_VALUE_ROWID(KUR3, 1, R); -- piesaista iegūto rindas numuru mainīgajam DBMS_SQL.BIND_VARIABLE_ROWID(KUR2, ':RR', R); JUNK := DBMS_SQL.EXECUTE(KUR2);END LOOP;DBMS_SQL.CLOSE_CURSOR(KUR2);DBMS_SQL.CLOSE_CURSOR(KUR3);return ODCICONST.SUCCESS;END;
16
Datu pārrakstīšanas piemērs izmantojot DBMS_SQL paketi
create tabele TA1(NUM1 number,NOS1 varchar2(10),SKA1 number);
insert into TA1 values (1, 'AA', 100);insert into TA1 values (2, 'BB', 50);insert into TA1 values (3, 'CC', 200);
create table TA2(NUM2 number,NOS2 varchar2(10),SKA2 number);
declarenum1_var number; nos1_var varchar2(10); ska1_var number; source_cursor integer; destination_cursor integer; ignore integer; source varchar2(10) default 'TA1';destination varchar2(10) default 'TA2';BEGIN -- Kursora sagatavošana nolasei no tabulas TA1.source_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(source_cursor, 'select NUM1, NOS1, SKA1 from ' || source, DBMS_SQL.NATIVE); DBMS_SQL.DEFINE_COLUMN(source_cursor, 1, num1_var); DBMS_SQL.DEFINE_COLUMN(source_cursor, 2, nos1_var, 10); DBMS_SQL.DEFINE_COLUMN(source_cursor, 3, ska1_var);-- Kursora sagatavošana ierakstīšanai tabulā TA2. destination_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(destination_cursor, 'insert into ' || destination || ' VALUES (:num1_bind, :nos1_bind, :ska1_bind)', DBMS_SQL.NATIVE); -- Rindu nolasīšana no pirmās tabulas un ierakstīšana otrā tabulā. LOOP IF DBMS_SQL.FETCH_ROWS(source_cursor)>0 THEN -- get column values of the row DBMS_SQL.COLUMN_VALUE(source_cursor, 1, num1_var); DBMS_SQL.COLUMN_VALUE(source_cursor, 2, nos1_var); DBMS_SQL.COLUMN_VALUE(source_cursor, 3, ska1_var); -- Bind the row into the cursor that inserts into the destination table. You -- could alter this example to require the use of dynamic SQL by inserting an -- if condition before the bind.
17
DBMS_SQL.BIND_VARIABLE(destination_cursor, ':ska1_bind', ska1_var); DBMS_SQL.BIND_VARIABLE(destination_cursor, ':nos1_bind', nos1_var); DBMS_SQL.BIND_VARIABLE(destination_cursor, ':ska1_bind', ska1_var); ignore := DBMS_SQL.EXECUTE(destination_cursor); ELSE -- No more rows to copy: EXIT; END IF; END LOOP; -- Commit and close all cursors: COMMIT; DBMS_SQL.CLOSE_CURSOR(source_cursor); DBMS_SQL.CLOSE_CURSOR(destination_cursor); EXCEPTION WHEN OTHERS THEN IF DBMS_SQL.IS_OPEN(source_cursor) THEN DBMS_SQL.CLOSE_CURSOR(source_cursor); END IF; IF DBMS_SQL.IS_OPEN(destination_cursor) THEN DBMS_SQL.CLOSE_CURSOR(destination_cursor); END IF; RAISE; END;
18
7.2.3. Metodes ODCIIndexStart, ODCIIndexFetch un ODCIIndexClose
Ja vaicājumā ir kāds no operatoriem WHERE daļā, kas ir sasaistīti ar konkrēto
indeksēšanas mehānismu, tad Oracle kodols var izvēlēties datus meklēt nevis
pamattabulā, veicot tās datu rindiņu pilnīgu pārlasi, bet gan griezties pie indeksēšanas
mehānisma.
Pamattabulas rindiņu identifikatoru izgūšanai tiek izmantotas ODCIIndexStart,
ODCIIndexFetch un ODCIIndexClose metodes.
1. Metode ODCIIndexStart atver kursoru speciālam vaicājumam, kurš tiek ģenerēts uz
operatora izteiksmes pamata.
2. Vaicājuma izpildes gaitā metode ODCIIndexFetch speciālā kolekcijā (kas ir viens no
ODCIIndexFetch argumentiem) tiek saglabāti pamattabulas rindiņu numuri, kas
apmierina dotā vaicājuma nosacījumus.
3. Beigās metode ODCIIndexClose aizver kursoru.
Gala rezultātā mēs iegūstam tos datu rakstus, kuru numuri tika atgriezti ar
ODCIIndexFetch metodi.
19
ODCIIndexStart metodes var būt vairākas. To argumentu skaits un tips var variēt – pēc
standartargumentiem seko argumenti, kas atbilst operatoru argumentiem (bez objektu
tipa). Piemērā ir divi dažādi operatoru tipi:
1) ar diviem argumentiem (vērtība konkrētam reģionam);
2) tikai salīdzināmā energopatēriņa vērtība.
Kurš tieši operators tiek izsauks no trijiem, tiek noteikts metodes izpildes gaitā, jo
arguments OP (SYS.ODCIPREDINFO tips) satur informāciju par operatoru.
-- vaicajumiem ar noteiktajiem reģioniemSTATIC function ODCIINDEXSTART(SCTX IN OUT T_ENERGO_INDEKSS,IA SYS.ODCIINDEXINFO, OP SYS.ODCIPREDINFO, QI SYS.ODCIQUERYINFO,STRT NUMBER, STOP NUMBER, SALPOZ NUMBER, SALVERT NUMBER, ENV SYS.ODCIENV) return number isKUR INTEGER;RID ROWID;RINDUSK INTEGER;RELOP VARCHAR2(2);VAIC VARCHAR2(1000);BEGIN SYS.ODCIINDEXINFODUMP(IA); SYS.ODCIPREDINFODUMP(OP); DBMS_OUTPUT.PUT_LINE('STARTA ATSLEGA : '||STRT); DBMS_OUTPUT.PUT_LINE('STOP ATSLEGA : '||STOP); DBMS_OUTPUT.PUT_LINE('SALIDZINASANAS POZICIJA : '||SALPOZ); DBMS_OUTPUT.PUT_LINE('SALIDZINASANAS VERTIBA : '||SALVERT);
-- operatora rezultats var but tikai 1 (taisniba) vai 0 (aplams)-- OP() = 1 vai OP() = 0if (STRT != 1) and (STRT != 0) then RAISE_APPLICATION_ERROR(-20101, 'NEPAREIZS OPERATORA PREDIKATS!'); end if;
if (STOP != 1) and (STOP != 0) then RAISE_APPLICATION_ERROR(-20101, 'NEPAREIZS OPERATORA PREDIKATS!'); end if;
-- genere vaicajumu datu izgusanai no indeksa tabulasif OP.OBJECTNAME = 'ENERGO_VIENADS' then if STRT = 1 then RELOP := '='; ELSE RELOP := '!='; end if;elsif OP.OBJECTNAME = 'ENERGO_MAZAKS' then
20
if STRT = 1 THEN RELOP := '<'; ELSE RELOP := '>='; end if; ELSIF OP.OBJECTNAME = 'ENERGO_LIELAKS' THEN IF STRT = 1 THEN RELOP := '>'; ELSE RELOP := '<='; END IF; ELSE RAISE_APPLICATION_ERROR(-20101, 'NEZINAMS OPERATORS'); END IF; VAIC := 'SELECT R FROM '||IA.INDEXSCHEMA||'.'||IA.INDEXNAME||'_EIND'|| ' WHERE RPOZ '|| '=' ||''''||SALPOZ||''''|| ' AND RVERT '||RELOP||''''||SALVERT||''''; KUR := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(KUR, VAIC, DBMS_SQL.NATIVE); DBMS_SQL.DEFINE_COLUMN_ROWID(KUR, 1, RID); RINDUSK := DBMS_SQL.EXECUTE(KUR); -- konteksts = kursora numurs SCTX := T_ENERGO_INDEKSS(KUR); RETURN ODCICONST.SUCCESS; END;
21
member function ODCIINDEXFETCH(NROWS number, RIDS OUT SYS.ODCIRIDLIST, ENV SYS.ODCIENV) return number is KUR INTEGER; IDX INTEGER := 1; RLIST SYS.ODCIRIDLIST := SYS.ODCIRIDLIST(); BEIGAS BOOLEAN := FALSE; BEGIN DBMS_OUTPUT.PUT_LINE('RINDUSK : '||ROUND(NROWS)); KUR := SELF.KURSORS; WHILE NOT BEIGAS LOOP IF IDX > NROWS THEN BEIGAS := TRUE; ELSE RLIST.EXTEND; IF DBMS_SQL.FETCH_ROWS(KUR) > 0 THEN DBMS_SQL.COLUMN_VALUE_ROWID(KUR, 1, RLIST(IDX)); IDX := IDX + 1; ELSE RLIST(IDX) := NULL; BEIGAS := TRUE; END IF; END IF; END LOOP;
RIDS := RLIST; RETURN ODCICONST.SUCCESS; END;
22
MEMBER function ODCIIndexClose(ENV SYS.ODCIENV) return number isKUR INTEGER; BEGIN -- aizver kursoru KUR := SELF.KURSORS; DBMS_SQL.CLOSE_CURSOR(KUR); RETURN ODCICONST.SUCCESS; END;
23
7.2.4. Metodes ODCIIndexInsert, ODCIIndexUpdate un ODCIIndexDelete
Šīs trīs metodes nostrādā gluži kā trigeri datu manipulāciju gadījumā: kad dati tiek
ievadīti pamattabulā (ODCIIndexInsert), kad dati tiek atjaunoti tajā (ODCIIndexUpdate)
un kad dati tiek dzēsti no tās (ODCIIndexDelete). Ar šo metožu palīdzību informācija
tiek sinhronizēta starp pamattabulu un indeksa tabulu, lai saturiski tās būtu ekvivalentas.
24
8. Indeksa tipa izveide
Kad ir izveidots indeksa objekta tips, var izveidot indeksa tipu, norādot operatorus,
uz kuru pamata indeksēšanas mehānisms spēs meklēt atbilstošās pamatdatu
rindiņas identifikatorus indeksa tabulā.
create or replace indextype ENERGO_INDEKSS FORENERGO_VIENADS(T_ENERGO_PIEPRAS, NUMBER, NUMBER),ENERGO_LIELAKS(T_ENERGO_PIEPRAS, NUMBER, NUMBER),ENERGO_MAZAKS(T_ENERGO_PIEPRAS, NUMBER, NUMBER),ENERGO_VIENADS_JEBK(T_ENERGO_PIEPRAS, NUMBER),ENERGO_LIELAKS_JEBK(T_ENERGO_PIEPRAS, NUMBER),ENERGO_MAZAKS_JEBK(T_ENERGO_PIEPRAS, NUMBER)
USING T_ENERGO_INDEKSS;
Pēc atslēgvārda FOR tiek uzskaitīti attiecīgie problēmsfēras operatori un to argumenti
iekavās. Pirmais arguments vienmēr ir problēmsfēras datus raksturojošs tips, kas
raksturos arī indeksējamo tabulas kolonnu. Pēc tam seko pārējo argumentu tipi, kas tad
arī nosaka metožu ODCIIndexStart skaitu.
25
9. Indeksa izveide
create index EINDEX on ENERGO_TABULA(PARAUGS) indextype is
T_ENERGO_INDEKSS;
SYS.ODCIIndexInfoDump procedūra nodrošina vispārīgas informācijas
izvadi (indeksa īpašnieks, nosaukums, tabulas īpašnieks, tabulas nosaukums,
indeksējamā tabulas kolonna un tas tips). Šī ir speciāla Oracle metode, kura
palīdz testēt problēmsfēras indeksa darbību.
26
10. Statistikas savākšana no pamattabulasOptimizators izmanto statistiku par vaicājumā minētajiem objektiem, lai izskaitļotu selektivitāti un izmaksas.
associate statistics with columns ENERGO_TABULA.PARAUGS using
ODCIStatsTips;
analyze table ENERGO_TABULA compute statistics;
Statistika tiek asociēta ar noteiktu kolonnu, izmantojot ODCIStatsTips objektu tipu, kuram noteikti jārealizē ODCIStatsCollect metodi analizējamās kolonnas tipam.
11. Paplašināmā optimizatora interfeisa metožu raksturojumi
Metode AprakstsODCIStatsCollect Metode statistikas savākšanai kolonnai vai
indeksa datiem.ODCIStatsDelete Izdzēš statistiku par kolonnu vai indeksa datiem.ODCIStatsSelectivity Novērtē lietotāja definētu funkcijas vai operatora
predikāta selektivitāti.ODCIStatsFunctionCost Balstoties uz uzdotajiem funkcijas parametriem,
izskaitļo funkcijas izpildes izmaksas.ODCIStatsIndexCost Balstoties uz uzdoto operatora predikātu, izskaitļo
problēmsfēras indeksa skenēšanas izmaksas.
27
12. Vaicājumu izpildeIndeksa mehānisms tiek iedarbināts gadījumā, kad vaicājuma WHERE daļā parādās
noteikts salīdzināšanas operators, kas norāda, pēc kādiem kritērijiem dati tiek atlasīti.
Problēmsfēras indeksa gadījumā tiek izsauktas indeksa metodes ODCIIndexStart,
ODCIIndexFetch un ODCIIndexClose, ar kuru palīdzību tiek izskatīta indeksa tabula
un atgrieztas vajadzīgās rindiņas.
Ja vaicājums tiek veikts bez problēmsfēras indeksa operatoriem WHERE daļā, tad
vaicājuma izpildes plānā redzams, ka Oracle veic pilnu piekļuvi pamattabulai – TABLE
ACCESS FULL, ko var novērot, analizējot vaicājuma izpildes plānu.
explain plan for select * from ENERGO_TABULA a
where a.PARAUGS.KOP_PIEPRAS>3;
select ID, Operation , Options, Object_name, Cost
from Plan_Table;
28
Ja tiek veikts vaicājums, kura WHERE daļā parādās kāds no problēmsfēras operatoriem,
kas ir saistīts ar indeksa tipu, izpildes plāns norāda, ka datu atlase notiek no indeksa
tabulas EINDEKSS_EIND.
select ID, Operation , Options, Object_name, Cost
from Plan_Table;
explain plan for select * from ENERGO_TABULA a
where ENERGO_VIENADS_JEBK(PARAUGS, 6) = 1;
29
Tabulas PLAN_TABLE struktūra
Tabulas struktūras izveidošana: @$ORACLE_HOME/rdbms/admin/utlxplan.sql
Column Type Description
STATEMENT_ID VARCHAR2(30) Value of the optional STATEMENT_ID parameter specified in the EXPLAIN PLAN statement.
PLAN_ID NUMBER Unique identifier of a plan in the database.
TIMESTAMP DATE Date and time when the EXPLAIN PLAN statement was generated.
REMARKS VARCHAR2(80) Any comment (of up to 80 bytes) you want to associate with each step of the explained plan. This column is used to indicate whether an outline or SQL Profile was used for the query.
If you need to add or change a remark on any row of the PLAN_TABLE, then use the UPDATE statement to modify the rows of the PLAN_TABLE.
OPERATION VARCHAR2(30) Name of the internal operation performed in this step. In the first row generated for a statement, the column contains one of the following values:1. DELETE STATEMENT 2. INSERT STATEMENT 3. SELECT STATEMENT 4. UPDATE STATEMENT
OPTIONS VARCHAR2(225) A variation on the operation described in the OPERATION column.
OBJECT_NODE VARCHAR2(128) Name of the database link used to reference the object (a table name or view name). For local queries using parallel execution, this column describes the order in which output from operations is consumed.
OBJECT_OWNER VARCHAR2(30) Name of the user who owns the schema containing the table or index.
OBJECT_NAME VARCHAR2(30) Name of the table or index.
OBJECT_ALIAS VARCHAR2(65) Unique alias of a table or view in a SQL statement. For indexes, it is the object alias of the underlying table.
OBJECT_INSTANCE NUMERIC Number corresponding to the ordinal position of the object as it appears in the original statement. The numbering proceeds from left to right, outer to inner with respect to the original statement text. View expansion results in unpredictable numbers.
OBJECT_TYPE VARCHAR2(30) Modifier that provides descriptive information about the object; for example, NON-UNIQUE for indexes.
30
Column Type Description
OPTIMIZER VARCHAR2(255) Current mode of the optimizer.
SEARCH_COLUMNS NUMBERIC Not currently used.
ID NUMERIC A number assigned to each step in the execution plan.
PARENT_ID NUMERIC The ID of the next execution step that operates on the output of the ID step.
DEPTH NUMERIC Depth of the operation in the row source tree that the plan represents. The value can be used for indenting the rows in a plan table report.
POSITION NUMERIC For the first row of output, this indicates the optimizer's estimated cost of executing the statement. For the other rows, it indicates the position relative to the other children of the same parent.
COST NUMERIC Cost of the operation as estimated by the optimizer's query approach. Cost is not determined for table access operations. The value of this column does not have any particular unit of measurement; it is merely a weighted value used to compare costs of execution plans. The value of this column is a function of the CPU_COST and IO_COST columns.
CARDINALITY NUMERIC Estimate by the query optimization approach of the number of rows accessed by the operation.
BYTES NUMERIC Estimate by the query optimization approach of the number of bytes accessed by the operation.
OTHER_TAG VARCHAR2(255) Describes the contents of the OTHER column. Values are: SERIAL (blank) - Serial execution. Currently, SQL is not loaded in the OTHER column for this case. SERIAL_FROM_REMOTE (S -> R) - Serial execution at a remote site. PARALLEL_FROM_SERIAL (S -> P) - Serial execution. Output of step is partitioned or broadcast to parallel execution servers. PARALLEL_TO_SERIAL (P -> S) - Parallel execution. Output of step is returned to serial query coordinator (QC) process. PARALLEL_TO_PARALLEL (P -> P) - Parallel execution. Output of step is repartitioned to second set of parallel execution servers. PARALLEL_COMBINED_WITH_PARENT (PWP) - Parallel execution; Output of step goes to next step in same parallel process. No interprocess communication to parent. PARALLEL_COMBINED_WITH_CHILD (PWC) - Parallel execution. Input of step comes from prior step in same parallel process. No interprocess communication from child.
31
Column Type Description
PARTITION_START VARCHAR2(255) Start partition of a range of accessed partitions. It can take one of the following values:
n indicates that the start partition has been identified by the SQL compiler, and its partition number is given by n.
KEY indicates that the start partition will be identified at run time from partitioning key values.
ROW REMOVE_LOCATION indicates that the start partition (same as the stop partition) will be computed at run time from the location of each record being retrieved. The record location is obtained by a user or from a global index.
INVALID indicates that the range of accessed partitions is empty.
PARTITION_STOP VARCHAR2(255) Stop partition of a range of accessed partitions. It can take one of the following values:
n indicates that the stop partition has been identified by the SQL compiler, and its partition number is given by n.
KEY indicates that the stop partition will be identified at run time from partitioning key values.
ROW REMOVE_LOCATION indicates that the stop partition (same as the start partition) will be computed at run time from the location of each record being retrieved. The record location is obtained by a user or from a global index.
INVALID indicates that the range of accessed partitions is empty.
PARTITION_ID NUMERIC Step that has computed the pair of values of the PARTITION_START and PARTITION_STOP columns.
OTHER LONG Other information that is specific to the execution step that a user might find useful. See the OTHER_TAG column.
DISTRIBUTION VARCHAR2(30) Method used to distribute rows from producer query servers to consumer query servers.
CPU_COST NUMERIC CPU cost of the operation as estimated by the query optimizer's approach. The value of this column is proportional to the number of machine cycles required for the operation. For statements that use the rule-
32
Column Type Description
based approach, this column is null.
IO_COST NUMERIC I/O cost of the operation as estimated by the query optimizer's approach. The value of this column is proportional to the number of data blocks read by the operation. For statements that use the rule-based approach, this column is null.
TEMP_SPACE NUMERIC Temporary space, in bytes, used by the operation as estimated by the query optimizer's approach. For statements that use the rule-based approach, or for operations that do not use any temporary space, this column is null.
ACCESS_PREDICATES
VARCHAR2(4000) Predicates used to locate rows in an access structure. For example, start or stop predicates for an index range scan.
FILTER_PREDICATES VARCHAR2(4000) Predicates used to filter rows before producing them.
PROJECTION VARCHAR2(4000) Expressions produced by the operation.
TIME NUMBER(20,2) Elapsed time in seconds of the operation as estimated by query optimization. For statements that use the rule-based approach, this column is null.
QBLOCK_NAME VARCHAR2(30) Name of the query block, either system-generated or defined by the user with the QB_NAME hint.