Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIvnV-0006H8-FT for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 14:21:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIvnT-0001NK-Ur for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 14:21:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIvnT-0001N3-Gq for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:21:35 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iIvnO-0004ip-VX for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:21:34 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 877D621AB4; Fri, 11 Oct 2019 10:21:29 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 11 Oct 2019 10:21:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=p n3R8nNDYrZjL68aenzaTGhvW5vy6f1qHdwafVBLgqc=; b=VrHWW5M02TBn734le +nDES4mTxkMwIa5pt54BP0Lx6R/tuTqMhyvAIhdjaGMJY5cm4yFUjjwUpGg5FM9I Ur3bLaEnP6G9WV3O+hIZKYsYIkQsdpmoRcENiSd+m2KYQQj7NrXkhjo5TkWw1y0d oN0FjLij4VsHsFBPi7O0E+pwgD25OOAIRksDH7e41noEWatknbPxo6EkYkWic/to PgqrzoaOzui6hYe/ZHNwBRKXytrtmUB5YvdEhhGBkZAdwEtg42Md0F7FTS4uzbk+ ZxjJcKfguQUzn38ZmsEFhmdY5Lx0Yn/u9ND9fwDB3+OqouD0dVwCjFY3gaJMPlME 6Rtwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=pn3R8nNDYrZjL68aenzaTGhvW5vy6f1qHdwafVBLg qc=; b=p3yBdXOmMIrsukK40BY+o5AtaMW+FeFL4uaHCutwLNf6LoNNmV175pjwz D3MTG3Ic3pHd2cYhbknEayrk78KrEhvo+Fzqcz3BELYbozZchzG9j4Z+NFv++u4E v1i1sDqT6n48OAHxIUOwnu06eZJ+iuwoc2zW53m+kcMT8a8cBtuO0DuUAlnBCgki A9Jyb7CdtQn6vmSTpN2aA+GIqw0EAZpCxSc+02QSIgG3MpsNyAmpdCVbHXltsevN dA1es+PVXXmjQZhcbC3z9fLoGJWqP7AmEpJJxk03HCmf5I/Bx3JS4XXI+ApPo2sp 0BcNXAxbCRsN1hDOHUCCfROPVhxMg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrieehgdejiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepshhlihguvghshhgrrh gvrdhnvghtpdhpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddvrddufeej rddufeehnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrse grkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.137.135]) by mail.messagingengine.com (Postfix) with ESMTPA id 79B7F8005B; Fri, 11 Oct 2019 10:21:28 -0400 (EDT) Subject: Re: JSON vs. JSONB storage size To: Thomas Kellerer , pgsql-general@lists.postgresql.org References: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> From: Adrian Klaver Message-ID: Date: Fri, 11 Oct 2019 07:21:27 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/11/19 4:40 AM, Thomas Kellerer wrote: > I recently stumbled over the presentation "How to Use JSON in MySQL Wrong" by Bill Karwin[1] > > While most of the indexing part simply doesn't apply to Postgres, I was curious about the statement that the data type of a json value inside the json matters as well (Slide 56) > > Apparently in MySQL storing {"a": 123456789} takes less space than {"a": '123456789'} > > So I tested that with Postgres both using json and jsonb - my expectation was, that this would be similar in Postgres as well. > > However, it turned out that for a json column there was no difference at all (both versions would show up the same with pg_total_relation_size()) > > The table size with jsonb was bigger in general, but the one with the "integer" value was even bigger than the one with the "string" storage. > > The following little test script: > > create table json_length_test1 (id serial primary key, d json); > insert into json_length_test1 > select i, jsonb_build_object('a', 1234567890) > from generate_series(1,1e6) t(i); > > create table json_length_test2 (id serial primary key, d json); > insert into json_length_test2 > select i, jsonb_build_object('a', '1234567890') > from generate_series(1,1e6) t(i); > > create table jsonb_length_test1 (id serial primary key, d jsonb); > insert into jsonb_length_test1 > select i, jsonb_build_object('a', 1234567890) > from generate_series(1,1e6) t(i); > > create table jsonb_length_test2 (id serial primary key, d jsonb); > insert into jsonb_length_test2 > select i, jsonb_build_object('a', '1234567890') > from generate_series(1,1e6) t(i); > > select 'json', pg_size_pretty(pg_total_relation_size('json_length_test1')) as json_int_size, > pg_size_pretty(pg_total_relation_size('json_length_test2')) as json_text_size > union all > select 'jsonb', pg_size_pretty(pg_total_relation_size('jsonb_length_test1')) as json_int_size, > pg_size_pretty(pg_total_relation_size('jsonb_length_test2')) as json_text_size > > > Returns (Postgres 12, Windows 10) > > ?column? | json_int_size | json_text_size > ---------+---------------+--------------- > json | 71 MB | 71 MB > jsonb | 87 MB | 79 MB > > I am a bit surprised by this (not because the jsonb sizes are generally bigger, but that the string value takes less space) > > Is this caused by the fact that a string value compresses better internally? Not sure if it applies here: https://www.postgresql.org/docs/11/datatype-json.html "When converting textual JSON input into jsonb, the primitive types described by RFC 7159 are effectively mapped onto native PostgreSQL types, as shown in Table 8.23. ..." Table 8.23. JSON primitive type PostgreSQL type Notes ... number numeric NaN and infinity values are disallowed ... > > > Thomas > > [1] https://www.slideshare.net/billkarwin/how-to-use-json-in-mysql-wrong > > > -- Adrian Klaver adrian.klaver@aklaver.com