Re: Query take 101 minutes, help, please

From: Alex Hayward <xelah-pgsql(at)xelah(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Query take 101 minutes, help, please
Date: 2005-09-07 18:40:57
Message-ID: Pine.LNX.4.58.0509071935300.27397@sphinx.mythic-beasts.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Wed, 7 Sep 2005, Meetesh Karia wrote:

> PG is creating the union of January, February and March tables first and
> that doesn't have an index on it. If you're going to do many queries using
> the union of those three tables, you might want to place their contents into
> one table and create an index on it.
>
> Otherwise, try something like this:
>
> SELECT "Rut Cliente"
> FROM "Internet_Abril"
> WHERE "Rut Cliente" NOT IN (SELECT "Rut Cliente" FROM
> "Internet_Enero")
> AND "Rut Cliente" NOT IN (SELECT "Rut Cliente" FROM
> "Internet_Febrero")
> AND "Rut Cliente" NOT IN (SELECT "Rut Cliente" FROM
> "Internet_Marzo");

You may also wish to try:

SELECT "Rut Cliente"
FROM "Internet_Abril"
WHERE NOT EXISTS
(SELECT 1 FROM "Internet_Enero"
WHERE "Internet_Enero"."Rut Cliente"="Internet_Abril"."Rut Cliente")
AND NOT EXISTS
(SELECT 1 FROM "Internet_Febrero"
WHERE "Internet_Febrero"."Rut Cliente"="Internet_Abril"."Rut Cliente")
AND NOT EXISTS
(SELECT 1 FROM "Internet_Marzo"
WHERE "Internet_Marzo"."Rut Cliente"="Internet_Abril"."Rut Cliente")

which will probably scan the indexes on the January, February and March
indexes once for each row in the April table.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Jim C. Nasby 2005-09-07 21:38:34 Re: Poor performance of delete by primary key
Previous Message Tom Lane 2005-09-07 17:57:36 Re: Query take 101 minutes, help, please