select Nth row from table

Posted on 2002-05-10
Last Modified: 2008-03-06
I have a table with a billion rows
id (primary key, identity), email, firstname, etc...

I want to grab a specific row number, but the id's have gaps (from deleting rows)

my first attempt:
select top 1 * from mytable e where (select count(id) from mytable where id <= = 412

that would return the 412th row regardless of gaps in id.. the problem with that query is its sooo slow!

can anyone help me with a better solution?

maybe a better idea would be to re-index the table whenever a row is deleted but I could not figure out how to do that either (without making a temporary table) BTW: no temp tables.. the table is toooo BIG to be duplicated
Question by:gfody
  • 2
LVL 69

Accepted Solution

Scott Pletcher earned 200 total points
ID: 7002151
If you're going after a relatively low-numbered row, such as 412, this should work OK:

FROM myTable
ORDER BY id) AS topOfMyTable
LVL 69

Expert Comment

by:Scott Pletcher
ID: 7002159
Naturally you will need an index on id (as the first/only column in the index) for this to run efficiently.  You should probably remove it from the other index unless it's needed there to specify order.

Also, since the TOP clause will not accept a variable, with this method you would have to use sp_executesql or EXEC('SELECT ...') to make the row number dynamic.

Author Comment

ID: 7002321
thank you, this is much faster

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now