`

《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.5 Evaluation Order

阅读更多

Evaluation Order   求值顺序     (page 282)
      Multiple rules can be specified in the rules section, and the rules can be specified with dependencies betweene them. The rule evaluation sequence can affect the functional behavior of the SQL statement, as you will see in this section. Furthermore, even within a single rule, the evaluation of the rule must adhere to a logical sequence. You will look at intra-rule valuation order first and then at inter-rule evaluation.
    在规则段可以指定多个规则,而规则之间则可以指定依赖顺序。规则的求值顺序能影响SQL语句的功能行为,如你在本节所见。再者,即使在一个规则内,规则的求值必须附上逻辑顺序。你将看到首先是规则内部求值,其次才是规则之间求值。

 

Row Evaluation Order     行求值顺序    
    Let's look at row evaluation order within a rule. Listing 9-11 is copied from the Listing 9-3. However, this time I've commented out the keywords AUTOMATIC ORDER in line 8. By commenting those keywords, I force the default behavior of SEQUENTIAL ORDER .
    我们来看下一规则中的行求值顺序。列表9-11复制于列表9-3。然而,这次我注释掉了第8行的关键字AUTOMATIC ORDER。通过注释这些关键字,我强制用默认的行为SEQUENTIAL ORDER
    The rule has an inter-row reference with the clause inventory [cv(year),cv(week)-1]. Inventory column values must be calculated in ascending order of the week. For example, the inventory rule for the week 40 must be evaluated before evaluationg the inventory rule for the week 41. With AUTOMATIC ORDER, the database engine identifies the row dependencies and evaluates the rows in strict dependency order. Without the AUTOMATIC ORDER clause, row evaluation order is undermined . That leads to ORA-32637 errors, as shown in Listing 9-11.
    规则是带有子句inventory [cv(year),cv(week)-1]的行间引用。Inventory列值必须按week的升序计算。例如,40周的库存规则必须在41周的库存规则求值前进行求值。用AUTOMATIC ORDER,数据库引擎确认行的依赖且按严格的依赖顺序对行求值。没有AUTOMATIC ORDER子句的话,行求值顺序(规则)被破坏了 这导致ORA-32637错误,如列表9-11所示。
Listing 9-11.  Sequential with Error ORA-32637
  1    select product, country, year, week, inventory, sale, receipts
  2    from sales_fact
  3    where country in ('Australia')
  4    model return updated rows
  5    partition by (product, country)
  6    dimension by (year, week)
  7    measures ( 0 inventory , sale, receipts)
  8    rules -- Commented: automatic order
  9    (
 10         inventory [year, week ] =
 11                                   nvl(inventory [cv(year), cv(week)-1 ] ,0)
 12                                    - sale[cv(year), cv(week) ] +
 13                                    + receipts [cv(year), cv(week) ]
 14     )
 15*   order by product, country,year, week
     *
ERROR at line 2:
ORA-32637: Self cyclic rule in sequential order MODEL     顺序排序MODEL中的自循环规则

    It is a better practice to specify the row evaluation order explicitly to avoid this error. Listing 9-12
provides an example. In the rule section, you specify the order of row evaluation using an  ORDER BY
year, week  clause. This clause specifies that rules must be evaluated in the ascending order of Year,
Week column values. That is inventory rule for the Year=2000 and Week=40 must be evaluated before
evaluating the inventory rule for the Year=2000 and Week=41.
    最佳实践就是显式的指定行求值顺序避免该错误。 列表9-12提供了一个例子。在规则段,你指定行求值顺序用ORDER BY year, week 子句。该子句指定规则必须按Year,Week列值的顺序求值。也就是Year=2000 and Week=40的库存规则的求值必须在Year=2000 and Week=41的库存规则求值之前。
Listing 9-12.  Evaluation Order at Cell Level      在单元格级的求值顺序
  1    select product, country, year, week, inventory, sale, receipts
  2    from sales_fact
  3    where country in ('Australia')  and product in ('Xtend Memory')  --注:原书中没有加and product in ('Xtend Memory') 将返回9347行
  4    model return updated rows
  5    partition by (product, country)
  6    dimension by (year, week)
  7    measures ( 0 inventory , sale, receipts)
  8    rules (
  9         inventory [year, week ] order by year, week  =
 10                                   nvl(inventory [cv(year), cv(week)-1 ] ,0)
 11                                    - sale[cv(year), cv(week) ] +
 12                                    + receipts [cv(year), cv(week) ]
 13     )
 14*   order by product, country,year, week
PRODUCT          COUNTRY     YEAR WEEK  INVENTORY       SALE   RECEIPTS
------------        ----------          -----     ----     ----------            ----------   ----------
...
Xtend Memory   Australia       2001     49      2.519               45.26        47.33
Xtend Memory   Australia       2001     50     11.775               23.14        32.40
...

注:实际结果如下:

Xtend Memory                   Australia   2001   49      4.772      45     47.718
Xtend Memory                   Australia   2001   50        9.4      23     27.768
Xtend Memory                   Australia   2001   51       4.86     115     110.28
Xtend Memory                   Australia   2001   52     14.116      23     32.396


    Note that there is no consistency check performed to see if this specification of row evaluation
order is logically consistent or not. It is up to the coder—to you!—to understand the implications of
evaluation order.
For example, the row evaluation order in Listing 9-13 is specified with the DESC
keyword. While the rule is syntactically correct, semantic correctness is only known to the coder.
Semantic correctness might well require the specification of ASC for an ascending sort. Only the
person writing the SQL statement can know which order meets the business problem being addressed. 
Notice that Inventory column values are different between the Listings 9-12 and 9-13. You need to
ensure that the order of row evolution is consistent with the requirements.
    注意这里不会执行一致性检查来看行求值顺序规范逻辑上是一致的与否。这全仰仗于编码者--你(自己)--理解求值顺序的隐含意义。 例如,在列表9-13中行求值顺序由关键字DESC指定。虽然该规则句法上正确,语义的正确性只有编码者(自己)知道。对于升序,语义的正确性可能需要指定ASC。只有写SQL语句的人能知道哪种顺序符合要解决的业务问题(的要求)。注意在列表9-12和9-13间的Inventory列值是不同的。你需要确保行求值的顺序与需求是一致的。
Listing 9-13.  Evaluation Order using the DESC Keyword  
 1    select product, country, year, week, inventory, sale, receipts
 2    from sales_fact
 3    where country in ('Australia') and product in ('Xtend Memory')
 4    model return updated rows
 5    partition by (product, country)
 6    dimension by (year, week)
 7    measures ( 0 inventory , sale, receipts)
 8    rules (
 9         inventory [year, week ]  order by year, week desc    =
10                                   nvl(inventory [cv(year), cv(week)-1 ] ,0)
11                                    - sale[cv(year), cv(week) ] +
12                                    + receipts [cv(year), cv(week) ]
13     )
14*   order by product, country,year, week
PRODUCT      COUNTRY     YEAR WEEK  INVENTORY       SALE   RECEIPTS
------------          ----------       -----     ----         ----------          ----------  ----------
...
Xtend Memory Australia       2001     49        2.068               45.26      47.33
Xtend Memory Australia       2001     50        9.256               23.14      32.40
...

 

注:实际执行结果:

Xtend Memory                   Australia   2001   49      2.458      45     47.718
Xtend Memory                   Australia   2001   50      4.628      23     27.768
Xtend Memory                   Australia   2001   51      -4.54     115     110.28
Xtend Memory                   Australia   2001   52      9.256      23     32.396

Rule Evaluation Order    规则求值顺序
    In addition to the order in which rows are evaluated, you also have the question of the order in which
the rules are applied. In Listing 9-14, there are two rules with inter-dependency between them. The
first rule is evaluating the rule and refers to the Receipts column, which is calculated by the second
rule. These two rules can be evaluated in any order and the results will depend upon the order of rule
evaluation. It is important to understand the order of rule evaluation because the functional behavior
of the SQL statement can change with the rule evaluation order.   
 
    除了行求值顺序之外,你还有应用哪个规则的顺序问题。在列表9-14中,有两个相互独立的规则。第一个规则是求值,引用Receipts列,它由第二个规则计算。这两个规则可以以任意顺序求值,而结果将依赖于规则求值的顺序。理解规则求值的顺序是重要的,因为SQL语句的功能行为能随着规则求值顺序而改变。
    To improve clarity, you will filter on rows with week > 50. In Listing 9-14, line 9 specifies
sequential order. Sequential order specifies that the rules are evaluated in the order in which they are
listed. In this example, the rule for the Inventory column is evaluated, followed by the rule for the
Receipts column. Since the receipts rule is evaluated after the inventory rule, the inventory rule uses
the unaltered values before the evaluation of the receipts rule. Essentially, changes from the receipts
rule for the Receipts column calculation are not factored in to the  Inventory  calculation.
    The situation with rule evaluation is the same as with rows. Only a coder will know what order of evaluation is appropriate for the business problem being solved.  Only the coder will know whether the inventory rule should use altered values from execution of the receipts rule, or otherwise.

    为了提升清晰度(更容易理解),你可对行使用过滤条件week>50。在列表9-14中,行9指定顺序次序。顺序次序定义规则是按所列的顺序求值的。在本例中,Inventory列规则的求值,接着是Receipts列的规则。因为receipts规则的求值是在inventory规则之后(发生的),inventory规则在receipts 规则求值之前使用不变的值(进行求值)。本质上,Receipts列的计算,来至于receipts规则的变化不会影响Inventory的计算。

    规则求值的情况与行求值的一样。只有编码者知道求值顺序(是否 )合适于要解决的业务问题。 只有编码者知道是否inventory规则应使用来至于receipts规则的变化的值,或者相反。

 

Listing 9-14.  Rule Evaluation Order – Sequential Order
 1    select * from  (
 2    select product, country, year, week, inventory, sale, receipts
 3    from sales_fact
 4    where country in ('Australia') and product in ('Xtend Memory')
 5    model return updated rows
 6    partition by (product, country)
 7    dimension by (year, week)
 8    measures ( 0 inventory , sale, receipts)
 9    rules sequential order (
10        inventory [year, week ] order by year, week   =
11                       nvl(inventory [cv(year), cv(week)-1 ] ,0)
12                     - sale[cv(year), cv(week) ] +
13                     + receipts [cv(year), cv(week) ],
14        receipts [ year in (2000,2001), week in (51,52,53) ] 
15                   order by year, week

16                    = receipts [cv(year), cv(week)] * 10
17        )
18     order by product, country,year, week
19*   ) where week >50
 
PRODUCT      COUNTRY     YEAR WEEK  INVENTORY       SALE   RECEIPTS
------------ ----------   -----   ----  ----------     ---------- ----------
...
Xtend Memory Australia     2000   52     -6.037          67.45     614.13
Xtend Memory Australia     2001   51    -20.617         114.82     824.28
Xtend Memory Australia     2001   52    -22.931          23.14     208.26

注:实际运行结果:

PRODUCT                        COUNTRY     YEAR WEEK  INVENTORY    SALE   RECEIPTS
------------------------------ ---------- ----- ---- ---------- ------- ----------
Xtend Memory                   Australia   1998   51        .04      58     61.236
Xtend Memory                   Australia   1998   52      5.812      86     92.152
Xtend Memory                   Australia   1999   53     -2.705      27     24.345
Xtend Memory                   Australia   2000   52     -1.383      67     660.67
Xtend Memory                   Australia   2001   51       4.86     115     1102.8
Xtend Memory                   Australia   2001   52     14.116      23     323.96

 


    Another method of evaluating the order employed by Oracle database is automatic order . In
Listing 9-15, the evaluation order is changed to automatic order . With  automatic order, dependencies
between the rules are automatically resolved by Oracle and the order of rule evaluation depends upon
the dependencies between the rules. 

    Oracle数据库运用的另一中求值次序方法是自动次序(automatic order)。在列表9-15中,求值顺序变成了自动次序。使用自动次序,规则间的依赖由Oracle自动解析且规则求值的顺序取决于规则间的依赖关系。
    The results from Listing 9-15 and Listing 9-14 do not match. For example, inventory for week 52 is
-22.931 in Listing 9-14, and it is 906.355 in Listing 9-15. By specifying  automatic order,  you allow the
database engine to identify a dependency between the rules. Thus, the engine evaluates the receipts
rule first, followed by the inventory rule. 

    列表9-15和9-14的结果是不一致的。例如,列表9-14中库存week52是-22.931,而列表9-15中是906.355。通过指定自动次序,你允许数据库引擎确认规则间的依赖关系。因此,引擎首先对receipts规则求值,接着是inventory规则。
    Clearly, the order of rule evaluation can be quite important. If there are complex inter-
dependencies, then you might want to specify sequential order and list the rules in a strict evaluation
sequence.
That way, you are in full control and nothing is left to doubt.

    显然,规则求值的顺序是非常重要的。如果存在复杂的相互依赖关系,则你可能要指定顺序次序且按严格的求值顺序列出规则。 这样的话,你是全面控制而没有留下什么歧义。

 

Listing 9-15.  Rule Evaluation Order- Automatic Order
... 
9     rules automatic order  (
...
 
PRODUCT      COUNTRY     YEAR WEEK  INVENTORY       SALE   RECEIPTS
------------ ---------- ----- ---- ---------- ---------- ----------
...
Xtend Memory Australia   2000   52     546.68      67.45     614.13
Xtend Memory Australia   2001   51    721.235     114.82     824.28
Xtend Memory Australia   2001   52    906.355      23.14     208.26

 

注:实际执行结果

PRODUCT                        COUNTRY     YEAR WEEK  INVENTORY    SALE   RECEIPTS
------------------------------ ---------- ----- ---- ---------- ------- ----------
Xtend Memory                   Australia   1998   51        .04      58     61.236
Xtend Memory                   Australia   1998   52      5.812      86     92.152
Xtend Memory                   Australia   1999   53     -2.705      27     24.345
Xtend Memory                   Australia   2000   52     593.22      67     660.67
Xtend Memory                   Australia   2001   51     997.38     115     1102.8
Xtend Memory                   Australia   2001   52     1298.2      23     323.96

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics