Oracle ROWNUM is a pseudocolumn (not a real column) that is generated
when running a select.
ROWNUM will be assigned the numbers 1, 2, 3, 4, ... N.
For each row combination returned by a query, ROWNUM returns a number indicating the order in which Oracle selects the rows from the table (tables)
The first row combination selected has a ROWNUM of 1, the second has 2, and so
on.
When used in a sub query, the subquery will generate a new ROWNUM sequence for
each sub query execution.
The ROWNUM pseudocolumn should be implemented under SQL_MODE=ORACLE.
In many cases one could also replace WHERE ROWNUM <= X with LIMIT X.
The most common usage of rownum is to limit the number of rows in the query:
SELECT * fromtablewhere rownum < 10;
Examples:
createtable t1 (a intprimarykey);
createtable t2 (b intprimarykey);
insertinto t1 values(1),(2),(3);
insertinto t2 values(1),(2),(3);
select t1.a, (select t2.b from t2 where t1.a=t2.b and rownum < t1.a) from t1;
The following query is undefined (can return any rows) as rownum is generated before ORDER BY is done and there is no guarantee in which order the database is returning rows.
Select * from t1 orderby t1.a deschaving rownum <= 2;
Authentic Oracle behavior:
Prepare the data
createtable t1(c1 number, c2 number);
createtable t2(c1 number, c2 number);
createtable t3(c1 number, c2 number);
insertinto t1 values(1,1);
insertinto t1 values(2,2);
insertinto t1 values(3,3);
insertinto t2 values(1,1);
insertinto t2 values(2,2);
insertinto t2 values(3,3);
insertinto t3 values(1,1);
insertinto t3 values(2,2);
insertinto t3 values(3,3);
Run queries
SQL> select * from t1 where rownum<2;
C1 C2
1 1
SQL> select t1.c1, t1.c2 ,rownum from t1 left join t2 on t1.c1=t2.c1 inner join t3 on t2.c1=t3.c1;
C1 C2 ROWNUM
---------- ---------- ----------
1 1 1
2 2 2
3 3 3
SQL> select * from ( select t1.* , rownum row_id from t1 where rownum <= 5 ) where row_id > 2;
C1 C2 ROW_ID
---------- ---------- ----------
3 3 3
SQL> select t1.c1, (select t2.c2 from t2 where t1.c1=t2.c2 and rownum < t1.c1) from t1;
In case of group by queries, the rownum will work on the originally found rows, not the result rows like LIMIT
SELECTsum(c1) FROM t1 WHERE rownum<=2;
This will return 3 (or at least not 6)
ROWNUM cannot be used with HAVING (Oracle has the same restriction)
ROWNUM should not be used in applications with the following constructs as the result will not be deterministic (it's legal to use it, but the rows used in the result set will differ based on engine or used optimizations):
In ON expressions
With GROUP BY
SELECT without an ORDER BY (This is a common case how ROWNUM is used and in this case we can replace it with LIMIT)
Architecture:
Add function ROWNUM() (Item_rownum()).
Map the result of ROWNUM() to 'last_join->accepted_records', which is incremented for each row added to the result set.
This variable will be incremented in end_send(), end_send_group(), end_update() and end_unique_update()
Filesort() needs each own variable that is incremented for each accepted row.
In oracle mode, if field with name 'rownum' is used and there is no match in
any of the tables, map the field to Item_rownum()
Things to consider and test in addition to the test in the suggested patch:
Limit optimization
If we have ROWNUM < # or ROWNUM <= # on the top level of the WHERE clause
this should be converted to LIMIT. This is to be done in the
JOIN::prepare phase(), probably after optimize_cond() which does
propagate_cond_constants()
When using sum functions, it's not sent rows, it's examined rows we have to
limit against
Filesort, when done over single table needs it's own handling
(as there are no 'send_records' here). Limit is done according to found rows,
not sorted order.
Ensure that ROWNUM also works with UPDATE and DELETE
MDEV-20020sql_mode="oracle" does not support "rownum" pseudo column
Open
causes
MDEV-26329Equality operator does not work with ROWNUM() function after first row
Closed
MDEV-29129Performance regression starting in 10.6: unlimited "select order by limit" always using temporary, taking between 60x and 2000x longer in 10.6, 10.7, and 10.8 than in 10.5