Performance between SELECT MAX (col_name) and ROWNUM = 1

I doubt the speed and result of the below queries. Can someone give me an explanation on them? (These queries are written for an Oracle database)

Let's say I have a table table1 (ID, itemID, trnx_date, balance, ...) . I want to get the last item balance.

Request 1:

SELECT balance FROM table1 WHERE ID = (SELECT MAX (ID) from table1 WHERE itemID = item_id);

Request 2:

SELECT balance FROM table1 WHERE itemID = item_id AND rownum = 1 ORDER BY ID DESC;

where item_id is a variable.

So, do these two queries perform the same result? And which one is faster or is there any other query that is faster than them?

thanks

+3
1

Rownum Oracle ( , Oracle ). , :

SELECT 
    balance
FROM 
    (SELECT balance FROM table1 WHERE itemID = *item_id* ORDER BY ID DESC)
WHERE
    rownum = 1;

, ID, .

, ?

+2

Source: https://habr.com/ru/post/1711839/


All Articles