当我使用这个查询时,需要超过5分钟请给我一些其他建议

 SELECT * FROM
( SELECT  id,name,rownum AS RN$$_RowNumber FROM MILLION_1) INNER_TABLE where
 RN$$_RowNumber  > (V_total_count - V_no_of_rows)
 ORDER BY RN$$_RowNumber DESC;

最佳答案

试试offset子句。
我有一个表,其中大约有16M条记录,如果我只想要最后的100000行,我通过ORDERBY子句对它们进行排序,然后使用OFFSET子句,基本上就是说,在返回任何数据之前,先读取这么多行。
database - 如何在oracle的10000K表中查找最后的100k行?-LMLPHP

select *
  from SHERI; -- 15,691,544  Rows

select *
  from SHERI
 order by COLUMN4 asc
offset 15591444 rows; -- my math was bad, should have offset 15591544 rows to get just the last 100,000

fetch first和offset子句是12c的新子句(docs
如果我们查看此查询下的计划,可以看到数据库是如何工作的:
PLAN_TABLE_OUTPUT
SQL_ID  7wd4ra8pfu1vb, child number 0
-------------------------------------
select *   from SHERI  order by COLUMN4 asc offset 15591444 rows

Plan hash value: 3535161482

----------------------------------------------
| Id  | Operation           | Name  | E-Rows |
----------------------------------------------
|   0 | SELECT STATEMENT    |       |        |
|*  1 |  VIEW               |       |     15M|
|   2 |   WINDOW SORT       |       |     15M|
|   3 |    TABLE ACCESS FULL| SHERI |     15M|
----------------------------------------------

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

   1 - filter("from$_subquery$_002"."rowlimit_$$_rownumber">15591444)

Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

“窗口排序”基本上转化为一个分析函数

关于database - 如何在oracle的10000K表中查找最后的100k行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53154992/

10-14 01:45