rownum use [turn]

 

ORACLE in, ROWNUM as a hidden field. The record is the number of rows.
The SELECT  ROWNUM, A. *  The FROM  TABLE A came out   

Such as:

The SELECT  ROWNUM, A *.  The FROM   PersonTable A; // query all records

You can check the first few data, such as:
SELECT  *  from  ( the SELECT  . ROWNUM RN, A *  the FROM  TABLE A) WHERE b.rn B = '10 '// query the records indicate rownum = 10

Oracle has rownum to limit the number of columns in the query out!

rownum and limit what's the difference?

Under normal circumstances, when paging is a key need. MySQL is limit keyword, MSSQL is top keywords, Oracle is in the rownum. There rownum <5 can only detect five data, starting from 0.

如: SELECT ROWNUM,A.* FROM  PersonTable A  where ROWNUM<5;  

 

The concept of ROWNUM
ROWNUM is a false columns. Will be assigned to 1,2,3,4, ... N, N is the number of rows. ROWNUM value is not a permanently assigned to the line (this is the most misunderstood). Table in a row does not label; you can not query row ROWNUM value of 5 - did not have the concept. Another problem is easily confused when ROWNUM value is assigned. ROWNUM value is assigned a predicate query after analysis, carried out prior to any sorting and aggregation. ROWNUM value will only grow after being allocated. This is why the following query will never return a result:
the SELECT * from the Countries the WHERE rownum> 1;
ROWNUM> 1 for the first row is not a true value, ROWNUM no growth to 2. So, no. Never use larger than 1 ROWNUM 'ROWNUM>? 'And' ROWNUM = 2 ... N 'such conditions.

Sort result by the first N records rownum
FROM / WHERE clause are executed first.
The row FROM / WHERE clause output, ROWNUM is assigned to them and grow from.
The SELECT is applied.
The GROUP BY is applied.
The HAVING IS is application
ORDER bY is applied.
this is why the following SQL is almost always wrong:
the SELECT rownum, JOB_TITLE, min_salary
from Jobs the WHERE rownum <3 by min_salary the Order;
correct wording: 
        the SELECT rownum, tmp * from (.
        SELECT JOB_TITLE, min_salary 
        from Jobs Order by min_salary) tmp
        WHERE rownum <=. 3;

for paging with ROWNUM
SELECT * from
        (SELECT / * + the FIRST_ROWS (n-) * / A *,.
        ROWNUM RNUM
        from (your_query_goes_here, with Order by) A
        ROWNUM WHERE <=
        : MAX_ROW_TO_FETCH)
WHERE RNUM> =: MIN_ROW_TO_FETCH;
the FIRST_ROWS (N) so that the optimizer considers the minimum time to obtain top N records.
: MAX_ROW_TO_FETCH last line of a page in the result set. If you are 10 lines per page, to be displayed on page 6, then the value is taken 60.
: The first line MIN_ROW_TO_FETCH a page in the result set. If you are 10 lines per page, to be displayed on page 6, then the value is taken 50.

ROWNUM impact on performance
ROWNUM oracle avoid sorts on disk. rownum can not avoid full table scan, but it avoids the whole table data sorting operation, after specifying the rownum, sorting operation can be easily done in memory.

 

Guess you like

Origin www.cnblogs.com/sunny3158/p/11608094.html