5

Say that I have some SELECT statement:

SELECT id, name FROM people
   ORDER BY name ASC;

I have a few million rows in the people table and the ORDER BY clause can be much more complex than what I have shown here (possibly operating on a dozen columns).

I retrieve only a small subset of the rows (say rows 1..11) in order to display them in the UI. Now, I would like to solve following problems:

  1. Find the number of a row with a given id.
  2. Display the 5 items before and the 5 items after a row with a given id.

Problem 2 is easy to solve once I have solved problem 1, as I can then use something like this if I know that the item I was looking for has row number 1000 in the sorted result set (this is the Firebird SQL dialect):

SELECT id, name FROM people
   ORDER BY name ASC
   ROWS 995 TO 1005;

I also know that I can find the rank of a row by counting all of the rows which come before the one I am looking for, but this can lead to very long WHERE clauses with tons of OR and AND in the condition. And I have to do this repeatedly. With my test data, this takes hundreds of milliseconds, even when using properly indexed columns, which is way too slow.

Is there some means of achieving this by using some SQL:2003 features (such as row_number supported in Firebird 3.0)? I am by no way an SQL guru and I need some pointers here. Could I create a cached view where the result would include a rank/dense rank/row index?

Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
  • too hard to answer without info about UI type. Web? Desktop? Command line? Voice UI ;-) – rstrelba May 22 '12 at 13:22
  • I am displaying a list of persons in a desktop application; since the collection is huge, I only fetch the rows for the persons that fit into the viewport of the list. The user has a scroll bar which allows her to move to any point in the list, and have it refresh its contents as if it were really filled with millions of rows. – Pierre Arnaud May 22 '12 at 16:25
  • how much additional parameters in viewport's filter? – rstrelba May 22 '12 at 16:48
  • I don't see why the UI is impacting the answer here. The problem stays the same however you represent/handle the information in the UI: get rows in sorter order and be able to find the rank of a row in the list based on its unique id. – Pierre Arnaud May 22 '12 at 19:28

1 Answers1

3

Firebird appears to support window functions (called analytic functions in Oracle). So you can do the following:

To find the "row" number of a a row with a given id:

select id, row_number() over (partition by NULL order by name, id)
from t
where id = <id>

This assumes the id's are unique.

To solve the second problem:

select t.*
from (select id, row_number() over (partition by NULL order by name, id) as rownum
      from t
     ) t join 
     (select id, row_number() over (partition by NULL order by name, id) as rownum
      from t
      where id = <id>
     ) tid
     on t.rownum between tid.rownum - 5 and tid.rownum + 5

I might suggest something else, though, if you can modify the table structure. Most databases offer the ability to add an auto-increment column when a row is inserted. If your records are never deleted, this can server as your counter, simplifying your queries.

Jagger
  • 10,350
  • 9
  • 51
  • 93
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • thank you for your suggestion. I was hoping something along the lines of what you suggest in your first solution. And yes, my `id` column contains unique values. – Pierre Arnaud May 22 '12 at 16:27
  • I don't understand what you mean by modifying the table structure; basically, `id` is already an auto-increment column. The trouble is, the order of the rows depends on the ordering in the `SELECT` statement. How could an additional column help me? Did I miss something? – Pierre Arnaud May 22 '12 at 16:29
  • If you don't delete rows from the table, you could just use "where id between - 5 and + 5". This would eliminate the row_number() calculation. In other words, the solution to your problem would just be a self join. – Gordon Linoff May 22 '12 at 17:26
  • Ah, no. I don't need to see between otherid-5 and otherid+5, since ids are not sequential when I sort the rows. – Pierre Arnaud May 22 '12 at 19:19
  • Window functions are part of Firebird 3.0; first alpha versions are scheduled for Q2 2012, stable versions will come later. For now, I'll have to way... – Pierre Arnaud May 24 '12 at 03:36