Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TR72a-000643-6q for pgsql-performance@postgresql.org; Wed, 24 Oct 2012 19:55:00 +0000 Received: from smtp2.peak6.com ([38.98.137.9]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TR72U-000599-Jl for pgsql-performance@postgresql.org; Wed, 24 Oct 2012 19:54:58 +0000 X-AuditID: 0a02cb20-b7f726d000005255-3c-5088478db066 Received: from sswchi5pcas2.peak6.net (sswchi5pcas2.peak6.net [10.100.13.24]) by smtp2.peak6.com (Symantec Messaging Gateway) with SMTP id 74.5A.21077.D8748805; Wed, 24 Oct 2012 14:54:53 -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:54:53 -0500 Message-ID: <5088478C.6000905@optionshouse.com> Date: Wed, 24 Oct 2012 14:54:52 -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: "pgsql-performance@postgresql.org" CC: Tom Lane Subject: Re: Setting Statistics on Functional Indexes References: <50881D73.100@optionshouse.com> <6668.1351105908@sss.pgh.pa.us> <508841FF.9040303@optionshouse.com> In-Reply-To: <508841FF.9040303@optionshouse.com> 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+NgFupnleLIzCtJLcpLzFFi42LhSuGV0O117wgw2HROx+L6zHPMFqdW3WVy YPK4sG8au8eiZxtZApiiGhhtEvPy8ksSS1IVUlKLk22V/AtKMvPzij3yS4tTFVwyi5NzEjNz U4uUFDJTbJWMlRQKchKTU3NT80pslRILClLzUpTsuBQwgA1QWWaeQmpecn5KZl66rZJnsL+u hYWppa6hkl08MkjoZMlYve4nS8E78Yq3R7YwNTA+EOpi5OSQEDCRmLRnMyuELSZx4d56ti5G Lg4hgROMEmu7bkM5Kxkl7v7sYQOp4hXQl9h87QiQzcHBIqAqseBrIUiYTUBXYlL/CrASIQEV ib6zN5hAbH4BYYl9H5qYQWxRgTCJ5Ts3M0GMEZQ4OfMJC4gtIuApMXv/NjCbWUBBYvf7DrB6 YQF7ic+zNzBCzMyXOH1rD9ihnAIGEnf/zGKDqLeVuDDnOlSvvMT2t3OYIepVJVY0T2SDeExB 4v+Vd0wTGEVmIVk9C0n7LCTtCxiZVzGKF5clJmdkmhUU55YUGOkVpCZmm+nlpZZsYgTFOtNp hR2Mm3usDjEKcDAq8fAKnG0LEGJNLCuuzD3EKMHBrCTCO/lBe4AQb0piZVVqUX58UWlOavEh Rh9gkE1klhJNzgemobySeENjUwNTS1NDU3NTQyMcwkrivJduA40XSAcmn+zU1ILUIphxTByc Ug2MAVL55x/cPVGVzT+5s9yW54LJcjWLwJcFx312nQ3i3lZgJ6O7dvH6BR/OC0mbrz5YEhAs 1TF30qn0jIwbuhk35Mw+M/CcCooq3RGenrpXVOHrtt6jEVK7Fpjmc2umX7BjZvLsnPaDZUlk cEYhs0vMGsH7txyK67L/hvdVKnznrlvBvL3/8wYlluKMREMt5qLiRAAPfZeJIgMAAA== X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201210/300 X-Sequence-Number: 48259 On 10/24/2012 02:31 PM, Shaun Thomas wrote: > The main flaw with my example is that it's random. But I swear I'm not > making it up! :) And then I find a way to make it non-random. Hooray: CREATE TABLE date_test ( id SERIAL, col1 varchar, col2 numeric, action_date TIMESTAMP WITHOUT TIME ZONE ); insert into date_test (col1, col2, action_date) select 'S:' || (a.num % 10000), a.num % 15000, current_date - a.num % 1000 from generate_series(1,10000000) a(num); create index idx_date_test_action_date_trunc on date_test (date_trunc('day', action_date)); create index idx_date_test_col1_col2 on date_test (col1, col2); set default_statistics_target = 500; vacuum analyze date_test; explain analyze select * from date_test where col1 IN ('S:96') and col2 = 657 and date_trunc('day', action_date) >= '2012-10-24' order by id desc, action_date; Sort (cost=9.38..9.39 rows=1 width=23) (actual time=83.418..83.418 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=83.409..83.409 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: 83.451 ms 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.077..0.077 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.069..0.069 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.105 m Then for fun: create index idx_date_test_action_date_trunc_col1 on date_test (date_trunc('day', action_date), col1); alter index idx_date_test_action_date_trunc alter column date_trunc set statistics -1; analyze date_test; Sort (cost=9.38..9.39 rows=1 width=23) (actual time=84.375..84.375 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=84.366..84.366 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: 84.410 ms o_O -- 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