Queries containing ORDER BY and LIMIT started to work slowly

From: Rondat Flyag <rondatflyag(at)yandex(dot)ru>
To: "pgsql-performance(at)lists(dot)postgresql(dot)org" <pgsql-performance(at)lists(dot)postgresql(dot)org>
Subject: Queries containing ORDER BY and LIMIT started to work slowly
Date: 2023-08-29 17:47:19
Message-ID: 32431693330715@mail.yandex.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


I have a legacy system that uses `Posgresql 9.6` and `Ubuntu 16.04`. Everything was fine several days ago even with standard Postgresql settings. I dumped a database with the compression option (maximum compression level -Z 9) in order to have a smaller size (`pg_dump --compress=9 database_name > database_name.sql`). After that I got a lot of problems. Some queries for certain tables started to be executed very slow. Queries for other tables work fine.

 

Here are the tables that I have issues with.

 

 

    asins:

    id (integer)

    value (string), index b-tree

    type (string)

 

  

 

    books:

    id (integer)

    asin (string), index b-tree

    ...

    (total 32 columns)

 

 

 

    asins_statistics:

    id (integer)

    average_price (float)

    average_rating (integer)

    asin_id (foreign key)

    ...

    (total 17 columns)

 

These tables contain 1 400 000 rows each. Detailed info in attachments.

 

Basically I used the following query and it worked well:

 

 

    (1) SELECT * FROM ISBNS JOIN BOOKS ON BOOKS.ISBN = ISBN.VALUE JOIN ISBNS_STATISTICS ON ISBNS_STATISTICS.ISBN_ID = ISBNS.ID ORDER BY ISBNS.VALUE LIMIT 100;

 

 

But after I made the dump it started to be executed extremely slow. I'm not sure whether it's because of the dump, but before the dump everything worked well. This query also works well:

 

 

    SELECT * FROM ISBNS JOIN BOOKS ON BOOKS.ISBN = ISBN.VALUE JOIN ISBNS_STATISTICS ON ISBNS_STATISTICS.ISBN_ID = ISBNS.ID LIMIT 100;

 

This query is executed quickly too:

 

    SELECT * FROM ISBNS JOIN BOOKS ON BOOKS.ISBN = ISBN.VALUE ORDER BY ISBNS.VALUE LIMIT 100;

 

 

I changed performance settings (for instance, increased `shared_buffer`), but it didn't increase speed too much.

 

I've read that queries containing LIMIT and ORDER BY work very slow, but if I make such queries to other tables it works fine.

 

The query plan for query (1) is in attachment.

 

So, the questions are:

1. Why everything worked well and started to work slowly?

2. Why similar queries to other tables are still executed quickly?

 

Thank you in advance.

 

Cheers,

Serg

 

Attachment Content-Type Size
unknown_filename text/html 2.7 KB
asins_statistics_schema.txt text/plain 1.6 KB
books_schema.txt text/plain 3.4 KB
asins_schema.txt text/plain 1.5 KB
query_plan.txt text/plain 2.7 KB

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Jeff Janes 2023-08-29 18:42:27 Re: Queries containing ORDER BY and LIMIT started to work slowly
Previous Message jayaprabhakar k 2023-08-29 16:47:18 Re: Index bloat and REINDEX/VACUUM optimization for partial index