How to get the first row in a selection of rowS
Posted in 2003
Topics: SQL Development & Query Writing
I have a select statement whch retrieves a number of rows. Given the underlying condition, I want the fastest way just to get the first of the selected rows. Andrew H. Detail =========================================================================================== I create a cursor 'userFileRead_nextOidCursor' based on the folloing SELECT select {+INDEX(user_file_read_table, oid_index)} oid, f2, f3, f4 from user_file_read_table where oid > x order by oid Then I get just the next ( i.e. the first ) row. EXEC SQL FETCH NEXT userFileRead_nextOidCursor INTO :the_oid, v2, v3, v4 Then I close the cursor. Can I do this faster ? May be it would be faster in one statement ? The best I can think of so far is two statements. select {+INDEX(user_file_read_table, oid_index)} MIN(oid) into required_oid from user_file_read_table where oid > x select {+INDEX(user_file_read_table, oid_index)} oid, f2, f3, f4 into the_oid, v2, v3, v4 from user_file_read_table where oid = required_oid Any suggestions. Speed is my main priority. But may be its as fast as it can be with the cursor. sending to informix-list
On Tue, 14 Oct 2003 17:00:40 +0100, "Andrew Hardy" <Andrew.Hardy@marconi.com> wrote: > >I have a select statement whch retrieves a number of rows. Given the >underlying condition, I want the fastest way just to get the first of the >selected rows. > >Andrew H. > > > > >Detail >=========================================================================================== > >I create a cursor 'userFileRead_nextOidCursor' based on the folloing SELECT > >select {+INDEX(user_file_read_table, oid_index)} oid, f2, f3, f4 from >user_file_read_table >where oid > x order by oid > >Then I get just the next ( i.e. the first ) row. > >EXEC SQL FETCH NEXT userFileRead_nextOidCursor INTO :the_oid, v2, v3, v4 > >Then I close the cursor. > >Can I do this faster ? May be it would be faster in one statement ? > >The best I can think of so far is two statements. > >select {+INDEX(user_file_read_table, oid_index)} MIN(oid) into required_oid >from user_file_read_table where oid > x >select {+INDEX(user_file_read_table, oid_index)} oid, f2, f3, f4 into >the_oid, v2, v3, v4 from user_file_read_table where oid = required_oid > >Any suggestions. Speed is my main priority. But may be its as fast as it >can be with the cursor. I don't see an Informix engine version. . . but is it possible to use the "select first" syntax???