Skip to content

Instantly share code, notes, and snippets.

@dburger
Created November 3, 2012 19:57
Show Gist options
  • Select an option

  • Save dburger/4008503 to your computer and use it in GitHub Desktop.

Select an option

Save dburger/4008503 to your computer and use it in GitHub Desktop.

Revisions

  1. dburger revised this gist Nov 3, 2012. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion gistfile1.sql
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    -- Say I have a very large table with a composite primary key with four parts,
    -- primary key = (A, B, C, D) where these are all integers. I would like to be
    -- able to select blocks of records of 10,000 in ORDER BY (A, B, C, D) primary
    -- key order. Given the last block where the final
    -- key order. I can get my first block with the query:

    SELECT * FROM BigTable
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- Given the last block where the final
    -- row had (A = a, B = b, C = c, D = d) a query for the next block is:

    SELECT * FROM BigTable
  2. dburger revised this gist Nov 3, 2012. 1 changed file with 1 addition and 12 deletions.
    13 changes: 1 addition & 12 deletions gistfile1.sql
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,4 @@ WHERE
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- This query does not seem to run very fast though, is there a better way?

    -- Ah, I guess this can be collapsed down to:

    SELECT * FROM BigTable
    WHERE
    A >= a AND B >= b AND C >= c AND D > d
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- but the query planner was probably smart enough to do that. I'll have
    -- to check if it is still not efficient.
    -- This query does not seem to run very fast though, is there a better way?
  3. dburger revised this gist Nov 3, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.sql
    Original file line number Diff line number Diff line change
    @@ -23,4 +23,5 @@ WHERE
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- but the query planner was probably smart enough to do that.
    -- but the query planner was probably smart enough to do that. I'll have
    -- to check if it is still not efficient.
  4. dburger revised this gist Nov 3, 2012. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion gistfile1.sql
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,14 @@ WHERE
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- This query does not seem to run very fast though, is there a better way?
    -- This query does not seem to run very fast though, is there a better way?

    -- Ah, I guess this can be collapsed down to:

    SELECT * FROM BigTable
    WHERE
    A >= a AND B >= b AND C >= c AND D > d
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- but the query planner was probably smart enough to do that.
  5. dburger created this gist Nov 3, 2012.
    16 changes: 16 additions & 0 deletions gistfile1.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    -- Say I have a very large table with a composite primary key with four parts,
    -- primary key = (A, B, C, D) where these are all integers. I would like to be
    -- able to select blocks of records of 10,000 in ORDER BY (A, B, C, D) primary
    -- key order. Given the last block where the final
    -- row had (A = a, B = b, C = c, D = d) a query for the next block is:

    SELECT * FROM BigTable
    WHERE
    A > a OR
    (A = a AND B > b) OR
    (A = a AND B = b AND C > c) OR
    (A = a AND B = b AND C = c AND D > d)
    ORDER BY (A, B, C, D)
    LIMIT 10000;

    -- This query does not seem to run very fast though, is there a better way?