Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TR6ff-0005M1-BZ for pgsql-performance@postgresql.org; Wed, 24 Oct 2012 19:31:19 +0000 Received: from smtp2.peak6.com ([38.98.137.9]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TR6fZ-0004re-Ic for pgsql-performance@postgresql.org; Wed, 24 Oct 2012 19:31:17 +0000 X-AuditID: 0a02cb20-b7f726d000005255-29-5088420094c4 Received: from sswchi5pcas2.peak6.net (sswchi5pcas2.peak6.net [10.100.13.24]) by smtp2.peak6.com (Symantec Messaging Gateway) with SMTP id BF.3A.21077.00248805; Wed, 24 Oct 2012 14:31:12 -0500 (CDT) Received: from [10.10.201.64] (10.10.201.64) by sswchi5pcas2.peak6.net (10.100.13.25) with Microsoft SMTP Server (TLS) id 14.2.247.3; Wed, 24 Oct 2012 14:31:11 -0500 Message-ID: <508841FF.9040303@optionshouse.com> Date: Wed, 24 Oct 2012 14:31:11 -0500 From: Shaun Thomas Reply-To: Organization: Peak6 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Tom Lane CC: "pgsql-performance@postgresql.org" Subject: Re: Setting Statistics on Functional Indexes References: <50881D73.100@optionshouse.com> <6668.1351105908@sss.pgh.pa.us> In-Reply-To: <6668.1351105908@sss.pgh.pa.us> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.201.64] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprAKsWRmVeSWpSXmKPExsXClcIrocvg1BFgcGo2n8X1meeYLU6tusvk wORxYd80do9FzzayBDBFNTDaJObl5ZcklqQqpKQWJ9sq+ReUZObnFXvklxanKrhkFifnJGbm phYpKWSm2CoZKykU5CQmp+am5pXYKiUWFKTmpSjZcSlgABugssw8hdS85PyUzLx0WyXPYH9d CwtTS11DJbt4ZJDQyZIxubeBsWC7REXD3CNMDYybhbsYOTkkBEwklu4/wgRhi0lcuLeerYuR i0NI4ASjxMHpb6GclYwSG29vYAGp4hXQl2g6uQKsg0VAVWLeyc1gNpuArsSk/hVsILaQgIpE 39kbYHF+AWGJfR+amEFsUYEwieU7Iep5BQQlTs58AjZTREBBou/BO3YQm1nAU6Lj1CpGEFtY wF7i8+wNjBAzvSV2nloCNp9TQEfi855nrBD1thIX5lxngbDlJba/ncMMUa8qsaJ5IhvEZwoS /6+8Y5rAKDILyepZSNpnIWlfwMi8ilG8uCwxOSPTrKA4t6TASK8gNTHbTC8vtWQTIyjamU4r 7GDc3GN1iFGAg1GJh1fgbFuAEGtiWXFl7iFGCQ5mJRHeyQ/aA4R4UxIrq1KL8uOLSnNSiw8x +gDDbCKzlGhyPjAR5ZXEGxqbGphamhqampsaGuEQVhLnvXQbaLxAOjD9ZKemFqQWwYxj4uCU amCcGxIVbcUS8XnJg7MLLsXoHfi2bsnOO8Vz1G/b1rz8Mzkg5/Ta7xdMN3sp7p3LPV/y1LP8 HCvnirPuxUqxP+Zem1Mv9PPf5bq59vtPd0g9vm74uqhJ536Lh+ybOzYHBFXnX1pT92a3zt8b u670zvz2s76uk99vLdeFIP1/mXn3n1Vuy085K6U6T4mlOCPRUIu5qDgRAPkWcdIjAwAA X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201210/299 X-Sequence-Number: 48258 On 10/24/2012 02:11 PM, Tom Lane wrote: > It's not particularly (not that you've even defined what you think > "optimistic" is, much less mentioned what baseline you're comparing > to). The main flaw with my example is that it's random. But I swear I'm not making it up! :) There seems to be a particularly nasty edge case we're triggering, then. Like I said, it's worse when col1+col2 don't match anything. In that case, it's using the trunc index on the date column, which has demonstrably worse performance. Here are the two analyzes I got before/after front-loading statistics. Before stats increase: Sort (cost=9.38..9.39 rows=1 width=23) (actual time=78.282..78.282 rows=0 loops=1) Sort Key: id, action_date Sort Method: quicksort Memory: 25kB -> Index Scan using idx_date_test_action_date_trunc on date_test (cost=0.00..9.37 rows=1 width=23) (actual time=78.274..78.274 rows=0 loops=1) Index Cond: (date_trunc('day'::text, action_date) >= '2012-10-24 00:00:00'::timestamp without time zone) Filter: (((col1)::text = 'S:96'::text) AND (col2 = 657::numeric)) Total runtime: 78.317 ms And then after. I used your unofficial trick to set it to 1000: alter index idx_date_test_action_date_trunc alter column date_trunc set statistics 1000; analyze date_test; Sort (cost=9.83..9.83 rows=1 width=23) (actual time=0.038..0.038 rows=0 loops=1) Sort Key: id, action_date Sort Method: quicksort Memory: 25kB -> Index Scan using idx_date_test_col1_col2 on date_test (cost=0.00..9.82 rows=1 width=23) (actual time=0.030..0.030 rows=0 loops=1) Index Cond: (((col1)::text = 'S:96'::text) AND (col2 = 657::numeric)) Filter: (date_trunc('day'::text, action_date) >= '2012-10-24 00:00:00'::timestamp without time zone) Total runtime: 0.066 ms This is on a bone-stock PG 9.1.6 from Ubuntu 12.04 LTS, with default_statistics increased to 500. The only thing I bumped up was the functional index between those two query plans. But then I noticed something else. I reverted back to the old 500 default for everything, and added an index: create index idx_date_test_action_date_trunc_col1 on date_test (date_trunc('day', action_date), col1); I think we can agree that this index would be more selective than the one on date_trunc by itself. Yet: Sort (cost=9.38..9.39 rows=1 width=23) (actual time=77.055..77.055 rows=0 loops=1) Sort Key: id, action_date Sort Method: quicksort Memory: 25kB -> Index Scan using idx_date_test_action_date_trunc on date_test (cost=0.00..9.37 rows=1 width=23) (actual time=77.046..77.046 rows=0 loops=1) Index Cond: (date_trunc('day'::text, action_date) >= '2012-10-24 00:00:00'::timestamp without time zone) Filter: (((col1)::text = 'S:96'::text) AND (col2 = 657::numeric)) Total runtime: 77.091 ms All I have to say about that is: wat. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604 312-444-8534 sthomas@optionshouse.com ______________________________________________ See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email