Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B29819FBE1D for ; Thu, 15 Feb 2007 13:11:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 67370-04-4 for ; Thu, 15 Feb 2007 13:10:55 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from 25.mail-out.ovh.net (25.mail-out.ovh.net [213.186.37.103]) by postgresql.org (Postfix) with SMTP id 1C6B09FBC2F for ; Thu, 15 Feb 2007 13:10:55 -0400 (AST) Received: (qmail 30198 invoked by uid 503); 15 Feb 2007 15:36:32 -0000 Received: (QMFILT: 1.0); 15 Feb 2007 15:36:32 -0000 Received: from b6.ovh.net (HELO mail237.ha.ovh.net) (213.186.33.56) by 25.mail-out.ovh.net with SMTP; 15 Feb 2007 15:36:32 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 15 Feb 2007 17:10:45 -0000 Received: from mail.companeo.com (mail.companeo.com [82.228.133.159]) by ssl0.ovh.net (IMP) with HTTP for ; Thu, 15 Feb 2007 18:10:45 +0100 Message-ID: <1171559445.45d494158444c@ssl0.ovh.net> Date: Thu, 15 Feb 2007 18:10:45 +0100 From: philippe To: Richard Huxton Cc: pgsql-performance@postgresql.org Subject: Re: Slow query with 'or' clause References: <1171550213.45d470056249c@ssl0.ovh.net> <45D47FE1.5010209@archonet.com> In-Reply-To: <45D47FE1.5010209@archonet.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.6 X-Originating-IP: 82.228.133.159 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.158 tagged_above=0 required=5 tests=BAYES_50, HOT_NASTY X-Spam-Level: X-Archive-Number: 200702/175 X-Sequence-Number: 23229 Selon Richard Huxton : > philippe wrote: > > explain analyse > > select count(*) from client_contact > > left join client_company using(cli_id) > > where (cli_mail = 'xxx@xxx.xx') OR > > (lower(cli_nom) = 'xxxxxx' and zipcode = '10001'); > > > > QUERY PLAN > > > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > Aggregate (cost=37523.98..37523.99 rows=1 width=0) (actual > > time=3871.086..3871.087 rows=1 loops=1) > > -> Merge Left Join (cost=0.00..36719.10 rows=321952 width=0) (actual > > time=3871.058..3871.058 rows=0 loops=1) > > This is the root of the problem - it's expecting to match over 320000 > rows rather than 0. > > I'm guessing there's a lot of correlation between cli_mail and cli_nom > (you're expecting them to match the same clients) but the planner > doesn't know this. > > If this is a common query, you could try an index on zipcode - that > might cut down the other side. > > However, I have to ask why you're using a left-join? Do you really have > rows in client_contact without a matching cli_id in client_company? > You are right, I was focused on server perf and I should have analysed my query. Query time is ok now. thanks you !! > -- > Richard Huxton > Archonet Ltd > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend >