《Pro Oracle SQL》Chapter8--8.9 Advanced topics

Advanced topics     高级主题      (page 268)
    A few advanced topics about the analytic functions are worthy of discussion. I will discuss topics such as dynamic analytic statements, nesting of analytic functions, parallelism, and PGA size.
    关于分析函数的一些高级主体很值得讨论。我将讨论如动态分析语句,分析函数的嵌套,并行性 ,以及PGA大小。
Dynamic SQL     动态SQL
    A common question about the analytic SQL statement is whether a bind variable can be used in place of partitioning or sorting columns. No. If you want the flexibility to modify the partitioning or sorting columns dynamically, you need to use dynamic SQL statements. Static analytic SQL statements can not change the partitioning or sorting columns.
    分析函数有一个常见的问题是:绑定变量是否能用于替换分区或排序列么?答案是不能。 如果你需要动态修改分区列和排序列(的这种弹性),你就需要使用动态SQL语句。 静态分析SQL语句不能改变分区或排序列。
    If your goal is to modify the partitioning columns dynamically, then consider creating a packaged procedure to capture the logic in the procedure. In the Listing 8-24, the procedure Analytic_dynamic_prc accepts a string to be used as partitioning columns. A SQL statement is constructed using the arguments passed and executed dynamically using Execute immediate  syntax. Result of the analytic statement is fetched into an array and printed using a call to dbms_output package. 
    如果你的目标是动态的修改分区列,就要考虑创建打包的存储过程,把逻辑封装在存储过程中。在列表8-24中,存储过程 Analytic_dynamic_prc接收一字符串用做分区列。SQL语句使用传入的参数构造且使用Execute immediate句法动态的执行。分析语句的结果从数组中取出且调用dbms_output包打印出结果。
    In the first call, the analytic_dynamic_prc passes the string  product, country, region  as the first argument and the columns in this list are used as the partitioning columns. The second call to the procedure uses the string  product, country, region, year  to use a different list of columns for the partitioning-clause.
    在第一次调用中,analytic_dynamic_prc传递字符串“product, country, region”作为第一个参数,在列表中的列用做分区列。第二次调用过程使用字符串“product, country, region, year”作为分区子句的不同的列表。
    Note that this procedure is given as an example and as such may not be construed as a production-ready code.
    注意这个过程仅作为一个例子,可能用于生产环境的代码不能如此构造。
Listing 8-24.  Dynamic SQL Statement
create or replace procedure  
  analytic_dynamic_prc ( part_col_string varchar2, v_country varchar2, v_product varchar2)
is 
  type numtab is table of number(18,2) index by binary_integer;
  l_year numtab;
  l_week numtab;
  l_sale numtab;
  l_rank numtab;
  l_sql_string  varchar2(512) ;
begin
 l_sql_String := 
 'select * from (
   select  year, week,sale, 
    rank() over(
          partition by ' ||part_col_string ||'
          order by sale desc
           ) sales_rank
   from sales_fact
   where country in (' ||chr(39) || v_country || chr(39) || ' )  and 
         product =' || chr(39) || v_product || chr(39) ||
       ' order by product, country,year, week
    ) where sales_rank<=10
    order by 1,4';
 execute immediate l_sql_string bulk collect into  l_year, l_week, l_sale, l_rank;
 for  i  in 1 .. l_year.count
  loop
       dbms_output.put_line ( l_year(i) ||' |' || l_week (i) ||
                            '|'|| l_sale(i) || '|' || l_rank(i) ); 
  end loop;
 end;
/
 
exec analytic_dynamic_prc ( 'product, country, region','Australia','Xtend Memory');
...
1998 |48|172.56|9
2000 |46|246.74|3
2000 |21|187.48|5
2000 |43|179.12|7
2000 |34|178.52|8
2001 |16|278.44|1
2001 |4|256.7|2
exec analytic_dynamic_prc ( 'product, country,region, year','Australia','Xtend Memory');
 
1998 |48|172.56|1
1998 |10|117.76|2
1998 |18|117.56|3
1998 |23|117.56|3
1998 |26|117.56|3
1998 |38|115.84|6
1998 |42|115.84|6
...

Nesting Analytic Functions
    Analytic functions can not be nested, but a nesting effect can be achieved with the use of subqueries. For example, the clause lag(first_value(column,1),1) is syntactically incorrect. Subqueries can be used to
create a nesting effect, as you’ll see below.
    分析函数不能嵌套,但是使用子查询能取得嵌套效果。 例如,子句lag(first_value(column,1),1)句法上是错误的。你将看到,使用子查询能创建嵌套效果。
    Suppose your goal is to fetch the maximum Sale column value for the year and the prior year in the same row; if so, then analytic functions lag  and first_value  can be used in the subqueries to write a SQL statement. In Listing 8-25, inner subquery is fetching the Year and Week Sale column value in which the maximum sale occurred, in addition to fetching the maximum Sale column value for that year. The lag function in the outer query retrieves the prior Year Maximum Sale column value.
    假设你的目标是在同一行中取出当年和上一年的最大的Sale列值。在列表8-25中,内部子查询取出的是最大sale值所在行的Year,Week列值,还有取出那年的最大Sale列值。外部查询的lag函数检索上一年最大Sale列值。
    Notice that the partitioning clause is different between lag and  first_value  functions. Analytic function first_value  is computing the top Sale row in a partition specified by the partitioning columns product, country, region, year  whereas the lag is fetching the first row from the prior year specifying only sorting-clause:  order by year desc . 
    注意lag和first_value函数的分区子句是有区别的。分析函数first_value计算的是按分区列product, country, region, year指定分区的第一Sale列值所在行,而lag则取的是由排序子句“order by year desc”指定的前一年的第一行。
    With multi-level nesting of analytic functions, complex goals can be implemented concisely using the analytic functions.
    通过多层分析函数嵌套,复杂的目标就能通过精简的使用分析函数得以执行。
Listing 8-25. Nesting Analytic Functions
select  year, week, top_sale_year,
    lag( top_sale_year) over ( order by year desc)  prev_top_sale_yer
from (
 select distinct
    first_value ( year)  over ( 
          partition by product, country, region ,year
          order by sale desc
          rows between unbounded preceding and unbounded following
     ) year,
    first_value ( week) over ( 
          partition by product, country, region ,year
          order by sale desc
          rows between unbounded preceding and unbounded following
     ) week,  
    first_value (sale) over(
          partition by product, country, region ,year
          order by sale desc
          rows between unbounded preceding and unbounded following
     ) top_sale_year
  from sales_fact
  where country in ('Australia')  and product ='Xtend Memory'
)
  order by year, week
/
 
 YEAR     WEEK     TOP_SALE_YEAR     PREV_TOP_SALE_YER
-----          ----           -------------                 -----------------
 1998       48            172.56                      148.12
 1999       17            148.12                      246.74
 2000       46             246.74                     278.44
 2001       16             278.44

Parallelism     并行性

    By specifying a parallel hint in the SQL statement or by setting parallelism at the object level, analytic functions can be parallelized. If you have huge amount of data that needs to be processed using analytic functions, parallelism is a good choice. A SQL statement using multi-level nesting also can benefit from parallelism. 
    通过在SQL语句中指定并发提示或者通过在对象层级设定并行性,分析函数就能并发化(执行)。 如果你有大量的数据需要使用分析函数处理,并行性就是不错的选择。使用了多层嵌套的SQL语句也能从并行性中获益。
    Listing 8-26 shows the execution plan for the query in the Listing 8-25 using parallelism. In the execution plan, there are two WINDOW operations as the SQL statement has nested the lag and first_value analytic functions.
    列表8-26展示了列表8-25查询使用并行性后的执行计划。在执行计划中,有两处WINDOW操作,因为SQL语句嵌套了分析函数lag和first_value。
    Optimal distribution of rows between the PQ slaves is critical to maintain functional correctness and that is automatically handled by Oracle database. 
    在
PQ副盘(Parallel Query slaves) 之间行集的优化分布对于保持功能正确性是至关重要的,且是由Oracle数据库自动处理的。
Listing 8-26. Parallelism
----------------------------------------------------
 Id  | Operation                                                          | Name
-----------------------------------------------------
   0 | SELECT STATEMENT                                        |
   1 |   SORT ORDER BY                                            |
   2 |     WINDOW BUFFER                                         |
   3 |        PX COORDINATOR                                     |
   4  |          PX SEND QC(ORDER)                               |  : T Q1 0 0 03
   5  |            SORT ORDER BY                                  |
   6  |              PX  RECEIVE                                     |
   7  |                PX SEND RANGE                               |  : T Q1 0 0 02
   8  |                  VIEW                                             |
   9  |                   HASH UNIQUE                               |
   10  |                     PX RECEIVE                              |
   11  |                        PX SEND HASH                       |  : T Q1 0 0 01
   12  |                          WINDOW SORT                      |
   13  |                             PX RECEIVE                      |
   14  |                               PX SEND HASH                |  : T Q1 0 0 00
   15  |                                 PX  BLOCK  ITERATOR     |
*  16  |                                   TABLE ACCESS FULL    |  SALES_FACT

PGA size

    Most operations associated with the analytic functions are performed in the Program Global Area(PGA) of the process. So, for optimal performance it is important to have a big enough memory area so that programs can execute analytic functions without spilling to the disk. This is very analogous to a Sort operation. If the Sort operation spills to the disk due to a lower value of the memory size, then the performance of the Sort operation will not be optimal.Similarly, the execution performance of analytic functions will suffer if the operation spills to the disk.

    与分析函数有关联的大部分操作在程序全局区(PGA)中执行。 如此以来,对优化性能而言有充足的内存区域将使得程序能执行分析函数而不要溢出 到磁盘中。这同Sort操作如出一辙。如果Sort操作由于低容量的内存溢出到磁盘,则Sort操作将不是优化的了。相似的,如果Sort操作溢出到磁盘则分析函数的执行性能将大打折扣。
    Database initialization parameter PGA_AGGREGATE_TARGET(PGAT) controls the cumulative maximum size of the PGA. By default, a serial process can allocate a PGA up to the maximum size of 5% of PGAT value. For parallel processes, the limit is up to 30% of PGAT. It is essential to keep PGAT to a bigger value to improve the performance of analytic functions.
    数据库初始化参数 PGA_AGGREGATE_TARGET(PGAT)控制着累积的最大PGA尺寸。默认情况下,一串行进程能最多分配5%的PGAT容量的PGA。而对于并行进程,这个限制上升到30%。保持PGAT足够的大对提高分析函数的性能有实质意义。

Organizational Behavior
   The hardest thing about analytic function is the organizational resistance to change. Developers and databae administrators are comfortable writing SQL statements using conventional syntax. Using analytic syntax will not come easy. However, these developers and database administrators need to embrace the change. Another plus: use of analytic functions forces one to think in terms of sets.

    关于分析函数最难的事是团队对(新事物)改变的阻力。开发者和数据库管理员满足于是用传统句法写SQL。推行分析函数将不那么顺利。然而,这些开发者和数据库管理员需要拥抱这种改变。另一个好处:使用分析函数迫使人们依照集合的方式来思考。
    Oracel Corporation releases new features in every major release of Oracle Database. We need to harness the new features to write more efficient and concise SQL statements. Proper training for these new features is also essential and hopefully, this chapter provided an insight in to analytic functions.

    Oracle公司在每一个Oracle数据库主要版本发布新特性。我们需要驾驭这些新特性来写出更加有效,精简的SQL语句。对这些新特性适当的培训也是必要的和有前途的,本章深入讨论了分析函数。
    When you start writing SQL statement utilizing the analytic functions, start with simpler SQL statement. Then add more complexity to meet tht goal.
    当你开始利用分析函数写SQL语句,开始用简单的SQL语句,然后逐渐的复杂,最终达到目的。
Summary      总结
    Complex SQL statements can be written using analytic functions concisely. Understanding analytic function provides you whole new way of thinking, analytically speaking. The ability to reference another row combined with partitioning and windowing clase allows you to simplify complex SQL statements. Many performance issues can be resolved rewriting the SQL statement using analytic functions, and that resistance can be easily overcome by showing the performance improvements with analytic functions.

    复杂的SQL语句能使用分析函数简明的写出来。理解分析函数给予你全新的方式思考,分析的表述。 行间引用的能力结合分区和窗口子句使得你能够简化复杂的SQL语句。许多性能问题通过使用分析函数重写SQL语句迎刃而解,且通过展示分析函数所带来的性能提升(推广它的)阻力也将容易克服。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
GeoPandas是一个开源的Python库,旨在简化地理空间数据的处理和分析。它结合了Pandas和Shapely的能力,为Python用户提供了一个强大而灵活的工具来处理地理空间数据。以下是关于GeoPandas的详细介绍: 一、GeoPandas的基本概念 1. 定义 GeoPandas是建立在Pandas和Shapely之上的一个Python库,用于处理和分析地理空间数据。 它扩展了Pandas的DataFrame和Series数据结构,允许在其中存储和操作地理空间几何图形。 2. 核心数据结构 GeoDataFrame:GeoPandas的核心数据结构,是Pandas DataFrame的扩展。它包含一个或多个列,其中至少一列是几何列(geometry column),用于存储地理空间几何图形(如点、线、多边形等)。 GeoSeries:GeoPandas中的另一个重要数据结构,类似于Pandas的Series,但用于存储几何图形序列。 二、GeoPandas的功能特性 1. 读取和写入多种地理空间数据格式 GeoPandas支持读取和写入多种常见的地理空间数据格式,包括Shapefile、GeoJSON、PostGIS、KML等。这使得用户可以轻松地从各种数据源中加载地理空间数据,并将处理后的数据保存为所需的格式。 2. 地理空间几何图形的创建、编辑和分析 GeoPandas允许用户创建、编辑和分析地理空间几何图形,包括点、线、多边形等。它提供了丰富的空间操作函数,如缓冲区分析、交集、并集、差集等,使得用户可以方便地进行地理空间数据分析。 3. 数据可视化 GeoPandas内置了数据可视化功能,可以绘制地理空间数据的地图。用户可以使用matplotlib等库来进一步定制地图的样式和布局。 4. 空间连接和空间索引 GeoPandas支持空间连接操作,可以将两个GeoDataFrame按照空间关系(如相交、包含等)进行连接。此外,它还支持空间索引,可以提高地理空间数据查询的效率。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值