title: iSeries query changes selected RRN of subquery result rows
body: <p>I'm trying to make an optimal SQL query for an iSeries database table that can contain millions of rows (perhaps up to 3 million per month). The only key I have for each row is its RRN (relative record number, which is the physical record number for the row).</p>
  
  <p>My goal is to join the table with another small table to give me a textual description of one of the numeric columns. However, the number of rows involved can exceed 2 million, which typically causes the query to fail due to an out-of-memory condition. So I want to rewrite the query to avoid joining a large subset with any other table. So the idea is to select a single page (up to 30 rows) within a given month, and <em>then</em> join that subset to the second table. </p>
  
  <p>However, I ran into a weird problem. I use the following query to retrieve the RRNs of the rows I want for the page:</p>
  
  <pre><code>    select t.RRN2    -- Gives correct RRNs
      from (
        select row_number() over() as SEQ,
          rrn(e2) as RRN2, e2.*
        from TABLE1 as e2
        where e2.UPDATED between '2013-05-01' and '2013-05-31'
        order by e2.UPDATED, e2.ACCOUNT
      ) as t
      where t.SEQ &gt; 270 and t.SEQ &lt;= 300    -- Paging
      order by t.UPDATED, t.ACCOUNT
  </code></pre>
  
  <p>This query works just fine, returning the correct RRNs for the rows I need. However, when I attempted to join the result of the subquery with another table, <em>the RRNs changed</em>. So I simplified the query to a subquery within a simple outer query, without any join:</p>
  
  <pre><code>select rrn(e) as RRN, e.*
    from TABLE1 as e
    where rrn(e) in (
      select t.RRN2    -- Gives correct RRNs
      from (
        select row_number() over() as SEQ,
          rrn(e2) as RRN2, e2.*
        from TABLE1 as e2
        where e2.UPDATED between '2013-05-01' and '2013-05-31'
        order by e2.UPDATED, e2.ACCOUNT
      ) as t
      where t.SEQ &gt; 270 and t.SEQ &lt;= 300    -- Paging
      order by t.UPDATED, t.ACCOUNT
    )
    order by e.UPDATED, e.ACCOUNT
  </code></pre>
  
  <p>The outer query simply grabs all of the columns of each row selected by the subquery, using the RRN as the row key. But this query <em>does not work</em> - it returns rows with completely different RRNs.</p>
  
  <p>I need the actual RRN, because it will be used to retrieve more detailed information from the table in a subsequent query.</p>
  
  <p>Any ideas about why the RRNs end up different?</p>
  
  <p><strong>Resolution</strong></p>
  
  <p>I decided to break the query into two calls, one to issue the simple subquery and return just the RRNs (rows-IDs), and the second to do the rest of the JOINs and so forth to retrieve the complete info for each row. (Since the table gets updated only once a day, and rows never get deleted, there are no potential timing problems to worry about.)</p>
  
  <p>This approach appears to work quite well.</p>
  
  <p><strong>Addendum</strong></p>
  
  <p>As to the question of why an out-of-memory error occurs, this appears to be a limitation on only <em>some</em> of our test servers. Some can only handle up to around 2m rows, while others can handle much more than that. So I'm guessing that this is some sort of limit imposed by the admins on a server-by-server basis.</p>