Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id B44767C454C for ; Wed, 14 Dec 2011 21:54:28 -0400 (AST) Received: from mail-iy0-f174.google.com ([209.85.210.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rb0Wd-0006PL-7B for pgsql-performance@postgresql.org; Thu, 15 Dec 2011 01:54:27 +0000 Received: by iaeh11 with SMTP id h11so2019509iae.19 for ; Wed, 14 Dec 2011 17:54:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=s6zNgS3Hpz2U7ROPRmqFTbSM4SvBSQ2uY4zpqX+JuWQ=; b=boeEOr6Q5uVRis6/Yrtt7cUgNOE9996o/EEQqXuyXHFLsTVsuQRQBxcNXfbBp0hkq4 yytM/Mu6nKt3TH4AlVxdNK6q9oXpKlr/MN0MgpdsGy6d0w2QHtF982f+cO0XtWV/37De N7Aca1FYmhYGW/Hd0hF/Hd3NSu/voEYAR1zso= Received: by 10.50.183.133 with SMTP id em5mr952749igc.73.1323914050615; Wed, 14 Dec 2011 17:54:10 -0800 (PST) Received: from [192.168.1.9] ([116.226.71.216]) by mx.google.com with ESMTPS id ew6sm7403002igc.4.2011.12.14.17.54.06 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 14 Dec 2011 17:54:09 -0800 (PST) Message-ID: <4EE9533E.1050001@gmail.com> Date: Thu, 15 Dec 2011 09:54:06 +0800 From: Rural Hunter User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: Marti Raudsepp CC: pgsql-performance@postgresql.org Subject: Re: Is it possible to use index on column for regexp match operator '~'? References: <4EE8C94B.5000607@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201112/121 X-Sequence-Number: 45920 actually I stored the pattern in col1. I want to get the row whose col1 pattern matches one string 'aaa'. 于2011年12月15日 4:43:37,Marti Raudsepp写到: > 2011/12/14 Rural Hunter: >> for example, the where condition is: where 'aaaa' ~ col1. I created a normal >> index on col1 but seems it is not used. > > I assume you want to search values that match one particular pattern, > that would be col1 ~ 'aaaa' > > The answer is, only very simple patterns that start with '^'. Note > that you MUST use the text_pattern_ops index opclass: > > # create table words (word text); > # copy words from '/usr/share/dict/words'; > # create index on words (word text_pattern_ops); > # explain select * from words where word ~ '^post'; > Index Scan using words_word_idx on words (cost=0.00..8.28 rows=10 width=9) > Index Cond: ((word ~>=~ 'post'::text) AND (word ~<~ 'posu'::text)) > Filter: (word ~ '^post'::text) > > ---- > > If you just want to search for arbitrary strings, in PostgreSQL 9.1+ > you can use pg_trgm extension with a LIKE expression: > > # create extension pg_trgm; > # create index on words using gist (word gist_trgm_ops); > # explain select * from words where word like '%post%'; > Bitmap Heap Scan on words (cost=4.36..40.23 rows=10 width=9) > Recheck Cond: (word ~~ '%post%'::text) > -> Bitmap Index Scan on words_word_idx1 (cost=0.00..4.36 rows=10 width=0) > Index Cond: (word ~~ '%post%'::text) > > ---- > > There's also the "wildspeed" external module which is somewhat faster > at this: http://www.sai.msu.su/~megera/wiki/wildspeed > > And someone is working to get pg_trgm support for arbitrary regular > expression searches. This *may* become part of the next major > PostgreSQL release (9.2) > http://archives.postgresql.org/message-id/CAPpHfduD6EGNise5codBz0KcdDahp7--MhFz_JDD_FRPC7-i=A@mail.gmail.com > > Regards, > Marti >