Skip to Main Content
  • Questions
  • Select then stop when result is found

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Todd.

Asked: March 06, 2007 - 4:10 pm UTC

Last updated: March 06, 2007 - 5:09 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Alright, after searching up and down on google, asking fellow workers and looking on here. I can't quite find an answer to a problem I'm having. I have a basic statetment in oracle that is doing a select from three columns of a database using the user params as 'the search words'. I'm trying to figure out how to get the query to 'stop' once it finds 1 record. (As thats all it needs to do, is match one record and stop - Dont keep looking in the database.)

Select a, b , c from database where a = param1 and b = param2 and c = param3


The reason I'm trying to limit the results to one record even is because the query seems kinda of slow. Here is an example of what I have tried to do when it comes to 'limiting'

Select a, b , c, rownum from database where a = param1 and b = param2 and c = param3 and rownum < 2 


I'm not so sure this "stops" oracle from processing the statement? So my basic question is How can i get oracle to stop "searching" once its found ONE record?

Thank you for the assistance you can provide.

and Tom said...

that stops it, you got it.


Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, March 06, 2007 - 5:04 pm UTC

Can use "rownum <2" technique in the place of "select distinct ... where .." to avoid displaying duplicate output.

Is it safe to use "rownum <2" here.
Tom Kyte
March 06, 2007 - 5:09 pm UTC

if you do rownum < 2 you will get one row

it is not possible to have 2 rows that are the some if you can only get one row at most.

can you use rownum < 2 to avoid "duplicates" - no, not in general (because select distinct can return a lot of rows! and rownum < 2 will return at most 1)

thanks

A reader, March 06, 2007 - 5:13 pm UTC