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 1lWbCV-0005wK-A1 for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 08:48:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWbCT-0006xL-Uh for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 08:48:41 +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 <9erthalion6@gmail.com>) id 1lWbCT-0006xC-L8 for pgsql-general@lists.postgresql.org; Wed, 14 Apr 2021 08:48:41 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1lWbCO-0005cX-Pj for pgsql-general@postgresql.org; Wed, 14 Apr 2021 08:48:40 +0000 Received: by mail-ej1-x635.google.com with SMTP id r12so30214300ejr.5 for ; Wed, 14 Apr 2021 01:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=N17qZNU5xsMlqOuRb3jLnCF0r7jMMiWDr8IRHdjHkq0=; b=De6L5Auo1BnVEUcoIGjuTuQ5xh4ICdHaEJRc6L963FqhehZyaWkf8K2GDX0Uch5fvr o/Zmc+yXB8dbi2IKcsfAa84Hcvh4cE1WH4ewu8FDluhDQmrndh8ok5gMjm1touev9Yhq f9i7EsZZyiLc3oeC8WsVvsrQSkiJG4NiZKPOhnAb1glO8zyBj73LiELTBZ6cM4c9Vx9X TW09pOX56wjPQU701RGtzveNUTGznX4nyXEOwRK6yoZGxmmtn2ExUpqo06dZk6rfYQDR nbOuIIFa2ng4n+X0QioX7eHk0YQAAXaiBX8urysKSWi0ZqEXGUhdTbVJR+UMPFzW6ZzP hAUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=N17qZNU5xsMlqOuRb3jLnCF0r7jMMiWDr8IRHdjHkq0=; b=qjBdLrmSIc0q+aPyqo8CCDJsBZWeRjh6/CeGMF9QG5PVAAoTgR7+MLm5vpSgR4XOio 8qNXanaMTkjEQhq7CvSrUvoypti9PdSXZlVtgFr7q2TLvejtbCMUAdzoI33ElaB0tRur bAgmnzHrHO1BDFKMC51buyeoOB/eq4w3RQg6qJuU07QlrjiCkeIa1vR4STbb6r2xQ6q6 CbBqsu6K9Ml82ixtTGi98i0gWLYhFh4JbnWi8irxZtpZxKDTx40kFW/vB4mJ9QY8gpc+ 0cJ4rFlCf25xTQCU+axZqDi3sGGH1AnkSLIEPMYYEXNkY1B2YNxz4AT//rbevZ9goAaY 7y9A== X-Gm-Message-State: AOAM531IFn0RCf/1n8ddVqynS9Gcdf94Ae75ziaxNcOMBWmZbDS84Q5l R+Enas0VO/qkA5xbgTjL/BQidpa5Hdc= X-Google-Smtp-Source: ABdhPJx0jYvZS2Fvf6F+lqGW5lTukcUuvaKnnl8f76vq0x8SjYg3fqIl3/ByegD+njzQdXLcYJLtpg== X-Received: by 2002:a17:906:3018:: with SMTP id 24mr1388152ejz.186.1618390115352; Wed, 14 Apr 2021 01:48:35 -0700 (PDT) Received: from localhost (dslb-178-012-047-165.178.012.pools.vodafone-ip.de. [178.12.47.165]) by smtp.gmail.com with ESMTPSA id i2sm8618183ejv.99.2021.04.14.01.48.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Apr 2021 01:48:34 -0700 (PDT) Date: Wed, 14 Apr 2021 10:48:45 +0200 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Mitar Cc: pgsql-general@postgresql.org Subject: Re: Why is writing JSONB faster than just JSON? Message-ID: <20210414084845.rkbe525twb4jr4fw@localhost> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Tue, Apr 13, 2021 at 11:38:04PM -0700, Mitar wrote: > Hi! > > I have a project where we among other data want to store static JSON > objects which can get pretty large (10-100 KB). I was trying to > evaluate how it would work if we simply store it as an additional > column in a PostgreSQL database. So I made a benchmark [1]. The > results surprised me a bit and I am writing here because I would like > to understand them. Namely, it looks like writing into a jsonb typed > column is 30% faster than writing into a json typed column. Why is > that? Does not jsonb require parsing of JSON and conversion? That > should be slower than just storing a blob as-is? > > [1] https://gitlab.com/mitar/benchmark-pg-json Interesting. I've tried to reproduce the schema and queries from the repository above (with a different generated data though and without the app itself) and indeed got a bit slower (not 30%, but visibly) execution for json column instead of jsonb. There are couple of important points: * Parsing is happening in both cases, for json it's done for validation purposes. * Compression of json data is actually dominates the runtime load for large json objects, making other factors less visible and reducing difference in size (jsonb also should be a bit bigger, that's why I would think it would be slower). * At the same time perf shows that json compression spends a bit more time in pglz_find_match (mostly around accessing and checking history entries), which is compression optimization via history table. So probably due to differences in data layout this optimization works slightly worse for plain json than for jsonb?