2

我有一个名为 Student 的表,如下所示:

CREATE TABLE  "STUDENT" 
(   "ID" NUMBER(*,0), 
    "NAME" VARCHAR2(20), 
    "AGE" NUMBER(*,0), 
    "CITY" VARCHAR2(20), 
    PRIMARY KEY ("ID") ENABLE
)

我正在尝试获取年龄大于平均年龄的学生的所有记录。这是我尝试过的:

SELECT *
FROM student
WHERE age > AVG(age)

SELECT * 
FROM student
HAVING age > AVG(age)

两种方法都不起作用!

4

2 回答 2

9

如果您要使用没有分组的聚合,则不能引用其他字段。(你是*)

但是,您可以创建一个子查询。

SELECT *
FROM student
WHERE age > (SELECT AVG(age) FROM STUDENT)

这很容易编写和理解。但是,如果您使用分析函数,您可以获得更好的性能,正如Justin Cave 在他的回答中解释的那样

于 2012-10-15T20:33:50.243 回答
5

Conrad Fix 建议的子查询方法是常规方法。然而,这不太可能是最有效的方法,因为它需要 Oracle 访问表两次——一次计算平均年龄,一次撤回具有高于平均工资的行。如果您使用分析函数,您可以完成同样的事情,同时只需访问一次表并执行(大约)一半的逻辑 I/O 操作。

select *
  from (select s.*, avg(age) over () avg_age
          from student s)
 where age > avg_age

传统方法需要 18 次一致的获取,并且必须对表进行两次完整扫描(请注意,我运行了几次测试以获得最低值以排除延迟块清除等事情)

SQL> ed
Wrote file afiedt.buf

  1  select *
  2    from hr.employees
  3   where salary > (select avg(salary)
  4*                    from hr.employees)
SQL> /

51 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1945967906

---------------------------------------------------------------------------------
| Id  | Operation           | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |           |     5 |   345 |     6   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL  | EMPLOYEES |     5 |   345 |     3   (0)| 00:00:01 |
|   2 |   SORT AGGREGATE    |           |     1 |     4 |            |          |
|   3 |    TABLE ACCESS FULL| EMPLOYEES |   107 |   428 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("SALARY"> (SELECT AVG("SALARY") FROM "HR"."EMPLOYEES"
              "EMPLOYEES"))


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         18  consistent gets
          0  physical reads
          0  redo size
       5532  bytes sent via SQL*Net to client
        557  bytes received via SQL*Net from client
          5  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         51  rows processed

然而,分析函数方法在一次表扫描中做同样的事情,只有 7 个一致的 get

SQL> select *
  2    from (select e.*, avg(salary) over () avg_salary
  3            from hr.employees e)
  4   where salary > avg_salary
  5  /

51 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 48081388

---------------------------------------------------------------------------------
| Id  | Operation           | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |           |   107 | 15622 |     3   (0)| 00:00:01 |
|*  1 |  VIEW               |           |   107 | 15622 |     3   (0)| 00:00:01 |
|   2 |   WINDOW BUFFER     |           |   107 |  7383 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| EMPLOYEES |   107 |  7383 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("SALARY">"AVG_SALARY")


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          7  consistent gets
          0  physical reads
          0  redo size
       5220  bytes sent via SQL*Net to client
        557  bytes received via SQL*Net from client
          5  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
         51  rows processed

不过,正如 Conrad 指出的那样,解析函数方法需要排序,因此它应该比传统方法使用更多的 PGA。您将以减少的 I/O 换取增加的 RAM。通常这是一个理想的权衡,但这是您应该注意的事情。

于 2012-10-15T20:52:19.267 回答