31
Daudzdimensiju datu masīvu modelēšana. Virtuālas datu noliktavas iegūšana (virtual datawarehouse) select . . . . . . group by cube( . . . ) select . . . . . . model . . . 1

Web view05.09.2013 · Daudzdimensiju datu masīvu modelēšana. Virtuālas datu noliktavas iegūšana (virtual datawarehouse) select . . . . . . grou. p. by cube

Embed Size (px)

Citation preview

9

Daudzdimensiju datu masvu modelana.

Virtulas datu noliktavas iegana

(virtual datawarehouse)

select . . .

. . .

group by cube( . . . )

select . . .

. . .

model

. . .

Model konstrukcijas iespjas SELECT vaicjumos

1. The MODEL clause create a multidimensional array from query results and then apply formulas (called rules) to this array to calculate new values.

2. The rules can range from basic arithmetic to simultaneous equations using recursion.

3. The MODEL clause can replace PC-based spreadsheets.

4. The core query engine can work with unlimited quantities of data.

5. Models can be shared easily across workgroups, ensuring that calculations are consistent for all applications.

6. The MODEL clause enables you to create a multidimensional array by mapping the columns of a query into three groups:

1) partitions define the logical blocks of the result set. Rules in the MODEL clause are applied to each partition independent of other partitions. Thus, partitions serve as a boundary point for parallelizing the MODEL computation.

2) dimensions define the multi-dimensional array and are used to identify cells within a partition. By default, a full combination of dimensions should identify just one cell in a partition. In default mode, they can be considered analogous to the key of a relational table.

3) measures are equivalent to the measures of a fact table in a star schema. They typically contain numeric values such as sales units or cost. Each cell is accessed by specifying its full combination of dimensions. Note that each partition may have a cell that matches a given combination of dimensions.

Likumu (rules) izmantoana

1. The MODEL clause enables you to specify rules to manipulate the measure values of the cells in the multi-dimensional array defined by partition and dimension columns.

2. Rules access and update measure column values by directly specifying dimension values.

3. The MODEL clause is a scalable and manageable way of computing business models in the database.

The top segment shows the concept of dividing a typical table into partition, dimension, and measure columns.

The middle segment shows two rules that calculate the value of Prod1 and Prod2 for the year 2002.

Finally, the third part shows the output of a query that applies the rules to such a table with hypothetical data. The unshaded output is the original data as it is retrieved from the database, while the shaded output shows the rows calculated by the rules. Note that results in partition A are calculated independently from results of partition B.

SQL Model konstrukcijas sistakse

MODEL[][][MAIN ] [PARTITION BY ()] DIMENSION BY () MEASURES () [] [RULES] (, ,.., )

