oracle优化distinct,【大话IT】distinct sql改写优化

没有order by语句,rownum的结果不确定的

SQL> create table t as select level id,level||'a' name from dual connect by leve

l<=2e6;

Table created.

SQL> set lines 80 pages 50000

SQL> select bb.*  from (

2   select aa.*,rownum rn from

3      (select distinct  name,id from  t

4       where name is not null

5         and  id >=2015

6        )aa

7   where rownum<=10

8  )bb

9   where bb.rn>=1;

)bb

*

ERROR at line 8:

ORA-00911: invalid character

SQL>  select bb.*  from (

2   select aa.*,rownum rn from

3      (select distinct  name,id from  t

4       where name is not null

5         and  id >=2015

6        )aa

7   where rownum<=10

8  )bb

9   where bb.rn>=1;

NAME                                              ID         RN

----------------------------------------- ---------- ----------

2015a                                           2015          1

2016a                                           2016          2

2017a                                           2017          3

2018a                                           2018          4

2019a                                           2019          5

2020a                                           2020          6

2021a                                           2021          7

2022a                                           2022          8

2023a                                           2023          9

2024a                                           2024         10

10 rows selected.

SQL> set timi  on

SQL> select bb.*  from (

2   select aa.*,rownum rn from

3      (select distinct  name,id from  t

4       where name is not null

5         and  id >=2015 order by name

6        )aa

7   where rownum<=10

8  )bb

9   where bb.rn>=1;

NAME                                              ID         RN

----------------------------------------- ---------- ----------

1000000a                                     1000000          1

1000001a                                     1000001          2

1000002a                                     1000002          3

1000003a                                     1000003          4

1000004a                                     1000004          5

1000005a                                     1000005          6

1000006a                                     1000006          7

1000007a                                     1000007          8

1000008a                                     1000008          9

1000009a                                     1000009         10

10 rows selected.

Elapsed: 00:00:00.56

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值