在Oracle怎样查询表中的top10条记录呢

在Oracle怎样查询表中的top10条记录呢?

  select *

  from test

  where rownum <=10

  下面是关于rownum的介绍

  ================================

  Rownum和row_number() over()的使用

  ROWNUM是Oracle从8开始提供的一个伪列,是把SQL出来的结果进行编号,始终从1开始,常见的用途就是用来分页输出.

  比如

  SELECT *

  FROM torderdetail a

  WHERE ROWNUM <= 10

  这条语句就是输出前10条纪录,在这里用途上类似于sql sever的top,不过rownum对于指定编号区间的输出应该说更强大

  SELECT *

  FROM (SELECT a.*, ROWNUM rn

  FROM torderdetail a)

  WHERE rn >= 10 AND rn <= 20

  这条语句即是输出第10到第20条纪录,这里之所以用rownum rn,是把rownum转成实例,因为rownum本身只能用 <=的比较方式,只有转成实列,这样就可做 >=的比较了。

  在实际用途中,常常会要求取最近的几条纪录,这就需要先对纪录进行排序后再取rownum <=

  一般常见的

  SELECT *

  FROM (SELECT a.*

  FROM torderdetail a

  ORDER BY order_date DESC)

  WHERE ROWNUM <= 10

  而在CSDN曾经发生过讨论,关于取近的10条纪录,有人给出这样的语句

  SELECT a.*

  FROM torderdetail a

  WHERE ROWNUM <= 10

  ORDER BY order_date DESC

  之所以会出现这样的语句,主要是从效率上的考虑,前面条语句,是要进行全表扫描后再排序,然后再取10条纪录,后一条语句则不会全表扫描,只会取出10条纪录,很明显后条语句的效率会高许多。

  那为什么会有争议呢,那就在于在执行顺序上争议,是先执行排序取10条纪录,还是取10条纪录,再排序呢?两种顺序取出来的结果是截然相反的,先排序再取10条,就是取最近的10条,而先取10条,再排序,则取出的最早的10条纪录。对于此语句,普遍的认为执行顺序是先取10条纪录再排序的。所以此语句应该是错误。但实际上并非如此,此语句的执行顺序和order by的字段有关系,如果你order by 的字段是pk,则是先排序,再取10条(速度比第一种语句快),而排序字段不是PK 时,是先取10条再排序,此时结果就与要求不一样了,所以第二种写法一定要在排序字段是主键的情况下才能保证结果正确。

  Row_number() over()这个分析函数是从9I开始提供的,一般的用途和rownum差不多。

  一般写法row_number() over( order by order_date desc) 生成的顺序和rownum的语句一样,效率也一样(对于同样有order by 的rownum语句来说),所以在这种情况下两种用法是一样的。

  而对于分组后取最近的10条纪录,则是rownum无法实现的,这时只有row_number可以实现,row_number() over(partition by 分组字段 order by 排序字段)就能实现分组后编号,比如说要取近一个月的每天最后10个订单纪录

  SELECT *

  FROM (SELECT a.*,

  ROW_NUMBER () OVER (PARTITION BY TRUNC (order_date) ORDER BY order_date DESC)

  rn

  FROM torderdetail a)

  WHERE rn <= 10

  Rownum的另类用法,有时候我们会遇到这种需求,要求输出当月的所有天数,许多人会烦恼,数据库里又没有这样的表,怎么输出一个月的所有天数呢?用rownum就能解决:

  SELECT TRUNC (SYSDATE, 'MM') + ROWNUM - 1

  FROM DUAL

  CONNECT BY ROWNUM <= TO_NUMBER (TO_CHAR (LAST_DAY (SYSDATE), 'dd'))

