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 1iiQmn-0005Wb-VF for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Dec 2019 22:30:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iiQmm-0005Cc-Ff for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Dec 2019 22:30:16 +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 1iiQmm-0005CU-47 for pgsql-bugs@lists.postgresql.org; Fri, 20 Dec 2019 22:30:16 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iiQmf-0002eN-BK for pgsql-bugs@lists.postgresql.org; Fri, 20 Dec 2019 22:30:14 +0000 Received: by mail-wm1-x341.google.com with SMTP id p9so10449178wmc.2 for ; Fri, 20 Dec 2019 14:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rxzxhoFJjUjza6fggNRZ49M77jCfYK+6H02pM81usPs=; b=rAM4qngslC35tFuSCUDhw84VbUwo1jB1Dzv8+U9/jJ3bWGbQkApHqZD+uTxdzxAN7K AD1IFNnzyFa8LrQhQzbZg6NjSDNAxr+ep3oMLVlY8hSn+iYf526FkQExxGoJllagf3VM qMWq6cKNjEc12gWc1tjE0nD+DXzMsvDyE1bZKRjea01kmMiVubPjkGlq0ha+YNgMg2Qp kSzB2LRCzh0mpRNOQ3rp7TlD/afAz9XnhvX3imrNdUOkNlpzDCkrnE2ZdN0t1bY+yGSa CXxUrKDhyzntw1YDiqiof1holbIoSFnjsniAee7K5htzfHehjGbrhVMQzr4q+63NE3uS KFKQ== 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=rxzxhoFJjUjza6fggNRZ49M77jCfYK+6H02pM81usPs=; b=ZQabwn84tm/Da/ypeogvtrXroGS3n354UIuCFcvMh4V4HZ0z/tJk0fbx0l8/AtBlG0 IJV0AokaIwRGMChSkYDcYPc1vi6tZpDJRAX+dy0wNRl29zPYFmFdrxNr3XU7BnA2SM6r ULfp/EHzD+QdyLNxGubepA3pxgjgAYqiQia0UDI4GHJPj7wwsjviqgRhRZwJruae31hj A0mRFCVeHLKpqOaYl0zol4QPECKaf5LD78F2CZSnsQx+dcqqluIBaVX5IFis5hBD9PGe azERiI6vgUoWcc3Z9MV0131RlCXDOHMbPd3tgyP0AChyxt+wMCG5BiSZILfGlnF6lILy SELQ== X-Gm-Message-State: APjAAAWq2UtqS9r2CuW6n/TUvE9cSJ/9DuTSwPttmSk8N5U30GUAWWCT So3iXQFYqAHIN0W3jqUhZX/xwg== X-Google-Smtp-Source: APXvYqzYtwPomKMvyrlenqDaOoIDuwEetUxhNVK+EUNnjmyErxpupKOFzrYx7MOEkpWGg0A0VRaLUw== X-Received: by 2002:a1c:9d52:: with SMTP id g79mr18880435wme.148.1576881007869; Fri, 20 Dec 2019 14:30:07 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id b16sm12105628wrj.23.2019.12.20.14.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2019 14:30:07 -0800 (PST) Date: Fri, 20 Dec 2019 23:30:05 +0100 From: Tomas Vondra To: Zhihong Zhang Cc: pgsql-bugs@lists.postgresql.org Subject: Re: Indexing on JSONB field not working Message-ID: <20191220223005.f6w4ff4kdbgtanpz@development> References: <0cf001d5b77f$bfe47e10$3fad7a30$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <0cf001d5b77f$bfe47e10$3fad7a30$@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Dec 20, 2019 at 04:52:17PM -0500, Zhihong Zhang wrote: >I have an index on JSONB fields like this, > > > >CREATE INDEX float_number_index_path2 > > ON public.assets USING btree > > (((_doc #> '{floatValue}'::text[])::double precision) ASC NULLS LAST) > > TABLESPACE pg_default; > > > >However query doesn't use it, > > > >explain select id, _doc->>'floatValue' from assets where (_doc #> >'{floatValue}'::text[])::double precision < 3.0 limit 3; > > > >Limit (cost=0.00..3.24 rows=3 width=53) > > -> Seq Scan on assets (cost=0.00..936605.40 rows=867607 width=53) > > Filter: (((_doc #> '{floatValue}'::text[]))::double precision < >'3'::double precision) > > The index scan is likely expected to be more expensive than the plain sequential scan with the LIMIT interrupting it pretty much right away (it's expected to scan only ~0.0003% of the table. You can probably push the database to use the index by disabling sequential scans, i.e. SET enable_seqscan = off; and then doing the explain again. The interesting question however is which of the plans is faster. It's quite possible the database is making the right choice - index scans are not necessarily faster. > >The version of the database, > > > >"PostgreSQL 11.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.2 >20140120 (Red Hat 4.8.2-16), 64-bit" > > > >However, the index works for text field in JSONB. > Well, the text field probably has different statistics, so the sequential scan would have so scan much larger part of the table. Who knows - you haven't shared the execution plans. > > >Let me know if I can provide more information. > Show us explain analyze for both queries, with both index-scan and seq-scan (you'll have to use enable_seqscan and enable_indexscan to force the plan choice). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services