Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aiiky-0003zv-AI for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Mar 2016 13:23:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aiikx-0006H0-TM for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Mar 2016 13:23:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aiijb-0003Pm-4p for pgsql-hackers@postgresql.org; Wed, 23 Mar 2016 13:22:03 +0000 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1aiijX-0000mJ-Mg for pgsql-hackers@postgresql.org; Wed, 23 Mar 2016 13:22:01 +0000 Received: by mail-wm0-x236.google.com with SMTP id p65so233536097wmp.1 for ; Wed, 23 Mar 2016 06:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=qlMb191kYOTlZ3PWmEILyE0H2zXGzZ3eawCUrNC4SNY=; b=bCxsfkLqLRMVEYTAvL9ckGilZ9Uw+KDkoh0ZsWsWmjzqHXdhu3kVF4Pfw3VqyO4DK+ 4BZFC+y01/Iycb+v/BnQm+PYsF1U1KsG2S8sphzpGgp78DdapLtD5x/QPmntdcgDMRrt hK/0OfSUh1+q+/HLAlGA4oWtVpbyK+Tt1fqQrrvZGqug10d0+g+PrzS/xbRVfDSH+cvN LPrE32DPg36ogOHvGQHwssGXyAysUrUBYX3RuJOY7ctjAdV82dZOMw8ijXzSis33lTAB CxCgopYMgxSLw7C1h5iQkCyvyvK39xAl5ErmBqBNfEU/XceWbGYlN7/bouRlw8oBpEz8 tRpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=qlMb191kYOTlZ3PWmEILyE0H2zXGzZ3eawCUrNC4SNY=; b=VpY/h2wFOykWsDHdmZwo4xIjp7Czk2XUk5+3D595t3O2s8rL2yOi89zEMYztX/cDw2 H96pHcp4r0LQBsGjk2pw7TSmLleMhiJOsdTB7gQFObLVJzHZ5rkrwhpGF6GqPWQjtNgP PqR7K+uaQuJygwY2R3ANQIE9Kc/JID5+qtlfcu7ZHaobNPIqImKe4HZlgcHpFdo7mxQJ pV1z2lgtiU5rvMSEqcKBNLrGnP6uMScrg7SpabJEGT6L3TAL8C2686NF07deICaDkAW3 9PDwAsNyE8y2te1UkyAL4g3hX70mq311Nb7/4IoOYKAnE10o9xte4gppAZ3peeh1EQA3 sf0g== X-Gm-Message-State: AD7BkJIWQdW0I4Qod/smwByp0fyQKApQwUlVj7gQn9eUOKW00WghyDktLMGNwZiAgwq8Zjy/HmLBSHQ9yIO3ZyOXnQPN3JThfxX4TCdU8mqTDqCKXvL6XGskIRZsHLLUPuhD+sTvVhlWfKr+gy/0+Zo08XUOJ6Pd4zoAxqrpbnSbviKub5HOumpLTzZZkJYY9HwSm1dotAZS X-Received: by 10.28.172.194 with SMTP id v185mr25465206wme.21.1458739317402; Wed, 23 Mar 2016 06:21:57 -0700 (PDT) Received: from [10.137.2.17] (ip-78-45-216-26.net.upcbroadband.cz. [78.45.216.26]) by smtp.gmail.com with ESMTPSA id h7sm22217776wmf.9.2016.03.23.06.21.56 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 23 Mar 2016 06:21:56 -0700 (PDT) Subject: Re: multivariate statistics v14 To: Tatsuo Ishii References: <20160323.110125.1808526964660429411.t-ishii@sraoss.co.jp> <20160323.115758.938003466026607400.t-ishii@sraoss.co.jp> <20160323.120101.207302357952079425.t-ishii@sraoss.co.jp> <20160323.142004.624106180171245380.t-ishii@sraoss.co.jp> Cc: jeff.janes@gmail.com, alvherre@2ndquadrant.com, pgsql-hackers@postgresql.org From: Tomas Vondra Message-ID: Date: Wed, 23 Mar 2016 14:21:53 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Thunderbird/46.0a2 MIME-Version: 1.0 In-Reply-To: <20160323.142004.624106180171245380.t-ishii@sraoss.co.jp> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 03/23/2016 06:20 AM, Tatsuo Ishii wrote: >>> I am now looking into the create statistics doc to see if the example >>> appearing in it is working. I will get back if I find any. > > I have the ref doc: CREATE STATISTICS > > There are nice examples how the multivariate statistics gives better > row number estimation. So I gave them a try. > > "Create table t1 with two functionally dependent columns, > i.e. knowledge of a value in the first column is sufficient for > determining the value in the other column" The example creates table > "t1", then populates it using generate_series. After CREATE > STATISTICS, ANALYZE and EXPLAIN. I expected the EXPLAIN demonstrates > how result rows estimation is enhanced by using the multivariate > statistics. > > Here is the EXPLAIN output using the multivariate statistics: > > EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 1); > QUERY PLAN > --------------------------------------------------------------------------------------------------- > Seq Scan on t1 (cost=0.00..19425.00 rows=98 width=8) (actual time=76.876..76.876 rows=0 loops=1) > Filter: ((a = 1) AND (b = 1)) > Rows Removed by Filter: 1000000 > Planning time: 0.146 ms > Execution time: 76.896 ms > (5 rows) > > Here is the EXPLAIN output without the multivariate statistics: > > EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 1); > QUERY PLAN > -------------------------------------------------------------------------------------------------- > Seq Scan on t1 (cost=0.00..19425.00 rows=1 width=8) (actual time=78.867..78.867 rows=0 loops=1) > Filter: ((a = 1) AND (b = 1)) > Rows Removed by Filter: 1000000 > Planning time: 0.102 ms > Execution time: 78.885 ms > (5 rows) > > It seems the row numbers estimation (98) using the multivariate > statistics is actually *worse* than the one (1) not using the > statistics because the actual row number is 0. Yes, there's a mistake in the first query, because the conditions actually are not compatible. I.e. (i/100)=1 and (i/500)=1 have no overlapping rows, clearly. It should be EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0); instead. Will fix. > > Next example (using table "t2") is much better than the case using t1. > > Here is the EXPLAIN output using the multivariate statistics: > > EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1); > QUERY PLAN > -------------------------------------------------------------------------------------------------------- > Seq Scan on t2 (cost=0.00..19425.00 rows=9633 width=8) (actual time=0.012..75.350 rows=10000 loops=1) > Filter: ((a = 1) AND (b = 1)) > Rows Removed by Filter: 990000 > Planning time: 0.107 ms > Execution time: 75.680 ms > (5 rows) > > Here is the EXPLAIN output without the multivariate statistics: > > EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1); > QUERY PLAN > ------------------------------------------------------------------------------------------------------ > Seq Scan on t2 (cost=0.00..19425.00 rows=91 width=8) (actual time=0.008..76.614 rows=10000 loops=1) > Filter: ((a = 1) AND (b = 1)) > Rows Removed by Filter: 990000 > Planning time: 0.067 ms > Execution time: 76.935 ms > (5 rows) > > This time it seems the row numbers estimation (9633) using the > multivariate statistics is much better than the one (91) not using the > statistics because the actual row number is 10000. > > The last example (using table "t3") seems no effect by multivariate statistics. Yes. There's a typo in the example - it analyzes the wrong table (t2 instead of t3). Once I fix that, the estimates are much better. > In summary, the only case which shows the effect of the multivariate > statistics is the "t2" case. So I don't see why other examples are > shown in the manual. Am I missing something? No, thanks for spotting those mistakes. I'll fix them and submit a new version of the patch - either later today or perhaps tomorrow. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers