Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmIU2-00006a-0E for pgsql-general@arkaria.postgresql.org; Mon, 07 Dec 2020 15:31:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmITz-0008Lv-RH for pgsql-general@arkaria.postgresql.org; Mon, 07 Dec 2020 15:31:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmITz-0008Lo-Ex for pgsql-general@lists.postgresql.org; Mon, 07 Dec 2020 15:31:23 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmITv-0004uv-6X for pgsql-general@lists.postgresql.org; Mon, 07 Dec 2020 15:31:22 +0000 Received: by mail-ed1-x534.google.com with SMTP id r5so14111199eda.12 for ; Mon, 07 Dec 2020 07:31:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=H2RE6GG2LWu24ZUPG70FwXf3s4UKmUyS5YHox9tSDlc=; b=xkcZSeFyI0feeDNiV2s6hLIT1afJdYCvCpSrYTR8PUFOS9HtNZp22F5z4iEhTlAgJM E/cYs282h1E4n1RHK6LfZzjQwtNlC4vb6GsEw3P6pqzpYzYM1Nx5tbEm7VC2eymNhecO xBP9r0xyXqcy/c9IdOuOPkDCu3F5zYMkgzpKjiGhQqt++6Pd8TA/aL++cgbYowZFJS2P MLtvmYmX+nIZARZeD2Nkzc4kW4vpOdDoXu/9e3vRPyL0jtjC7E6Jxh+ZdeS7hmvOWuES B2ZQ+XVzWlILdW84+ouqrqM8Urk9xGYyGzrz3Y8boq2lWrhc7k8BAPCJmN2Dos35aUvH Nu4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=H2RE6GG2LWu24ZUPG70FwXf3s4UKmUyS5YHox9tSDlc=; b=hnrT4jRNVOj8EiHv5BYeN/RVXOGOZUBdQNN0Jm2tYBbABtgYaqMSAG5fmZkzta6za0 ab9COxWxHqfVo8ZZps/0vgADpuJC5J+apG/I2qhQpIOrZsNnizLMRWk9CRqMPL+3tNNc r6Bo1xjhIPUmDqCyEhW2PBNUd3JVCMqPBasoeeElDXsguaEj3mfabfmN9JBk8yOttw9d ix7w0QxN0iRkLnRfmHDUDjW8XT6gnAddg0mY5k4wPmbwWJGGaTm1GT0bNU+AnZN5qyGE VXtUfgT46PqlAt4bj1e269Ed7VxRUUEaOu06Agj+2KGkBFvNaXXgcX5GVIvcIxrcQjDN /jnw== X-Gm-Message-State: AOAM532LZ1rPSiXBYH/lJ67w+NxrdveFEWreR0vONOiCjLgCYu6BGY7s 1TwQhdsJUyGdK/rPpCrSWqVYd85kIXzhkpePA95cjq5NrXZoEg0Uo/vQnQM9W5UxmA5OcRL/3Qx CLPFvrYSo4oXP60uYBlq5rEsjCP5R/QtAtsDL9CVYvrknYrDC4eupZRaaGlcxEwLXVe4cN1OcRv ziRmkt60EEAxgfYhci5Yru0jgKKmR96ts6kWLEZVJIDxuU8ZEyd1u9YcVFy9uTHapPZk4s8NC3x 23OXtnROAQBVymj94zxff0eQqAiY56MiBRCNVB1FbJZMRS+yfBBRg== X-Google-Smtp-Source: ABdhPJz2xrzUjTd5A6gNZYFS2RzlllE7eST0MbjvrBR7zpYRvld58Xexs+D16iIBlj1tFRNuVsNWaA== X-Received: by 2002:aa7:d608:: with SMTP id c8mr18138289edr.260.1607355077082; Mon, 07 Dec 2020 07:31:17 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-249-71.net.upcbroadband.cz. [86.49.249.71]) by smtp.gmail.com with ESMTPSA id i4sm13676401edu.78.2020.12.07.07.31.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Dec 2020 07:31:16 -0800 (PST) Subject: Re: Extended statistics for correlated columns, row estimates when values are not in MCVs list To: Michael Lewis , PostgreSQL General References: From: Tomas Vondra Message-ID: <2ba2609a-3c22-396d-a39a-7a7795909296@enterprisedb.com> Date: Mon, 7 Dec 2020 16:31:15 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/4/20 11:53 PM, Michael Lewis wrote: > On version 12.5, I have a query similar to the below where I am getting > rather unfortunate row estimates and a sub-optimal index choice as a result. > > SELECT > id > FROM > messages > WHERE > client_id = 1234 > and site_id = 889977 > and message_type_id in ( 59, 62, 102, 162 ) > and sent_on > NOW() - INTERVAL '3 days'; > > I created extended statistics (all types) on client_id and site_id, > analyzed the columns, and in pg_stats_ext.dependencies (4 = client_id, 7 = > site_id) have: > {"4 => 7": 0.002997, "7 => 4": 0.896230} > > When I check row estimates like below, I get results that indicate the > columns are expected to be independent still. > explain SELECT id FROM messages; --889594304 > explain SELECT id FROM messages where client_id = 1234; --133439 > explain SELECT id FROM messages where site_id = 889977; --28800 > explain SELECT id FROM messages where client_id = 1234 and site_id = > 889977; --4 > I was a bit confused about this initially, because why would it still be estimated as independent clauses, with the extended stats defined? But I think the explanation is fairly simple - the combination of values is simply rare enough not to be included in the MCV list, so it probably gets estimated using the "regular" logic as if independent. But we still mark the clauses as estimated, so the functional dependencies are not really considered. (I wonder if the code in PG14 would work better, but that's of little use of course.) > However, I pick a client & site ID pair which show up in the MCVs list, > then I get the same estimate when querying for that site_id with or without > including the client_id. That is great. > I think this is consistent with the above explanation - in this case the MCV actually kicks in, significantly improving the estimate. > Is it reasonable to expect that if the correlation between two columns is > rather high, then the optimizer might figure the columns are not > independent and perhaps would give less weight to the value derived from > independent column statistics? With table statistics, it is possible to set > a static value or ratio for something like ndistinct. Any chance for > something similar on dependency someday? > > Perhaps I am expecting too much or have a poor understanding of what > extended statistics can or someday might do. I deal with under estimates > from correlations between client_id and sites or other similar > dependent objects a fair bit and am hopeful to resolve some of those > planning problems with extended stats, without the maintenance overhead of > migrating everything to a single client per database to get more specific > statistics. > What you might try is defining the statistics with only the functional dependencies. That should consider the column-level correlation even when the combination of values is not in the MCV. It might make the "good" estimate worse, but that's an inherent trade-off. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company