Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 047B8B5DDFF for ; Thu, 2 Jun 2011 12:34:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 03827-02 for ; Thu, 2 Jun 2011 15:34:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.peak6.com (smtp.peak6.com [38.98.137.6]) by mail.postgresql.org (Postfix) with ESMTP id C7F06B60228 for ; Thu, 2 Jun 2011 12:34:08 -0300 (ADT) X-AuditID: 0a64cb1e-b7bf7ae000000b0d-93-4de7ad6ec6ab Message-ID: <4DE7AD6E.3020604@peak6.com> Date: Thu, 2 Jun 2011 10:34:06 -0500 From: Shaun Thomas Reply-To: Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.7) Gecko/20100720 Lightning/1.0b2 Thunderbird/3.1.1 MIME-Version: 1.0 To: Kevin Grittner CC: CS DBA , Subject: Re: Problem query References: <4DE69D92.5040407@consistentstate.com> <4DE66AF8020000250003DFB6@gw.wicourts.gov> <4DE6BD11.6060603@consistentstate.com> <4DE74E06020000250003DFE6@gw.wicourts.gov> In-Reply-To: <4DE74E06020000250003DFE6@gw.wicourts.gov> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.221.50] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrLIsWRmVeSWpSXmKPExsXCxcQrppu/9rmvwevNLBaLFu1htnh0fjmL xfWZ55gdmD3+3HjJ7nFh3zR2j1cTNrMEMEc1MNok5uXllySWpCqkpBYn2yoFuDp6mykElyTm pSQWpSi4ZBYn5yRm5qYWKSlkptgqGSkpFOQkJqfmpuaV2ColFhSk5qUo2XHZAAUz8xRS85Lz UzLz0m2VPIP9dS0sTC11DZXs4vGBhMvMGYdOd7MVPOWqWPxmJ0sD4xaOLkZODgkBE4mu3a/Y IGwxiQv31oPZvAIaEhsm7mfuYuTgYBFQkdjakwoSZhNQk1i4+yYjiC0kICuxf9U5VhCbX0BY Yt+HJmYQW1SgQKJp63ZmiDGCEidnPmEBsUUEDCRurFgBVs8s4C3RdPcoWFwYaPz+3+vZIWau Z5RYsdwDxOYUMJfomdnMDlFvK3FhznUWCFteYvvbOcwQ9XISdxqes4GcKSGgIHHvr9MERqFZ SDbPQtI9C0n3AkbmVYzixWWJyRmZpgXFuSUFhnoFqYnZZnp5qSWbGEGRnHJabgfj9KWKhxgF OBiVeHgLlj73FWJNLCuuzD3EKMHBrCTCu6oSKMSbklhZlVqUH19UmpNafIhRGxhiE5mluJPz gUkmryTe2MBASZzXxmaqr5BAOjAJZKemFqQWwVQzcXBKNTDmHurcsv9jkMQ7c+E3vUIpJ04W +DHdkk+bfihJv2xJKJ/8Mf6FtnPWb9BOWa9ePC/CT1ThNM+G3gcbd90vS3O5cOb1ydMKF/cZ Zi5sbDtYUy+w7t/5/88VrvJ1/d78l3eDut0hzvLq5xVsawONX3XFrOmQs/960vJjdAFjwuMf ep5OCy9yhM5TYinOSDTUYi4qTgQAT4upYfACAAA= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/21 X-Sequence-Number: 43954 On 06/02/2011 08:47 AM, Kevin Grittner wrote: > Is there some reason to believe that a sequential scan isn't the > fastest way to get the data? When generating summary data like > this, it often is faster than lots of random access. If you can > coerce it into a faster plan by turning off enable_seqscan on the > connection before running the query, then we can look at how you > might adjust your costing parameters to get better plans. This is right. There's really no way for the optimizer to get the values you want, even though your columns are indexed. But your query is a tad naive, unless you wrote up a special case for us. You're counting the number of maximum values in your table for tds_cx_ind and cxs_ind_2, but there will always be at least one for every combination. What you really want is this: SELECT count(1) FROM ( SELECT DISTINCT tds_cx_ind, cxs_ind_2 FROM max_xtrv_st_t ); If you really must have that inner query because it's generated and you won't know what it contains, you'd be better off with a CTE: WITH x AS ( SELECT max(pri_num) FROM max_xtrv_st_t GROUP BY tds_cx_ind, cxs_ind_2 ) SELECT count(1) FROM x; You'll still get a sequence scan from these, however. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email