Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZT0XZ-00036Q-4D for pgsql-performance@arkaria.postgresql.org; Sat, 22 Aug 2015 04:36:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZT0XY-0001SK-7P for pgsql-performance@arkaria.postgresql.org; Sat, 22 Aug 2015 04:36:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZT0XX-0001SC-Qo for pgsql-performance@postgresql.org; Sat, 22 Aug 2015 04:36:23 +0000 Received: from mail-wi0-f176.google.com ([209.85.212.176]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZT0XU-0006Q5-5M for pgsql-performance@postgresql.org; Sat, 22 Aug 2015 04:36:23 +0000 Received: by wicne3 with SMTP id ne3so29547847wic.0 for ; Fri, 21 Aug 2015 21:36:19 -0700 (PDT) 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:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=ZWyVRB8lEXpdXIvHlGwZKOMZSCAdaNHTwz05Waxn46E=; b=je/PRstTvizPUOQ4tZ20qE+18BYxOdbwevVcuHGMe7BW1+difRFviDKdACaP6ynncI PflV1qbNoIKZawUX4cx+CWPe3SJioZm9nwwl8DuiE/Jlu3x4MAOScYLEdMsVJkrx4elv UKmwLx0/bnPdeXzsCA1r99P2D3GE5uVnZl7JrvWLhAvEVhixoQtNmPf6UUFK8VQA81ig PeiecFL2n4uNGdX2u2LP+K8J3ib5vmgqVsGSrW/3JPdK1s8ImWm8TwgI3RmzyldvXQ5E Q0bN4tf2lVPGfoEXuOKa5t8vxJs4rUA5RfdHe/gdVUO9SeGjww5VtUAaAVozMea4zMBc 1GJQ== X-Gm-Message-State: ALoCoQmYjoXnRWVrbM0tIt2kBNzZZMqWuaKKJFAopbwvXPrX6TCpHFaGhWNMKyjZF4YFgdHTYnRUAo/ErPjNbCwKqkwCtun84MNavbl7S8jgj9s6gRnUI9i84BSLwM2/uCgNEz/1oSxdzWuD9s4ADxIXpgYhERmzTCLOXPOWy9z0H2/KMedDkAaL/140Zqg70JHb4weQPlkOEt++S4yo0sev10v8HhIRQw== X-Received: by 10.194.57.19 with SMTP id e19mr22906206wjq.152.1440218179238; Fri, 21 Aug 2015 21:36:19 -0700 (PDT) Received: from [10.137.2.12] (ip-78-45-136-74.net.upcbroadband.cz. [78.45.136.74]) by smtp.gmail.com with ESMTPSA id l13sm12703304wjr.18.2015.08.21.21.36.17 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Aug 2015 21:36:18 -0700 (PDT) Message-ID: <55D7FC3F.6070104@2ndquadrant.com> Date: Sat, 22 Aug 2015 06:36:15 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: query not using GIN index References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit 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 Hi, On 08/22/2015 03:55 AM, Guo, Yun wrote: > Hi, > > We have a query on a column with GIN index, but query plan chooses not > using the index but do an seq scan whichi is must slower > > CREATE INDEX idx_access_grants_on_access_tokens ON access_grants USING > gin (access_tokens); > > explain analyze SELECT "access_grants".* FROM "access_grants" WHERE > (access_tokens @> ARRAY['124e5a1f9de325fc176a7c89152ac734']) ; > QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------- > Limit (cost=0.00..7.46 rows=1 width=157) (actual > time=260.376..260.377 rows=1 loops=1) > -> Seq Scan on access_grants (cost=0.00..29718.03 rows=3985 > width=157) (actual time=260.373..260.373 rows=1 loops=1) > Filter: (access_tokens @> > '{124e5a1f9de325fc176a7c89152ac734}'::text[]) > Rows Removed by Filter: 796818 > Total runtime: 260.408 ms > I find it very likely that the explain output actually comes from a slightly different query, including a LIMIT 1 clause. That might easily be the problem here, because the optimizer expects the 3985 "matches" to be uniformly distributed in the table, so it thinks it'll scan just a tiny fraction of the table (1/3985) until the first match. But it's quite possible all at rows are end of the table, and the executor has to actually scan the whole table. It's difficult to say without further details of the table and how the data are generated. > We tested on smaller table in development region and it chooses to use > the index there. However, in production size table it decides to ignore > the index for unknown reasons. Please provide explain output from that table. It's difficult to say what's different without seeing the details. Also please provide important details about the system (e.g. which PostgreSQL version, how much RAM, what work_mem/shared_buffers and such stuff). > Is the large number of tuples skewing the query planner’s decision > or the index itself is larger than the table therefor it would decide > to do table scan? What large number of tuples? The indexes are supposed to be more efficient the larger the table is. 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