`

《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
分享到:
评论

相关推荐

    《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.6 Aggregation

    本章“《Pro Oracle SQL》CHAPTER 9 The Model Clause”重点讲解了Model子句的使用,特别是其在聚合操作中的应用。 Model子句的基本结构允许用户指定一个数据区域(即模型区域),在这个区域内对数据进行计算和操作...

    《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.4Returning Updated Rows

    《Pro Oracle SQL》一书的第9章深入探讨了Oracle数据库中的"Model"子句,这一章节重点关注如何使用Model子句来更新数据行。在Oracle SQL中,Model子句是一种强大的功能,允许进行复杂的行处理和模拟迭代计算,通常...

    《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.7 Iteration

    《Pro Oracle SQL》一书中的第9章详细探讨了Oracle SQL中的“模型”子句,这一章的主题是“9.7 迭代”。在Oracle数据库中,模型子句是一种强大的功能,它允许用户进行复杂的多步骤计算,尤其适用于处理数组、矩阵...

    《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.3 Positional and Symbolic Refere

    《Pro Oracle SQL》一书中的第9章详细探讨了Oracle SQL中的“模型”子句,这一章重点关注了位置引用和符号引用的概念。在Oracle数据库中,模型子句是一种强大的工具,用于处理复杂的行列操作,例如数据建模、数据...

    《Pro Oracle SQL》CHAPTER 9 -- 9.2 Inter-Row Referencing via the Model clause

    《Pro Oracle SQL》是Oracle数据库查询的一本权威指南,其中第9章主要讲解了如何使用Model子句进行行间引用,这是一个高级SQL特性,用于处理复杂的行与行之间的计算和逻辑操作。9.2章节专注于Inter-Row Referencing...

    《Pro Oracle SQL》CHAPTER 9 -- 9.10Performance Tuning with the Model Clause

    《Pro Oracle SQL》一书的第9章,重点关注了使用"Model"子句进行性能调优的方法。在Oracle数据库中,Model子句是一种强大的功能,它允许数据建模和复杂的计算,尤其适用于解决多步骤计算问题,如模拟、预测和序列...

    《Pro Oracle SQL》Chapter7 Advanced Grouping -- 7.2HAVING Clause

    《Pro Oracle SQL》一书的第七章深入探讨了高级分组技术,特别是关于`HAVING`子句的部分。`HAVING`子句在SQL查询中扮演着至关重要的角色,它用于在聚合函数的结果集上设置条件,这与`WHERE`子句有所不同。`WHERE`...

    Pro Oracle SQL

    Pro Oracle SQL unlocks the power of SQL in the Oracle Database—one of the most potent SQL implementations on the market today. To master it requires a three-pronged approach: learn the language ...

    oracle advanced sql 高级SQL教程 ORACLE官方教材

    Walking the Tree: From the Top Down 5-9 Ranking Rows with the LEVEL Pseudocolumn 5-10 Formatting Hierarchical Reports Using LEVEL and LPAD 5-11 Pruning Branches 5-13 Summary 5-14 Practice 5 Overview 5...

    Oracle SQL 优化与调优技术详解-附录:SQL提示.pdf

    ### Oracle SQL 优化与调优技术详解:深入理解SQL提示 #### 一、SQL提示的定义及作用 在Oracle数据库的SQL语句中,SQL提示(Hints)是一种用于指导优化器选择特定执行计划的特殊注释语法。这些提示能够帮助数据库...

    Oracle和SqlServer语法区别

    Oracle和SqlServer语法区别 Oracle和SqlServer是两种流行的关系型数据库管理系统,它们之间存在着一些语法区别。了解这些区别对于开发者来说非常重要,因为它可以帮助他们更好地迁移到新的数据库管理系统。下面将...

    ORACLE和SQL Server的语法区别

    ### ORACLE和SQL Server的语法区别 #### 一、概述 本文主要介绍Oracle与SQL Server在SQL语言层面的异同之处,重点在于Transact-SQL(T-SQL)与PL/SQL之间的区别,并提供了一些迁移策略。对于希望将现有的Oracle...

    Clause-view个人中心、设置常用的itemview

    Clause-view个人中心、设置常用的itemview

    Oracle SQL查考手册chm

    Oracle SQL查考手册是一部关于Oracle数据库查询语言的重要参考资料,它涵盖了运算符、表达式、条件、函数以及常见的SQL DDL(Data Definition Language)和Clause等内容。这篇总结将深入解析这些核心概念,帮助读者...

    Python库 | clause-1.1.2.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:clause-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Chatopera 语义理解系统:机器学习,聊天机器人,意图识别clause-osc.zip

    Chatopera 语义理解系统:机器学习,聊天机器人,意图识别clause-osc.zip

Global site tag (gtag.js) - Google Analytics