Re: Apply LIMIT when computation is logically irrelevant

From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Robins Tharakan <tharakan(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Apply LIMIT when computation is logically irrelevant
Date: 2020-07-06 12:43:35
Message-ID: CANP8+jL+37nWsPUn1v-FZc2htPhRVncG4W=Q19i2YVWP_jkz1Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 6 Jul 2020 at 12:37, Robins Tharakan <tharakan(at)gmail(dot)com> wrote:

> When an SQL needs to UNION constants on either side, it should be possible
> to
> implicitly apply a LIMIT 1 and get good speed up. Is this an incorrect
> understanding,
> or something already discussed but rejected for some reason?
>
> This need came up while reviewing generated SQL, where the need was to
> return true when
> at least one of two lists had a row. A simplified version is given below:
>
> (SELECT 1 FROM pg_class) UNION (SELECT 1 FROM pg_class);
> vs.
> (select 1 FROM pg_class limit 1) UNION (SELECT 1 FROM pg_class limit 1);
> -- Faster
>

Those two queries aren't logically equivalent, so you can't apply the LIMIT
1 as an optimization.

First query returns lots of random rows, the second query returns just one
random row.

--
Simon Riggs http://www.2ndQuadrant.com/
<http://www.2ndquadrant.com/>
Mission Critical Databases

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2020-07-06 13:33:16 Re: SV: Using Postgres jdbc driver with Oracle SQL Developer
Previous Message Matthias Apitz 2020-07-06 12:27:23 Re: PostgreSQL server does not increment a SERIAL internally