Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:47:32+00:00 2026-05-23T15:47:32+00:00

I have a table I’m doing an ORDER BY on before a LIMIT and

  • 0

I have a table I’m doing an ORDER BY on before a LIMIT and OFFSET in order to paginate.

Adding an index on the ORDER BY column makes a massive difference to performance (when used in combination with a small LIMIT). On a 500,000 row table, I saw a 10,000x improvement adding the index, as long as there was a small LIMIT.

However, the index has no impact for high OFFSETs (i.e. later pages in my pagination). This is understandable: a b-tree index makes it easy to iterate in order from the beginning but not to find the nth item.

It seems that what would help is a counted b-tree index, but I’m not aware of support for these in PostgreSQL. Is there another solution? It seems that optimizing for large OFFSETs (especially in pagination use-cases) isn’t that unusual.

Unfortunately, the PostgreSQL manual simply says “The rows skipped by an OFFSET clause still have to be computed inside the server; therefore a large OFFSET might be inefficient.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:47:33+00:00Added an answer on May 23, 2026 at 3:47 pm

    You might want a computed index.

    Let’s create a table:

    create table sales(day date, amount real);
    

    And fill it with some random stuff:

    insert into sales 
        select current_date + s.a as day, random()*100 as amount
        from generate_series(1,20);
    

    Index it by day, nothing special here:

    create index sales_by_day on sales(day);
    

    Create a row position function. There are other approaches, this one is the simplest:

    create or replace function sales_pos (date) returns bigint 
       as 'select count(day) from sales where day <= $1;' 
       language sql immutable;
    

    Check if it works (don’t call it like this on large datasets though):

    select sales_pos(day), day, amount from sales;
    
         sales_pos |    day     |  amount  
        -----------+------------+----------
                 1 | 2011-07-08 |  41.6135
                 2 | 2011-07-09 |  19.0663
                 3 | 2011-07-10 |  12.3715
        ..................
    

    Now the tricky part: add another index computed on the sales_pos function values:

    create index sales_by_pos on sales using btree(sales_pos(day));
    

    Here is how you use it. 5 is your “offset”, 10 is the “limit”:

    select * from sales where sales_pos(day) >= 5 and sales_pos(day) < 5+10;
    
            day     | amount  
        ------------+---------
         2011-07-12 | 94.3042
         2011-07-13 | 12.9532
         2011-07-14 | 74.7261
        ...............
    

    It is fast, because when you call it like this, Postgres uses precalculated values from the index:

    explain select * from sales 
      where sales_pos(day) >= 5 and sales_pos(day) < 5+10;
    
                                        QUERY PLAN                                
        --------------------------------------------------------------------------
         Index Scan using sales_by_pos on sales  (cost=0.50..8.77 rows=1 width=8)
           Index Cond: ((sales_pos(day) >= 5) AND (sales_pos(day) < 15))
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in sqlalchemy 0.4 that with types.DateTime column: Column(dfield, types.DateTime, index=True) I
I have table defined with FlexiGrid. Call to all variables is ok. One column
i have table contain two column id and word . word column may contain
I have table named A with column B defined int not null Primary Key
I have table named lantable in sqlite database where there is a column named
I have table Widgets and table Persons both of them contains Identity column (
I have table with data 1/1 to 1/20 in one column. I want the
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table rows of data in html being filled from a CGI application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.