原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/27/11420027.shtml
1、功能 A、获取top前10Oracle的进程,并打印出(根据实际进程情况列出 R/S 状态) B、对这些进行进行PS检查资源占有情况,并确定是那个数据库实例的进程 C、根据当前目录下的connect.json配置数据库连接数据库访问,打印执行SQL 2、作用 A、避免手工操作的多个代码输入检查 传统检查操作如下: X、通过top找出占用资源的Oracle进程 Y、通过ps确定这个Oracle进程所说实例 Z、连接对应的Oracle实例找出执行SQL B、进行了占用内存大小自动转换,快速查看占有大小是G、M、K 3、将来版本 A、提供Socket侦听,使用telnet直接可以查看 B、提供telnet扩展命令,直接查看性能与语句 C、支持远程进行的直接Kill功能 D、支持SQL直接执行相关请求,并返回结果 E、提供REST外部请求服务功能 [root@fj43 tmp]# ./RunApp.sh /var/tmp/connect.json Config loaded... cpu used:98.0% 11362 oracle 20 0 2231m 485m 482m R 98.4 1.6 2737:26 oracle <----top输出 12780 oracle 20 0 2230m 30m 27m S 1.9 0.1 27:33.12 oracle <----top输出 13673 oracle 20 0 2248m 78m 75m S 1.9 0.3 0:01.29 oracle <----top输出 13980 oracle 20 0 2248m 93m 90m S 1.9 0.3 0:01.55 oracle <----top输出 18135 oracle 20 0 2251m 364m 357m S 1.9 1.2 2:20.23 oracle <----top输出 25677 oracle 20 0 2231m 31m 31m S 1.9 0.1 8:27.59 oracle <----top输出 PROC=11362 PerCPU=99.4 PerMEM=1.6 VMEM=2.0G RMEM=485.0M TIME=2737:26 CMD=[ora_j002_MG01] <----ps输出 PROC=12780 PerCPU=0.0 PerMEM=0.1 VMEM=2.0G RMEM=30.0M TIME=27:33 CMD=[ora_dia0_CSHQ] <----ps输出 PROC=13673 PerCPU=0.1 PerMEM=0.2 VMEM=2.0G RMEM=78.0M TIME=0:01 CMD=[ora_j001_CSGFC] <----ps输出 PROC=13980 PerCPU=0.2 PerMEM=0.3 VMEM=2.0G RMEM=93.0M TIME=0:01 CMD=[ora_j000_CSGFC] <----ps输出 PROC=18135 PerCPU=0.0 PerMEM=1.2 VMEM=2.0G RMEM=364.0M TIME=2:20 CMD=[ora_cjq0_FTFIN] <----ps输出 PROC=25677 PerCPU=0.0 PerMEM=0.1 VMEM=2.0G RMEM=31.0M TIME=8:27 CMD=[ora_pmon_CENTER] <----ps输出 jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:CSHQ username=DBUSRBMS password=**** count=1 <----数据库连接 not sql runing <----当前未执行SQL jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:MG01 username=DBUSROMS1 password=**** count=1 <----数据库连接 DELETE FROM SELLMAIL WHERE SMKEY = :B1 <----当前正执行SQL jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:CENTER username=DBUSRDAT password=**** count=1 <----数据库连接 not sql runing <----当前未执行SQL jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:CSGFC username=DBUSRDMS password=**** count=2 <----数据库连接 not sql runing <----当前未执行SQL not sql runing <----当前未执行SQL jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:FTFIN username=DBUSRFIN password=**** count=1 <----数据库连接 not sql runing <----当前未执行SQL [root@fj43 tmp]# 改进版输出情况: [root@dxs1 tmp]# RunApp.sh /var/tmp/connect.json Config loaded... cpu used:99.1% 12041 oracle 25 0 3467m 157m 152m R 99.0 1.0 2:03.69 oracle 12144 oracle 15 0 3463m 95m 93m S 9.9 0.6 0:00.38 oracle 12167 oracle 15 0 3463m 37m 34m S 4.0 0.2 0:00.04 oracle PROC=12041 PerCPU=87.1 PerMEM=0.9 VMEM=3.0G RMEM=157.0M TIME=2:03 CMD=[oracleMG01 (LOCAL=NO)] PROC=12144 PerCPU=9.5 PerMEM=0.6 VMEM=3.0G RMEM=96.0M TIME=0:00 CMD=[oracleMG01 (LOCAL=NO)] PROC=12167 PerCPU=4.0 PerMEM=0.2 VMEM=3.0G RMEM=37.0M TIME=0:00 CMD=[oracleMG01 (LOCAL=NO)] jdbcUrl=jdbc:oracle:thin:@193.0.0.77:1521:MG01 username=DBUSROMS1 password=**** count=3 SELECT RAWTOHEX(TB.SQL_ADDRESS) SQL_ADDRESS,TB.SQL_HASH_VALUE,TB.SID FROM v$process TA INNER JOIN v$session TB ON TA.addr=TB.paddr WHERE TA.spid='12167' AND TB.sql_address!='00' not sql runing SELECT RAWTOHEX(TB.SQL_ADDRESS) SQL_ADDRESS,TB.SQL_HASH_VALUE,TB.SID FROM v$process TA INNER JOIN v$session TB ON TA.addr=TB.paddr WHERE TA.spid='12041' AND TB.sql_address!='00' session id=13 SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES WHERE ADDRESS = HEXTORAW('000000007C9641F0') AND HASH_VALUE = 2890562617 ORDER BY PIECE /* concatenate */ Update goods a Set (goodsid,obligatestr1,goodsflag,fgoodsid,code number,codegrade)= (Select goodsid,obligatestr1,goodsflag,fgood sid,codenumber,codegrade From goods_20130720 b Where a.goodsid=b .goodsid) SELECT RAWTOHEX(TB.SQL_ADDRESS) SQL_ADDRESS,TB.SQL_HASH_VALUE,TB.SID FROM v$process TA INNER JOIN v$session TB ON TA.addr=TB.paddr WHERE TA.spid='12144' AND TB.sql_address!='00' session id=475 SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES WHERE ADDRESS = HEXTORAW('000000007C973940') AND HASH_VALUE = 3246036779 ORDER BY PIECE /* concatenate */ INSERT INTO "DBUSROTHER"."FJ_SALEDETAIL" ("SDMKTNO","SDCOUNTER" ,"SDBILLNO","SDROWNO","SDPOSNO","SDINVNO","SDDATE","SDSWAPDATE", "SDSELLER","SDSELLERNM","SDGDID","SDBARCODE","SDGNM","SDUID","SD UNIT","SDSPEC","SDSALEQNT","SDSALEPRICE","SDSALEAMT","SDSWAPPRIC E","SDSWAPAMT","SDPOPDISAMT","SDPOPDISPARTAKE","SDPOPBILLNO","SD CUSTDISAMT","SDGRANTZK","SDGRANTNO","SDSORT","SDANALCODE","SDOLD DATE","SDOLDPOSNO","SDOLDINVNO","SDRETREASON","SDTAXRATE","SDINP BARCODE","SDDISTOTAL","SDMULTOTAL","SDKJKCBZ","SDSPML","SDBATCHN O","SDPROMPRICE") SELECT "A2"."SDMKTNO","A2"."SDCOUNTER",TO_CHAR ("A2"."SDSWAPDATE",'yymmdd')||SUBSTRB("A2"."SDMKTNO",(-3))||SUBS TRB("A2"."SDPOSNO",(-2))||SUBSTRB(TO_CHAR("A2"."SDINVNO",'000000 00'),(-4)),"A2"."SDROWNO","A2"."SDPOSNO","A2"."SDINVNO","A2"."SD DATE","A2"."SDSWAPDATE","A2"."SDSELLER","A2"."SDSELLERNM","A2"." SDGDID","A2"."SDBARCODE","A2"."SDGNM","A2"."SDUID","A2"."SDUNIT" ,"A2"."SDSPEC","A2"."SDSALEQNT","A2"."SDSALEPRICE","A2"."SDSALEA MT","A2"."SDSWAPPRICE","A2"."SDSWAPAMT","A2"."SDPOPDISAMT","A2". "SDPOPDISPARTAKE","A2"."SDPOPBILLNO","A2"."SDCUSTDISAMT","A2"."S DGRANTZK","A2"."SDGRANTNO","A2"."SDSORT","A2"."SDANALCODE","A2". "SDOLDDATE","A2"."SDOLDPOSNO","A2"."SDOLDINVNO","A2"."SDRETREASO N","A2"."SDTAXRATE","A2"."SDINPBARCODE","A2"."SDDISTOTAL","A2"." SDMULTOTAL","A2"."SDKJKCBZ","A2"."SDSPML",DECODE(TRIM("A2"."SDBA TCHNO"),'N',NULL,"A2"."SDBATCHNO"),"A2"."SDPROMPRICE" FROM "POS" ."SALEDETAIL"@! "A2" WHERE TO_CHAR("A2"."SDSWAPDATE",'yymmdd')|| SUBSTRB("A2"."SDMKTNO",(-3))||SUBSTRB("A2"."SDPOSNO",(-2))||SUBS TRB(TO_CHAR("A2"."SDINVNO",'00000000'),(-4))='130720002043323' [root@dxs1 tmp]# RunApp.sh /var/tmp/connect.json Config loaded... cpu used:196.6% 12041 oracle 25 0 3467m 168m 163m R 98.9 1.1 5:10.29 oracle 12287 oracle 19 0 3465m 102m 97m R 98.9 0.6 0:05.46 oracle PROC=12041 PerCPU=94.3 PerMEM=1.0 VMEM=3.0G RMEM=168.0M TIME=5:10 CMD=[oracleMG01 (LOCAL=NO)] PROC=12287 PerCPU=91.8 PerMEM=0.6 VMEM=3.0G RMEM=102.0M TIME=0:05 CMD=[ora_j001_MG01] jdbcUrl=jdbc:oracle:thin:@193.0.0.77:1521:MG01 username=DBUSROMS1 password=**** count=2 SELECT RAWTOHEX(TB.SQL_ADDRESS) SQL_ADDRESS,TB.SQL_HASH_VALUE,TB.SID FROM v$process TA INNER JOIN v$session TB ON TA.addr=TB.paddr WHERE TA.spid='12041' AND TB.sql_address!='00' session id=13 SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES WHERE ADDRESS = HEXTORAW('000000007C9641F0') AND HASH_VALUE = 2890562617 ORDER BY PIECE /* concatenate */ Update goods a Set (goodsid,obligatestr1,goodsflag,fgoodsid,code number,codegrade)= (Select goodsid,obligatestr1,goodsflag,fgood sid,codenumber,codegrade From goods_20130720 b Where a.goodsid=b .goodsid) SELECT RAWTOHEX(TB.SQL_ADDRESS) SQL_ADDRESS,TB.SQL_HASH_VALUE,TB.SID FROM v$process TA INNER JOIN v$session TB ON TA.addr=TB.paddr WHERE TA.spid='12287' AND TB.sql_address!='00' session id=327 SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES WHERE ADDRESS = HEXTORAW('0000000081A93490') AND HASH_VALUE = 2457621776 ORDER BY PIECE /* concatenate */ INSERT INTO SELLPAYMENT SELECT TO_NUMBER(SPBILLNO), SPPAYCODE, S UM(SPPAYMENTAMT), SPRATE, SPNO, SPPAYERID FROM SALEPAYMENT WHERE SPBILLNO = :B3 AND SPPOSNO = :B2 AND SPINVNO = :B1 GROUP BY SPB ILLNO, SPPAYCODE, SPRATE, SPNO, SPPAYERID [root@dxs1 tmp]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值