十万大小的数据集,要从中选出最大的十个元素怎么做呢?

怎么做,就是千万不要去写程序,因为你Oracle执行完成你才够打开编译器的.

Solution:

复制代码
SQL> select b.* from (select a.*,rownum as rn from (select * from bigemp order by salary desc) a) b where b.rn<11;

        ID     SALARY         RN
---------- ---------- ----------
     58299      99998          1
     81565      99998          2
      8781      99996          3
     11017      99996          4
     46760      99996          5
     72216      99994          6
     52250      99993          7
     77820      99992          8
     19348      99990          9
     83024      99990         10

已选择10行。

已用时间:  00: 00: 00.06
复制代码

0.06秒,人家已经搞定了.这还是没有索引的情况!

所有想把十万数据集从DB取出来用Java去算的都面壁三分钟去吧.

全部SQL:

复制代码
SQL> create table bigemp(
  2      id number(6,0) primary key,
  3      salary number(6,0) not null);

表已创建。


SQL> insert into bigemp
  2  select rownum,dbms_random.value(0,100000) from dual
  3  connect by level<=100000;

已创建100000行。

SQL> commit;

提交完成。

SQL> select b.* from (select a.*,rownum as rn from (select * from bigemp order by salary desc) a) b where b.rn<11;

        ID     SALARY         RN
---------- ---------- ----------
     58299      99998          1
     81565      99998          2
      8781      99996          3
     11017      99996          4
     46760      99996          5
     72216      99994          6
     52250      99993          7
     77820      99992          8
     19348      99990          9
     83024      99990         10

已选择10行。

SQL> set timing on;
SQL> select b.* from (select a.*,rownum as rn from (select * from bigemp order by salary desc) a) b where b.rn<11;

        ID     SALARY         RN
---------- ---------- ----------
     58299      99998          1
     81565      99998          2
      8781      99996          3
     11017      99996          4
     46760      99996          5
     72216      99994          6
     52250      99993          7
     77820      99992          8
     19348      99990          9
     83024      99990         10

已选择10行。

已用时间:  00: 00: 00.06
复制代码

--2020年5月11日--

posted @   逆火狂飙  阅读(666)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示