1

when i am using this query it is taking more than 5 mins please give me some other suggestion

 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;
aastha
  • 11
  • 3
  • 1
    use `TOP` and `ORDER BY DESC` instead of `ROW_Number` – Marian Nasry Nov 05 '18 at 13:07
  • 1
    what order do you expect? Since you do not define an order by in your subquery the rownum is a rather arbitrary number. – Martin Preiss Nov 05 '18 at 13:16
  • 2
    There's no such thing as "last 100k rows" in database as the records are stored in no particular order by default. You have to tell us precisely last 100k in "which" order ? – Kaushik Nayak Nov 05 '18 at 13:39
  • Actually we dont know the table structure as tables may vary from client to client we just have to give them the last limited rows according to insertion order. we receive the column names,table name and how many rows they want to fetch . If we use rownum full table scanned.RowId can help me in this or their is any way to fetch the last rows. One more thing is that they uses all versions of oracle so the solution should be generic – aastha Nov 06 '18 at 05:41
  • If you don't know the table structure, you don't know the insertion order, which means you simply *cannot* get the "last" 100k records. – Jeffrey Kemp Nov 07 '18 at 06:13

2 Answers2

1

Try the offset clause.

I have a table with about 16M records in it, if i just want the last 100,000 rows, I ORDER them via the ORDER BY clause, and then I use the OFFSET clause, which basically says, read this many rows first, before you return any data.

enter image description here

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 

The FETCH FIRST and OFFSET clauses are new for 12c (docs)

If we look at the plan under this query, we can see how the database makes it work:

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   

'window sort' basically translates to, an analytic function

thatjeffsmith
  • 15,972
  • 2
  • 27
  • 87
  • Actually we dont know the table structure as tables may vary from client to client we just have to give them the last limited rows according to insertion order. we receive the column names,table name and how many rows they want to fetch . If we use rownum full table scanned.RowId can help me in this or their is any way to fetch the last rows. One more thing is that they uses all versions of oracle so the solution should be generic – aastha Nov 06 '18 at 07:09
  • table structure doesn't matter, get size of table and then skip to the last 100k rows. The sooner you get your clients to supported versions of Oracle the better - this is one feature you have to look forward to ... btw, there's no gurantee that rows are stored in 'insertion order' - not unless you track that and order by that yourself in the query – thatjeffsmith Nov 06 '18 at 14:14
0

There are some very thorough answers at this similar question, but I'll try to make them specific to your case.

First, when you say "last 100k rows", what do you mean? It looks like you just want to pull the last 100k rows from an unsorted query, but that doesn't make a lot of sense. If you want the 100k most recent rows, Oracle doesn't guarantee that they'll be at the end of your unsorted query. So you want to order by something which will have the most recent ones at the end.

Also, part of the reason your query is slow is that you're sorting/filtering on the rownum pseudo-column, which can't be indexed. Sorting on a column that has an index would drastically speed this up. So I'd guess you want to order by the id column, which is probably a unique/primary key.

So this is the old (11g and earlier) way to do this.

select id, name
from (select id, name
      from MILLION_1
      order by id desc)
where rownum < 100000;

If you're on 12c or later, there's a newer way to do it.

select id, name
from MILLION_1
order by id desc
fetch first 100000 rows only;
kfinity
  • 6,703
  • 1
  • 9
  • 17
  • Actually we dont know the table structure as tables may vary from client to client we just have to give them the last limited rows according to insertion order. we receive the column names,table name and how many rows they want to fetch . If we use rownum full table scanned.RowId can help me in this or their is any way to fetch the last rows. One more thing is that they uses all versions of oracle so the solution should be generic. – aastha Nov 06 '18 at 05:39