Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A28F56325D4 for ; Fri, 12 Feb 2010 12:07:29 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 80693-02 for ; Fri, 12 Feb 2010 16:07:19 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from FS.denninger.net (wsip-70-169-168-7.pn.at.cox.net [70.169.168.7]) by mail.postgresql.org (Postfix) with ESMTP id B397C632563 for ; Fri, 12 Feb 2010 12:07:18 -0400 (AST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by FS.denninger.net (8.14.3/8.13.1) with SMTP id o1CG7HvR002095 for ; Fri, 12 Feb 2010 10:07:18 -0600 (CST) (envelope-from karl@denninger.net) Received: from [127.0.0.1] [192.168.1.40] by Spamblock-sys (LOCAL); Fri Feb 12 10:07:18 2010 Message-ID: <4B757C59.8070500@denninger.net> Date: Fri, 12 Feb 2010 10:05:45 -0600 From: Karl Denninger User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: Kevin Grittner CC: Bryce Nesbitt , pgsql-performance@postgresql.org Subject: Re: 512,600ms query becomes 7500ms... but why? Postgres 8.3 query planner quirk? References: <4B723042.7090406@obviously.com> <4B751AD5020000250002F248@gw.wicourts.gov> In-Reply-To: <4B751AD5020000250002F248@gw.wicourts.gov> X-Enigmail-Version: 0.96.0 Content-Type: multipart/mixed; boundary="------------060904090906080804070607" X-Antivirus: avast! (VPS 100212-0, 02/12/2010), Outbound message X-Antivirus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.639 tagged_above=-10 required=5 tests=BAYES_20=-0.74, HTML_MESSAGE=0.001, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 201002/205 X-Sequence-Number: 37626 This is a multi-part message in MIME format. --------------060904090906080804070607 Content-Type: multipart/alternative; boundary="------------010400070708060905030102" --------------010400070708060905030102 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit "Exists" can be quite slow. So can "not exists" See if you can re-write it using a sub-select - just replace the "exists ...." with "(select ...) is not null" Surprisingly this often results in a MUCH better query plan under Postgresql. Why the planner evaluates it "better" eludes me (it shouldn't) but the differences are often STRIKING - I've seen factor-of-10 differences in execution performance. Kevin Grittner wrote: > Bryce Nesbitt wrote: > > >> I've got a very slow query, which I can make faster by doing >> something seemingly trivial. >> > > Out of curiosity, what kind of performance do you get with?: > > EXPLAIN ANALYZE > SELECT contexts.context_key > FROM contexts > JOIN articles ON (articles.context_key = contexts.context_key) > JOIN matview_82034 ON (matview_82034.context_key = > contexts.context_key) > WHERE EXISTS > ( > SELECT * > FROM article_words > JOIN words using (word_key) > WHERE context_key = contexts.context_key > AND word = 'insider' > ) > AND EXISTS > ( > SELECT * > FROM article_words > JOIN words using (word_key) > WHERE context_key = contexts.context_key > AND word = 'trading' > ) > AND EXISTS > ( > SELECT * > FROM virtual_ancestors a > JOIN bp_categories ON (bp_categories.context_key = > a.ancestor_key) > WHERE a.context_key = contexts.context_key > AND lower(bp_categories.category) = 'law' > ) > AND articles.indexed > ; > > (You may have to add some table aliases in the subqueries.) > > If you are able to make a copy on 8.4 and test the various forms, > that would also be interesting. I suspect that the above might do > pretty well in 8.4. > > -Kevin > > --------------010400070708060905030102 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit "Exists" can be quite slow.  So can "not exists"

See if you can re-write it using a sub-select - just replace the "exists ...." with "(select ...) is not null"

Surprisingly this often results in a MUCH better query plan under Postgresql.  Why the planner evaluates it "better" eludes me (it shouldn't) but the differences are often STRIKING - I've seen factor-of-10 differences in execution performance.


Kevin Grittner wrote:
Bryce Nesbitt <bryce2@obviously.com> wrote:
 
  
I've got a very slow query, which I can make faster by doing
something seemingly trivial. 
    
 
Out of curiosity, what kind of performance do you get with?:
 
EXPLAIN ANALYZE
SELECT contexts.context_key
  FROM contexts
  JOIN articles ON (articles.context_key = contexts.context_key)
  JOIN matview_82034 ON (matview_82034.context_key =
                         contexts.context_key)
  WHERE EXISTS
        (
          SELECT *
            FROM article_words
            JOIN words using (word_key)
            WHERE context_key = contexts.context_key
              AND word = 'insider'
        )
    AND EXISTS
        (
          SELECT *
            FROM article_words
            JOIN words using (word_key)
            WHERE context_key = contexts.context_key
              AND word = 'trading'
        )
    AND EXISTS
        (
          SELECT *
            FROM virtual_ancestors a
            JOIN bp_categories ON (bp_categories.context_key =
                                   a.ancestor_key)
            WHERE a.context_key = contexts.context_key
              AND lower(bp_categories.category) = 'law'
        )
    AND articles.indexed
;
 
(You may have to add some table aliases in the subqueries.)
 
If you are able to make a copy on 8.4 and test the various forms,
that would also be interesting.  I suspect that the above might do
pretty well in 8.4.
 
-Kevin

  
--------------010400070708060905030102-- --------------060904090906080804070607 Content-Type: text/x-vcard; charset=utf-8; name="karl.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="karl.vcf" begin:vcard fn:Karl Denninger n:Denninger;Karl email;internet:karl@denninger.net x-mozilla-html:TRUE version:2.1 end:vcard --------------060904090906080804070607--