Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YGcSW-0007Xx-Hk for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jan 2015 23:55:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YGcSV-00086a-Mi for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jan 2015 23:55:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YGcSU-00086U-QN for pgsql-performance@postgresql.org; Wed, 28 Jan 2015 23:55:42 +0000 Received: from mail-wi0-f175.google.com ([209.85.212.175]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YGcSJ-0005Ym-7Z for pgsql-performance@postgresql.org; Wed, 28 Jan 2015 23:55:41 +0000 Received: by mail-wi0-f175.google.com with SMTP id fb4so17346433wid.2 for ; Wed, 28 Jan 2015 15:55:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:organization:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=jBbQuDxvxzwt7XSoHGqbku0nvTUvpsjAJ6aTmB32k04=; b=RQWsj9t1D5NRgeubE72MiYPMttu4PeDMQdTqVjx11ZMUJVfLPCg1eN9aXDLBE0vEdR ROMItFb8qMh/IbTcA6o1U1fynoZAHRwXaCrvh89b4FB0RO/U1Qe9G+jCucsZ/oxlbSUH g00UC/lfXPtrTMK8x1+9ahp4pZoShO1afVvV5rTqqaGmmUvfKFtoxosZUXlzapzNFRcw amEXiorYD51fbDaVUNJz8vLEcISznqWqLu8f+MgiEQc1lMuOycZQsceS1CeUlYMXQS93 14LcROGv1PLMgmMRgv2YfsVmW2wzD1asJbb+2cnUQtZI+5pt/em+JrRiBCSsg5Ymfx53 KQtw== X-Gm-Message-State: ALoCoQkdtMbB1mPagkYyubHFhsLKc1RCYTcZWc/hy17QMDGAZEDRc7eJ/3bacicLboK34T6njomrN4XzrPgyELLouA6hp1pT63HT5ynjsDyZ4mAn8mZmvsBiu9K1sJlcdJcd84UsyQ/LaBD6+ABO0rHHMZTB5rIOPL2PrwuI4EGKmaTUS0wVj1V52On3chir+X5VTHJygeco X-Received: by 10.180.81.98 with SMTP id z2mr12359423wix.40.1422489329487; Wed, 28 Jan 2015 15:55:29 -0800 (PST) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id hv5sm8108027wjb.16.2015.01.28.15.55.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Jan 2015 15:55:28 -0800 (PST) Message-ID: <54C976ED.6030309@2ndquadrant.com> Date: Thu, 29 Jan 2015 00:55:25 +0100 From: Tomas Vondra Organization: 2ndQuadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: working around JSONB's lack of stats? References: <54C738E1.8080405@agliodbs.com> <54C93CF2.3020600@2ndquadrant.com> <54C96AAA.9050901@agliodbs.com> In-Reply-To: <54C96AAA.9050901@agliodbs.com> Content-Type: text/plain; charset=utf-8 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-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 29.1.2015 00:03, Josh Berkus wrote: > On 01/28/2015 11:48 AM, Tomas Vondra wrote: >> On 27.1.2015 08:06, Josh Berkus wrote: >>> Folks, >>> >> ... >>> >>> On a normal column, I'd raise n_distinct to reflect the higher >>> selecivity of the search terms. However, since @> uses contsel, >>> n_distinct is ignored. Anyone know a clever workaround I don't >>> currently see? >> >> I don't see any reasonable workaround :-( >> >> ISTM we'll have to invent a way to collect useful stats about contents >> of JSON/JSONB documents. JSONB is cool, but at the moment we're mostly >> relying on defaults that may be reasonable, but still misfire in many >> cases. Do we have any ideas of how that might work? >> >> We're already collecting stats about contents of arrays, and maybe we >> could do something similar for JSONB? The nested nature of JSON makes >> that rather incompatible with the flat MCV/histogram stats, though. > > Well, I was thinking about this. > > We already have most_common_elem (MCE) for arrays and tsearch. What > if we put JSONB's most common top-level keys (or array elements, > depending) in the MCE array? Then we could still apply a simple rule > for any path criteria below the top-level keys, say assuming that any > sub-key criteria would match 10% of the time. While it wouldn't be > perfect, it would be better than what we have now. So how would that work with your 'tags' example? ISTM most of your documents have 'tags' as top-level key, so that would end up in the MCV list. But there's no info about the elements of the 'tags' array (thus the 10% default, which may work in this particular case, but it's hardly a general solution and I doubt it's somehow superior to the defaults we're using right now). I think a 'proper' solution to JSONB stats needs to somehow reflect the nested structure. What I was thinking about is tracking MCV for "complete paths", i.e. for a document: { "keyA" : { "keyB" : "x", "keyC" : "z", } "keyD" : [1, 2, 3, 4] } We'd extract three paths "keyA.keyB" "keyA.keyC" "keyD" and aggregate that over all the documents to select the MCV paths. And then, for each of those MCV paths track the most common values. ISTM this would allow better estimations, but it has issues too: Firstly, it does not match the MCV structure, because it requires storing (a) MCV paths and (b) MCV values for those paths. Moreover, (b) probably stores different data types (some values are strings, some integers, etc.). Arrays might be handled just like regular arrays, i.e. tracking stats of elements, but it's still mixed data types. Secondly, I think it's based on the assumption of independence (i.e. that the occurence of one path does not depend on occurence of a different path in the same document). Same for values x paths. Which may or may not be be true - it's essentially the same as assumption of independence for predicates on multiple columns. While I do have ideas on how to approach this in the multi-column case, handling this for JSONB is going to be much more complex I think. But the first question (what stats to collect and how to store them) is the most important at this point, I guess. regards -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance