7.10 GROUP BY的限制 (page 217)
Your study of GROUP BY would be incomplete without considering what it cannot do. The list of restrictions placed on GROUP BY is not very long. The restrictions are listed in the Oracle 11.2 SQL Language Reference for Oracle 11.2. For example:
• LOB columns, nested tables, or arrays may not be used as part of a GROUP BY expression.
• Scalar subquery expressions are not allowed.
• Queries cannot be parallelized if the GROUP BY clause references any object type columns.
如果你不知道GROUP BY不能做什么,那么你对它的学习是不完整的。对GROUP BY的限制列表不是很长。在Oracle 11.2SQL语言参考列出了对Oracle 11.2的限制。例如:
• LOB 列,嵌套表, 或者数组可能不能被用做GROUP BY表达式的一部分。
• 标量子查询也是不允许的。
• 如果GROUP BY子句引用任何对象类型的列,查询不能并行化。
SQL queries were constructed to demonstrate the first two restrictions as shown in Listing 7-20 and 7-21. The error messages clearly show that LOB columns and scalar subqueries cannot be used as part of GROUP BY clause.
列表7-20和7-21所示,构造的SQL查询演示了前两个限制。错误消息清晰的说明LOB列和标量子查询不能被用做GROUP BY子句的一部分。
Listing 7-20. GROUP BY Restrictions – LOB Not Allowed
SQL> @l_7_2
1 with lobtest as (
2 select to_clob(d.dname )
dname
3 from scott.emp e
4 join scott.dept d on d.deptno = e.deptno
5 )
6 select l.dname
7 from lobtest l
8* group by l.dname
group by l.dname;
*
ERROR at line 8:
ORA-00932: inconsistent datatypes: expected - got CLOB
Listing 7-21. GROUP BY Restrictions – Scalar Subquery Not Allowed
1 select d.dname, count(empno) empcount
2 from scott.emp e
3 join scott.dept d on d.deptno = e.deptno
4 group by (select dname from scott.dept d2 where d2.dname = d.dname)
5 order by d.dname;
group by (select dname from scott.dept d2 where d2.dname = d.dname);
*
ERROR at line 4:
ORA-22818: subquery expressions not allowed here
The final restriction listed appears to be a documentation error. Evidence for that can be seen in
Listing 7-22 where the GROUP BY on an OBJECT datatype is being executed in parallel, contrary to what the documentation states. The member function match in the dept_location type body is used to compare the value for city, and this in turn is used by GROUP BY to group employees by CITY.
Should you need to create aggregations based on data in an OBJECT column, you can certainly do so as of Oracle 11.1.0.7. Testing has shown that the GROUP BY of Listing 7-22 will not be executed in parallel in Oracle 11.1.0.6.
所列的最后一个限制看起来像一个文档错误。列表7-22所示证实,GROUP BY的OBJECT数据类型并行执行了,相比文档所述。dept_location_type体中的成员函数match用于比较city的值,进而又被GROUP BY用于按CITY分组employees。
你能基于一个OBJECT列的数据创建聚合么?Oracle 11.1.0.7一定能做到。测试显示列表7-22中的GROUP BY在Oracle 11.1.0.6中不能并行执行。
Listing 7-22. GROUP BY on Object Column in Parallel
SQL> create type dept_location_type
2 as object
3 (
4 street_address VARCHAR2(40)
5 , postal_code VARCHAR2(10)
6 , city VARCHAR2(30)
7 , state_province VARCHAR2(10)
8 , country_id CHAR(2)
9 , order member function match (e dept_location_type) return integer
10 );
11 /
Type created.
SQL>
SQL> create or replace type body dept_location_type
2 as order member function match (e dept_location_type) return integer
3 is
4 begin
5 if city < e.city then
6 return -1;
7 elsif city > e.city then
8 return 1;
9 else
10 return 0;
11 end if;
12 end;
13 end;
14 /
Type body created.
SQL>
SQL> create table deptobj
2 as
3 select d.deptno,d.dname
4 from scott.dept d;
Table created.
SQL> alter table deptobj add (dept_location dept_location_type);
Table altered.
SQL> update deptobj set dept_location =
2 dept_location_type('1234 Money St', '97401','Eugene', 'OR', 'US')
3 where deptno=20;
1 row updated.
SQL> update deptobj set dept_location =
2 dept_location_type('459 Durella Street', '97463','Oakridge', 'OR', 'US')
3 where deptno=40;
1 row updated.
SQL> update deptobj set dept_location =
2 dept_location_type('12642 Rex Rd', '97006','Beavertown', 'OR', 'US')
3 where deptno=10;
1 row updated.
SQL> update deptobj set dept_location =
2 dept_location_type('9298 Hamilton Rd', '97140','George', 'WA', 'US')
3 where deptno=30;
1 row updated.
1 commit;
Commit complete.
PL/SQL procedure successfully completed.
1 select /*+ gather_plan_statistics parallel(e 2)*/
2 d.dept_location, count(e.ename) ecount
3 from scott.emp e, deptobj
d
4 where e.deptno = d.deptno
5 group by dept_location
6 order by dept_location
; -- (按match函数的返回值,分组,排序)
DEPT_LOCATION(STREET_ADDRESS, POSTAL_CODE, CITY, STATE_PROVI ECOUNT
------------------------------------------------------------ ------
DEPT_LOCATION_TYPE('1234 Money St', '97401', 'Eugene', 'OR', 'US') 5
DEPT_LOCATION_TYPE('12642 Rex Rd', '97006', 'Beavertown','OR','US') 3
DEPT_LOCATION_TYPE('9298 Hamilton Rd', '97140', 'George','WA','US') 6
3 rows selected.
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 3 |
| 1 | PX COORDINATOR | | 1 | | 3 |
| 2 | PX SEND QC (ORDER) | :TQ10002 | 0 | 14 | 0 |
| 3 | SORT GROUP BY | | 0 | 14 | 0 |
| 4 | PX RECEIVE | | 0 | 14 | 0 |
| 5 | PX SEND RANGE | :TQ10001 | 0 | 14 | 0 |
| 6 | HASH GROUP BY | | 0 | 14 | 0 |
|* 7 | HASH JOIN | | 0 | 14 | 0 |
| 8 | BUFFER SORT | | 0 | | 0 |
| 9 | PX RECEIVE | | 0 | 4 | 0 |
| 10 | PX SEND BROADCAST | :TQ10000 | 0 | 4 | 0 |
| 11 | TABLE ACCESS FULL | DEPTOBJ | 1 | 4 | 4 |
| 12 | PX BLOCK ITERATOR | | 0 | 14 | 0 |
|* 13 | TABLE ACCESS FULL | EMP | 0 | 14 | 0 |
---------------------------------------------------------------------------
(译者注:使用Oracle 11.2.0运行上面的查询,报错:ORA-12801:并行查询服务器P001中发出错误信号 ORA-04063: type bod "SYS.DEPT_LOCATION_TYPE"有错误 )
Summary
Oracle has provided some excellent tools for the SQL practitioner in the form of extensions to the GROUP BY clause. Not only do they reduce code, they improve database efficiency. They do, however, take some dedication and practice to learn how best to use them. The introduction here to advanced grouping features is by no means comprehensive. Most of these features can be combined for many different effects, far more than is practical to include in a book. Please endeavor to make use of these features in your own applications and continue to experiment with them based on what you have learned here.
总结
Oracle已经为SQL实践者以GROUP BY扩展式的形式提供了一些卓越的工具。不仅可以减少代码量而且能提高数据库的性能。它们需要你花些时间和精力来学习如何最佳使用它们。这里所介绍的高级分组特性并不复杂。大部分这些特性能组合而形成多种效果,远超出一本书所包含的实例。请在你自己的应用中努力用到这些特性,把你在这里所学的不断应用于实践。
分享到:
相关推荐
MS-DOS.7.10完整安装光盘ISO版
【VMware Workstation安装MS-DOS V7.10界面】教程主要涵盖了在虚拟环境中安装老版本MS-DOS操作系统的过程。VMware Workstation是一款强大的虚拟机软件,它允许用户在单个计算机上运行多个操作系统,而无需物理硬件的...
在本例中,我们讨论的是OpenJDK 11.0.7的特定版本,11.0.7.10-1,这是Red Hat发布的一个更新,特别针对Windows x86_64架构。 OpenJDK 11是一个长期支持(LTS)版本,这意味着它将得到更长时间的技术支持,这对于...
ISO文件MS-DOS.7.10.DOS7_CD
《PyPI官网下载:探索skitai-0.28.7.10.tar.gz的Python库魅力》 PyPI(Python Package Index)是Python开发者的重要资源库,它提供了丰富的Python库供全球开发者下载和使用。本文将深入探讨标题中的"skitai-0.28....
VMware下安装MS-DOS7.10的镜像文件 vmware 虚拟机 dos 工具
"OpenNI-Win64-1.5.7.10"是针对Windows 64位操作系统的OpenNI版本,编号为1.5.7.10。这个版本可能包含了对64位系统的优化和支持,使得在高性能计算平台上运行更加稳定和高效。值得注意的是,这个特定的版本可能较旧...
VMware Horizon 7.10 是一款强大的虚拟桌面基础设施(VDI)解决方案,它提供了集中管理和交付虚拟桌面和应用程序的功能。在本手册中,我们将聚焦于Horizon Composer服务器的安装和配置,这是一个关键组件,用于高效...
标题中的"drools-wb-7.10 tomcat 安装包"指的是Drools Workbench的一个版本,这是Red Hat公司开发的一款基于Java的工作台,用于规则引擎Drools和决策服务KieServer的管理和开发。Drools是一款强大的业务规则管理系统...
在提供的压缩包文件名称“dex2jar-0.0.7.10-SNAPSHOT”中,我们可以推断这可能是dex2jar工具的一个版本号。"0.0.7.10"表示这是工具的第7次主要更新,第10次要修正。"SNAPSHOT"通常用于标识这是一个开发版本,意味着...
参考资料-4.2.7.10 室内采暖管道及配件安装检验批质量验收表.zip
【MS-DOS 7.10 完整安装启动光盘.ISO】是一个经典的系统镜像文件,主要用于在特定条件下安装或恢复MS-DOS 7.10操作系统。这个ISO文件包含了完整的MS-DOS 7.10系统,能够通过光盘启动,尤其适用于那些仅支持光驱启动...
【MS-DOS 7.10 完整安装光盘ISO版】是一个历史悠久的操作系统版本,主要用于个人计算机的早期时代。这个压缩包包含了MS-DOS 7.10的安装程序以及相关的学习和示例资源。DOS是“磁盘操作系统”(Disk Operating System...
7. **故障恢复**:DOS 7.10 提供了基本的故障检测和修复工具,如CHKDSK,可以帮助用户解决常见的系统问题。 在ISO文件中,"ms-dos7.10_veryhuo.com"很可能是包含MS-DOS 7.10完整安装程序的镜像文件。用户通常需要...
MS-DOS 7.10 完整安装版说明 ************************** MS-DOS 7.10介绍 =============== MS-DOS 7.10是目前功能最强大实用,且兼容性最好的DOS。而且由于它全面支持大硬盘、大内存、长文件名(LFN)、FAT32分区...
7. 检验方法:详细说明如何进行各项检验,例如目测、工具测量、通电试验等。 8. 验收结果:记录每项检验的合格或不合格情况,并由相关人员签字确认。 9. 备注:对于特殊情况或需要整改的地方进行说明。 验收表的...
MS-DOS 7.10 是微软公司推出的个人计算机操作系统的一个重要版本,它在1995年随着Windows 95一同发布。MS-DOS,全称Microsoft Disk Operating System,是1980年代和1990年代初期PC领域的主导操作系统。这个系统以其...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装