::= ::= RETURN {ALL|UPDATED} ROWS ::= [IGNORE NAV | [KEEP NAV] [UNIQUE DIMENSION | UNIQUE SINGLE REFERENCE] ::= [UPDATE | UPSERT | UPSERT ALL] [AUTOMATIC ORDER | SEQUENTIAL ORDER] [ITERATE () [UNTIL ]] ::= REFERENCE ON ON () DIMENSION BY () MEASURES ()

Datu apstrde ar SQL Model konstrukciju

One of the rules updates an existing value, while the other two create new values for a forecast. The figure shows that the rows of data retrieved by a query are fed into the MODEL clause and rearranged into an array. Once the array is defined, rules are applied one by one to the data. The shaded cells represent new data created by the rules and the cells enclosed by ovals represent the source data for the new values. Finally, the data, including both its updated values and newly created values, is rearranged into row form and presented as the results of the query.

Piemros izmantojam tabula

create table PRECU_PARDOSANA(

IDnumber Primary key,

VALSTSvarchar2(20),

PRECEvarchar2(20),

GADSnumber,

DAUDZUMSnumber,

IENEMUMInumber);

begin

insert into PRECU_PARDOSANA values(1, 'Latvija', 'piens', 2010, 1000, 300000);

insert into PRECU_PARDOSANA values(2, 'Latvija', 'piens', 2011, 1500, 450000);

insert into PRECU_PARDOSANA values(3, 'Latvija', 'piens', 2012, 2000, 600000);

insert into PRECU_PARDOSANA values(4, 'Latvija', 'maize', 2010, 4000, 1600);

insert into PRECU_PARDOSANA values(5, 'Latvija', 'maize', 2011, 5000, 2000);

insert into PRECU_PARDOSANA values(6, 'Latvija', 'maize', 2012, 5500, 2200);

insert into PRECU_PARDOSANA values(7, 'Latvija', 'dens', 2010, 1000, 100000);

insert into PRECU_PARDOSANA values(8, 'Latvija', 'dens', 2011, 1100, 110000);

insert into PRECU_PARDOSANA values(9, 'Latvija', 'dens', 2012, 1200, 120000);

insert into PRECU_PARDOSANA values(10, 'Lietuva', 'piens', 2010, 2000, 600000);

insert into PRECU_PARDOSANA values(11, 'Lietuva', 'piens', 2011, 3000, 900000);

insert into PRECU_PARDOSANA values(12, 'Lietuva', 'piens', 2012, 3500, 1050000);

insert into PRECU_PARDOSANA values(13, 'Lietuva', 'sviests', 2010, 1000, 1000000);

insert into PRECU_PARDOSANA values(14, 'Lietuva', 'sviests', 2011, 2000, 2000000);

insert into PRECU_PARDOSANA values(15, 'Lietuva', 'sviests', 2012, 2500, 250000);

insert into PRECU_PARDOSANA values(16, 'Lietuva', 'boli', 2010, 2000, 100000);

insert into PRECU_PARDOSANA values(17, 'Lietuva', 'boli', 2011, 3000, 150000);

insert into PRECU_PARDOSANA values(18, 'Lietuva', 'boli', 2012, 4000, 200000);

end;

SQL Model konstrukcijas izmantoanas iespjas

1. Jievro sinonmu lietoanas iespjas:

select PRECE, GADS, DAUDZUMS

from PRECU_PARDOSANA a

where a.VALSTS = 'Latvija'

model

partition by (a.VALSTS)

dimension by (a.PRECE, a.GADS)

measures (a.DAUDZUMS)

rules

(DAUDZUMS[PRECE ='piens', GADS =2012] =

DAUDZUMS[PRECE = 'piens', GADS =2011] *2)

order by PRECE, GADS;

(PRECE G DAUDZ-------------------- ---------- ------------maize 2010 4000maize 2011 5000maize 2012 5500piens 2010 1000piens 2011 1500piens 2012 3000dens 2010 1000dens 2011 1100dens 2012 1200)select PRECE, G, DAUDZ

from PRECU_PARDOSANA a

where VALSTS = 'Latvija'

model

partition by (VALSTS)

dimension by (PRECE, GADS as G)

measures (DAUDZUMS as DAUDZ)

rules

(DAUDZ[PRECE ='piens', G =2012] =

DAUDZ[PRECE = 'piens', G =2011] *2)

order by PRECE, G;

select PRECE, G, DAUDZ

from PRECU_PARDOSANA a

where VALSTS = 'Latvija'

model

partition by (VALSTS)

dimension by (PRECE, GADS as G)

measures (DAUDZUMS as DAUDZ)

rules

(DAUDZ['piens', 2012] = DAUDZ['piens', 2011] *2)

order by PRECE, G;

SQL Model konstrukcijas izmantoanas iespjas (turpinjums)

2. Funkciju lietoana likumos

select PRECE, GADS, DAUDZUMS

from PRECU_PARDOSANA a

where a.VALSTS = 'Latvija'

model

partition by (a.VALSTS)

dimension by (a.PRECE, a.GADS)

measures (a.DAUDZUMS)

rules

(DAUDZUMS[PRECE ='piens', GADS =2012] =

SUM(DAUDZUMS)[PRECE = 'piens', GADS BETWEEN 2010 and 2011])

order by PRECE, GADS;

PRECE GADS DAUDZUMS

-------------------- ---------- ----------

maize 2010 4000

maize 2011 5000

maize 2012 5500

piens 2010 1000

piens 2011 1500

piens 2012 2500

dens 2010 1000

dens 2011 1100

dens 2012 1200

SQL Model konstrukcijas izmantoanas iespjas (turpinjums)

3. UPSERT, UPSERT ALL un UPDATE papildiespjas

The UPSERT option (is the default) can create cell values that do not exist in the input data:

1) if the cell referenced exists in the data, it is updated;

2) if the cell referenced does not exist in the data, and the rule uses appropriate notation, then the cell is inserted. The UPSERT ALL option enables you to have UPSERT behavior for a wider variety of rules.

The UPDATE option, on the other hand, would never insert any new cells.

You can specify these options globally, in which case they apply to all rules, or per each rule. If you specify an option at the rule level, it overrides the global option.

select PRECE, GADS, DAUDZUMS

from PRECU_PARDOSANA a

where a.VALSTS = 'Latvija'

model

partition by (a.VALSTS)

dimension by (a.PRECE, a.GADS)

measures (a.DAUDZUMS)

rules

(DAUDZUMS[PRECE ='dens', GADS =2012] =

UPSERT DAUDZUMS[PRECE = 'dens', GADS = 2012] + 1000)

order by PRECE, GADS;

PRECE GADS DAUDZUMS

-------------------- ---------- --------------------

maize 2010