1

给定这张表:

x   y
--  -
10  a
20  b
30  c

我想要映射值的最佳方式

[10,20) -> a
[20,30) -> b
[30,inf) -> c

现在我正在使用如下查询:

select y from foo
 where x=(select max(x) from foo
           where x<=21);

有一个更好的方法吗?是否有可能有帮助的分析功能?

这是我的测试用例:

create table foo as
select 10 as x ,'a' as y from dual union
select 20,'b' from dual union
select 30,'c' from dual;

-- returns: a,b,b:
select y from foo where x=(select max(x) from foo where x<=19);
select y from foo where x=(select max(x) from foo where x<=20);
select y from foo where x=(select max(x) from foo where x<=21);
4

3 回答 3

3

通过使用 MAX-KEEP 聚合函数,您可以重写查询以仅访问 foo 表一次而不是两次。

一个例子:

SQL> var N number
SQL> exec :N := 19

PL/SQL-procedure is geslaagd.

SQL> select max(y) keep (dense_rank last order by x) y
  2    from foo
  3   where x <= :N
  4  /

Y
-
a

1 rij is geselecteerd.

SQL> exec :N := 20

PL/SQL-procedure is geslaagd.

SQL> select max(y) keep (dense_rank last order by x) y
  2    from foo
  3   where x <= :N
  4  /

Y
-
b

1 rij is geselecteerd.

SQL> exec :N := 21

PL/SQL-procedure is geslaagd.

SQL> select max(y) keep (dense_rank last order by x) y
  2    from foo
  3   where x <= :N
  4  /

Y
-
b

1 rij is geselecteerd.

结果也是a,b,b。查询计划:

SQL> set serveroutput off
SQL> select /*+ gather_plan_statistics */
  2         y
  3    from foo
  4   where x = (select max(x) from foo where x<=:N)
  5  /

Y
-
b

1 rij is geselecteerd.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'predicate -note last'))
  2  /

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------
SQL_ID  3kh85qqnb2phy, child number 0
-------------------------------------
select /*+ gather_plan_statistics */        y   from foo  where x =
(select max(x) from foo where x<=:N)

Plan hash value: 763646971

----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |       |       |     8 (100)|          |
|*  1 |  TABLE ACCESS FULL  | FOO  |     1 |    16 |     4   (0)| 00:00:01 |
|   2 |   SORT AGGREGATE    |      |     1 |    13 |            |          |
|*  3 |    TABLE ACCESS FULL| FOO  |     2 |    26 |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------

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

   1 - filter("X"=)
   3 - filter("X"<=:N)


22 rijen zijn geselecteerd.

SQL> select max(y) keep (dense_rank last order by x) y
  2    from foo
  3   where x <= :N
  4  /

Y
-
b

1 rij is geselecteerd.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'predicate -note last'))
  2  /

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------
SQL_ID  avm2zh62c8cwd, child number 0
-------------------------------------
select max(y) keep (dense_rank last order by x) y   from foo  where x
<= :N

Plan hash value: 3274996510

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |       |       |     4 (100)|          |
|   1 |  SORT AGGREGATE    |      |     1 |    16 |            |          |
|*  2 |   TABLE ACCESS FULL| FOO  |     1 |    16 |     4   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   2 - filter("X"<=:N)


20 rijen zijn geselecteerd.

对 foo 进行两次全表扫描,针对新查询进行一次。

问候,罗布。

于 2009-07-06T09:44:46.830 回答
1
select distinct first_value(y) over (order by x desc) from foo where x<=19;
select distinct first_value(y) over (order by x desc) from foo where x<=20;
select distinct first_value(y) over (order by x desc) from foo where x<=21;

另外:x 上的索引可能是个好主意。

于 2009-07-06T06:17:51.287 回答
1

这是通过usenet收到的另一个答案。到目前为止,这似乎是最有效的执行。

select max(y) keep (dense_rank last order by x) from foo where x<=21;
于 2009-07-06T07:01:13.400 回答