Re: LIMIT clause extremely slow

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Melzer Kassensysteme <office(at)melzer-kassen(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: LIMIT clause extremely slow
Date: 2023-11-15 18:08:51
Message-ID: 235e74c6189774e822d51f3504adbd223de9beb3.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 2023-11-15 at 17:46 +0100, Melzer Kassensysteme wrote:
> SELECT * FROM table WHERE (index1 > 1 OR (index1 = 1 AND index2 > 5)) ORDER BY index1, index2 LIMIT 1
>  
> index1 and index2 are index fields of datatype integer.
>  
> This takes some 100 times longer than in Mysql or other databases

This is not a bug; at worst, it is a performance problem.

For good performance, rewrite the query to

SELECT * FROM "table"
WHERE (index1, index2) > (1, 5)
ORDER BY index1, index2
LIMIT 1;

and make sure you have an index on (index1, index2).

Yours,
Laurenz Albe

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Halil Han Badem 2023-11-15 18:11:32 Re: BUG #18196: Databases Created in Turkish Language Will Not Run on the Latest Version of Windows
Previous Message Laurenz Albe 2023-11-15 17:48:47 Re: BUG #18196: Databases Created in Turkish Language Will Not Run on the Latest Version of Windows