Monday 15 June 2015

sql - Select `n` last inserted records in table - oracle -


Unfortunately, the sequence used to generate the key for some other tables is the primary key generated in sequence in the table. (I did not design it and I can not change it).

What is the fastest way to enter the last n , what is the order in Oracle, ordered by ID (placed on top) in descending order?

n is a relatively small number - the number of records displayed on the page - probably not greater than 50

The table now has 30.000.000 records

edit:
In response to a comment: This question was inspired:

With the execution plan for the query:

  Select from the MyTable order based on priority  

The execution plan was:

  - ------------------------------------------- - | ID | Operation | Name | --------------------------------------------- | 0 | Select Command | | | 1 | Sort order by | | | 2 | Table access complete. Mitebab | ---------------------------------------------  < / Ex> 

I was surprised that Oracle wants to scan and sort the entire table when it has an index on the sort field.

Uses the query index from the accepted answer and sorts.

Editing 2:
Comment again APC: was the part of the trim, which surprised me I expected the Oracle to retrieve the rows in the expected order Will use. Execution plan for query: Choose from

  * (choose * from arh_promjene command to promjena_id desc) x where rownum & lt; 50000000  

The full table uses index instead of access and sort (notice status rownum <50.000.000 - this table and recorded in Oracle Knows more than the number that he should get all the records from the table). This query returns all the rows as the first query, but with the following execution scheme:

  | ID | Operation | Name | -------------------------------------------------- ----- | 0 | Select Command | | | * 1 | COUNT STOPKEY | | | 2 | See | | | 3 | Table access by INDEX ROWID. Mitebab | | 4 | Index Full scan designing. SYS_C008809 | Retrospective information (identification by operation ID): ----------------------------------------- ---------- 1 - Filter (ROWNUM & lt; 50000000)  

It was unusual for me that Oracle was making separate execution plans for these two questions Set which are essentially returning the same results.

Edit 3: Remarks of Amok:

Oracle does not know that 50M is more than the number of rows, of course, there are statistics in it , But they can be old and wrong - and Oracle does not allow himself to give wrong results because only the figures are wrong.

Are you sure? In Oracle editions, it was recommended to manually update the data from time to time up to 9. Oracle updates statistics automatically since version 10. If Oracle does not use it for query optimization, then what is the use of data statistics?

Use ROWNUM :

  Select * to (choose from foo order * by bork) x where ROWNUM < = N  

Note that rownum applies before sorting for a subquery, that is why you have two nested queries If needed, otherwise you will only get the random rows.


No comments:

Post a Comment