Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: query "select * from table_name where rownum = 1" very slow

Re: query "select * from table_name where rownum = 1" very slow

From: <sybrandb_at_yahoo.com>
Date: 13 Jul 2005 02:17:20 -0700
Message-ID: <1121246240.308450.277260@z14g2000cwz.googlegroups.com>


No version, no specifics about optimizer being used. Why do so many people assume everyone knows which version they are using, and/or expect we can look over their shoulder.

Rownum per se doesn't result in a full table scan. Rownum just limits the resultset to the first record. (The optimizer will perform a count stopkey operation on the resultset)
However in the statement above, if you leave the rownum=1 condition out, what do you get? Exactly: a full table scan. So, if you would have run dbms_xplan on the the statement, you would have typically seen count(stopkey)

    table_name (full table scan)
So yes, evidently, if you run this statement against a table with a large number of records that will take some time, and you will thrash the performance of the database.
But evidently: you ASK it to perform a full table scan, so that is what you get.

The rule of leading subset doesn't apply anymore to Oracle 9i and higher.
If you are using the CBO, you will get index skip scans.

I wouldn't trust on hearsay, but adhere to Tom Kyte's mantra: Test, test and test
(and of course use auto_trace and run explain plan)

-- 
Sybrand Bakker
Senior Oracle DBA
Received on Wed Jul 13 2005 - 04:17:20 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US