[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


select Nth row from table

Posted on 2002-05-10
Medium Priority
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 <= e.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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 70

Accepted Solution

Scott Pletcher earned 800 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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

656 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