[freetds] dbnextrow() vs dbskiprow() (skipping rows in FreeTDS)

Joel Fouse joel at fouse.net
Sun Jul 27 20:15:33 EDT 2008


On Sun, 2008-07-27 at 15:57 -0700, Navdeep Shergill wrote:

> Here is a sample scenerio. Lets say I have a table with 1000 rows and I
> issue this query
> 
> select * from mytable;
> 
> If I wanted to read the '900th' row; then I would have to call dbnextrow()
> 900 times to get to that row. By calling dbnextrow; I have wasted all this
> time loading data value for the previous 900 rows; even though I did not
> need them at all. I am looking for some way to advance the cursor to the
> 900th row. (Without doing another db query).


At first glance, this seems like the sort of thing better addressed by a
more targeted query, like:

select * from mytable where id = 900

...or something similar.  Generally, if you're looking for a specific
row or group of rows, you're better off narrowing that down in the query
in the first place using an appropriate WHERE clause rather than
grabbing the whole thing and looping through until you get the row you
want.

Even if there's some kind of dependent logic, where the data you get in
the first row somehow determines what the next row you want is (which,
even then, would strongly suggest a database redesign), you would be
better off getting your determining info, closing that query, and
opening a new one asking for the specific row(s) you want (again, back
to the WHERE clause).

Does this help?  Or is there something specific about your scenario that
requires this kind of "give me everything except I don't really want it"
approach?

- Joel



More information about the FreeTDS mailing list