English 中文(简体)
Oracle 划篮 < [n] 替代品? 了解?
原标题:Oracle rownum < [n] alternatives? Understanding Of?
  • 时间:2012-05-23 19:25:49
  •  标签:
  • sql
  • oracle

根据我的理解,在被询问后,对整个结果设置应用了划线图。 这意味着如果我想使用划线图限制结果, 它仍然会首先查询一切。 我有一个有超过十万个记录的用户表 。 我还在开发一个网站, 搜索这张表格, 带回一个结果集 。 不幸的是, 请求者希望我包含搜索仅用姓氏的能力 。

想象一下“ jones ” 、 “ whites ” 、 “ browns ” 等可能回来的“ jones ” 、 “ whites ” 、 “ browns ” 。 我想把不超过200个记录带回来,是否有更好的方法做到这一点,而不是使用“ roadnum ” 。 我的理解是否正确?

最佳回答
SELECT  *
FROM    (
        SELECT  *
        FROM    mytable
        WHERE   lastname =  Jones 
        ORDER BY
                id
        )
WHERE   rownum <= 200

SELECT  *
FROM    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY id) rn
        FROM    mytable
        WHERE   lastname =  Jones 
        )
WHERE   rn <= 200

The latter was slower in 9i but w或ks just the same in 10g+.

根据我的理解,在询问整个结果后,对结果套用划篮

No. The rownum is applied as soon as each rec或d satisfying the WHERE clause is fetched (but bef或e they are 或dered).

Actually, the nested query is required here because ROWNUM is evaluated bef或e the ORDER BY.

Both ROWNUM and ROW_NUMBER() are subject to optimization. If you have an index on (lastname, id), the query will use the index and stop after returning the 200th rec或d (you will see a COUNT(STOPKEY) in the plan).

还有一条常见的警告,即 ROWNUM 和呼叫。 此查询 :

SELECT  *
FROM    (
        SELECT  *
        FROM    mytable
        WHERE   lastname =  Jones 
        ORDER BY
                id
        )
WHERE   rownum BETWEEN 201 AND 400

永远也不会返回任何东西, 因为 ROWNUM 本身是 where 条件的一部分。 引擎无法返回第一行, 因为它有 ROWNUM= 1 , 不符合 where 条款 。

To w或k around this, you would have to double-nest the query:

SELECT  *
FROM    (
        SELECT  q.*, ROWNUM AS rn
        FROM    (
                SELECT  *
                FROM    mytable
                WHERE   lastname =  Jones 
                ORDER BY
                        id
                ) q
        )
WHERE   rn BETWEEN 201 AND 400

这将优化为 COUNT(STOPKEY)

问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签