Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bIJAH-0005eN-Uy for pgsql-admin@arkaria.postgresql.org; Wed, 29 Jun 2016 17:20:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bIJAH-0002wv-H2 for pgsql-admin@arkaria.postgresql.org; Wed, 29 Jun 2016 17:20:41 +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_2) (envelope-from ) id 1bIJAH-0002wo-2r for pgsql-admin@postgresql.org; Wed, 29 Jun 2016 17:20:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bIJAD-00079N-Jc for pgsql-admin@postgresql.org; Wed, 29 Jun 2016 17:20:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u5THKYLq021193; Wed, 29 Jun 2016 13:20:34 -0400 From: Tom Lane To: Max Kremer cc: pgsql-admin@postgresql.org Subject: Re: GIN vs BTREE - query planner picking the wrong one some times In-reply-to: References: Comments: In-reply-to Max Kremer message dated "Tue, 28 Jun 2016 17:16:18 -0400" Date: Wed, 29 Jun 2016 13:20:34 -0400 Message-ID: <21192.1467220834@sss.pgh.pa.us> X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org Max Kremer writes: > The problem is when I run EXPLAIN ANALYZE on the above query now its only > ever using location_idx_gin and ignoring location_idx which kinda sucks > cause the location_idx index is faster at trailing % queries. The query > planner seems to ignore my BTREE index in all cases and uses the GIN index. I tried to reproduce that, but in my hands the planner just about always prefers the btree if it has a choice. Especially for longer strings such as in your example. (It looks to me like the cost estimate for GIN is mostly proportional to the number of trigrams in the query string; I find that GIN is estimated to cost about the same for LIKE strings as short as 'the%', but loses badly for anything longer.) Can you show a concrete example (with sample data) where this doesn't hold? > Some metrics (queries trailing %): > - BTREE : <1 second explain: https://explain.depesz.com/s/7wgx > - GIN : 3.8 seconds explain: https://explain.depesz.com/s/wYhk The obfuscation of the index conditions makes these just about useless for telling what's going on :-( regards, tom lane -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin