Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1hIE-00036N-KD for pgsql-performance@arkaria.postgresql.org; Thu, 18 Dec 2014 20:03:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1hIE-0006yM-1X for pgsql-performance@arkaria.postgresql.org; Thu, 18 Dec 2014 20:03:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1hIB-0006vL-TC for pgsql-performance@postgresql.org; Thu, 18 Dec 2014 20:03:24 +0000 Received: from nm20-vm0.bullet.mail.ne1.yahoo.com ([98.138.91.45]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y1hI7-00017x-JN for pgsql-performance@postgresql.org; Thu, 18 Dec 2014 20:03:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s2048; t=1418932996; bh=BvGEugh9fARyXy5CvevKGgvUEH/jy0Yd7fn5W991/Pc=; h=Date:From:Reply-To:To:Cc:In-Reply-To:References:Subject:From:Subject; b=exueG3WcQr2FC6O6tgEWJIYqGkHVIpDxLQ9uM1Q6Dlm7mVkW/EiV8sXHWVAsUAY78jOzoNnP/N2bVjhvHXAH8XA/6G34R/Q+Pw5SuHigNvIF63+YxqD9PcKP5GkSPnNd1oBie1s5mxiTpaHvtPDXYdloBJGx0qtIGijDicDEhe1ooqqTqoL1VW3er1TRqUqYl3xS/n6DFtKUinXiOqfoV50iZnrWTYb/NXpGL1Z8TMnvtT6THZEtbjpLpedSUTpeKlp8DXimHp8mP6Y/r0+JETqV/J4uJj3eA/LJkfCNEXFakjExSCXqFiW+HVEJJAtZBLjuchbpKzvFKveLUZN6Jw== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=ymail.com; b=hXIj/WTVygKDDgZ8p8S9rF/mhaHhraT3P6F/cumQla89jkR445iU1ZLpjmvLzgRdl7KK/Zb0EgS+ECEz5Ord7IsJAEj31SA40u3h+AnPTUqBAk4h/bMBPYhORghL0lqTKeHkJa4PQJEMeYqmzWl59+ollLWi/ZJ5VyCki9EwttMQ2rhMOPF+eKXxtofZUNqbCQOMHaoMPPUCRBZ2QmgAAv3vAuj2cTMGi4a0Q95WD8twMVTgGuubvAQctGRPc7jIwWvAu0BtUm0c40FzxgjF6VKuj4P5o9wXa6wF78TzAdk8ZTLiMFanT4Pzu6esOHY6rF0F4GU3KJEtFgMUA4t+cw==; Received: from [98.138.100.112] by nm20.bullet.mail.ne1.yahoo.com with NNFMP; 18 Dec 2014 20:03:16 -0000 Received: from [98.138.89.168] by tm103.bullet.mail.ne1.yahoo.com with NNFMP; 18 Dec 2014 20:03:16 -0000 Received: from [127.0.0.1] by omp1024.mail.ne1.yahoo.com with NNFMP; 18 Dec 2014 20:03:16 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 935671.66695.bm@omp1024.mail.ne1.yahoo.com X-YMail-OSG: ZTr4mW4VM1nWtiMIdIU4XDgBXUYTMxadb2lHB7gYST4Yfv6Evlb9g_tby158qRz 4np6grakxzX4GRmM9TOQFy26Fwg9fTXe1Kr3oWP7G3AsjqNJD4ZsImm1UxIcjvO3ZN4cmF7qn3Mh d2rlE50gFh0.iIE2.JFLfdbc4vrPO1q_BMwheIEBu_hLVNqoRf.HmG.hq5iILNIBQGcV3xHiyt3H 96BOAjbB7Rab5UugoIZM4tKwFaDcaybRW4nnkDACWKGbJMdx3l8mgxwUjUwgO8nOJ59Oeio67yXs j7lL66A1YRdwG9j5Efid1ljKYa7b2RzwcE1O6oT1uTZmaw4eUMdnC8UbtGgUKOnFMVa1Pa.YWGK2 y6.ychaoutqver.qJshXSBy1k9IEVmS.hjVOkCwR2tqK.qjSJSJOmG38JimGUA8oZzP3W69tbcO9 mHSnu2.xNooRtQOuBVjp__bP.vYXRQ.QR6ljU_hcI0C2wiYw1HnNQg8C_cyWM.f1Tlzux0fpEqI1 VxyVk7_k0pQ_JnyMcr4cgbIUFSbRY3lzbUxpN54aQZ.JqYGMg3.mYKOpkC2E- Received: by 98.138.105.213; Thu, 18 Dec 2014 20:03:16 +0000 Date: Thu, 18 Dec 2014 20:03:13 +0000 (UTC) From: Kevin Grittner Reply-To: Kevin Grittner To: Giuseppe Broccolo , Robert DiFalco Cc: Tom Lane , "pgsql-performance@postgresql.org" Message-ID: <123557937.571326.1418932993939.JavaMail.yahoo@jws100182.mail.ne1.yahoo.com> In-Reply-To: References: Subject: Re: Question about trigram GIST index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Length: 2425 X-Pg-Spam-Score: -2.0 (--) 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 Giuseppe Broccolo wrote: > I'm not sure about the '%' operator, but I'm sure that the GIST > index will never be used in the > > SELECT * FROM users WHERE lower(name) LIKE '%john%'; > > query; it is used for left or right anchored search, such as > 'john%' or '%john'. It *will* use a *trigram* index for a non-anchored search. test=# create table words (word text not null); CREATE TABLE test=# copy words from '/usr/share/dict/words'; COPY 99171 test=# CREATE EXTENSION pg_trgm; CREATE EXTENSION test=# CREATE INDEX words_trgm ON words USING gist (word gist_trgm_ops); CREATE INDEX test=# vacuum analyze words; VACUUM test=# explain analyze select * from words where word like '%john%'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on words (cost=4.36..40.24 rows=10 width=9) (actual time=17.758..17.772 rows=8 loops=1) Recheck Cond: (word ~~ '%john%'::text) Rows Removed by Index Recheck: 16 Heap Blocks: exact=4 -> Bitmap Index Scan on words_trgm (cost=0.00..4.36 rows=10 width=0) (actual time=17.708..17.708 rows=24 loops=1) Index Cond: (word ~~ '%john%'::text) Planning time: 0.227 ms Execution time: 17.862 ms (8 rows) test=# explain analyze select * from words where word ilike '%john%'; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on words (cost=44.05..556.57 rows=1002 width=9) (actual time=12.151..12.197 rows=24 loops=1) Recheck Cond: (word ~~* '%john%'::text) Heap Blocks: exact=4 -> Bitmap Index Scan on words_trgm (cost=0.00..43.80 rows=1002 width=0) (actual time=12.124..12.124 rows=24 loops=1) Index Cond: (word ~~* '%john%'::text) Planning time: 0.392 ms Execution time: 12.252 ms (7 rows) Note that a trigram index is case-insensitive; doing a case-sensitive search requires an extra Recheck node to eliminate the rows that match in the case-insensitive index scan but have different capitalization. Because of that case-sensitive is slower. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance