X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 34C73D819D for ; Wed, 7 Sep 2005 15:41:09 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15322-02 for ; Wed, 7 Sep 2005 18:41:02 +0000 (GMT) Received: from wumpus.mythic-beasts.com (wumpus.mythic-beasts.com [212.69.37.9]) by svr1.postgresql.org (Postfix) with ESMTP id 40CA7D8187 for ; Wed, 7 Sep 2005 15:40:58 -0300 (ADT) Received: from sphinx.mythic-beasts.com ([212.69.37.6]) by wumpus.mythic-beasts.com with esmtp (Exim 4.44) id 1ED4qv-000302-Et for pgsql-performance@postgresql.org; Wed, 07 Sep 2005 19:40:57 +0100 Received: from xelah (helo=localhost) by sphinx.mythic-beasts.com with local-esmtp (Exim 4.51) id 1ED4qv-0007Ix-AK for pgsql-performance@postgresql.org; Wed, 07 Sep 2005 19:40:57 +0100 Date: Wed, 7 Sep 2005 19:40:57 +0100 (BST) From: Alex Hayward X-X-Sender: xelah@sphinx.mythic-beasts.com To: pgsql-performance@postgresql.org Subject: Re: Query take 101 minutes, help, please In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.207 required=5 tests=[UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200509/112 X-Sequence-Number: 14420 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.