Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 9301C63396A for ; Wed, 25 Feb 2009 16:15:44 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 68558-09 for ; Wed, 25 Feb 2009 16:15:37 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by mail.postgresql.org (Postfix) with ESMTP id 6410663339A for ; Wed, 25 Feb 2009 16:15:36 -0400 (AST) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1LcQAG-0006um-0G for pgsql-admin@postgresql.org; Wed, 25 Feb 2009 20:15:32 +0000 Received: from srv.protecting.net ([212.126.218.242]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 25 Feb 2009 20:15:31 +0000 Received: from hari.fuchs by srv.protecting.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 25 Feb 2009 20:15:31 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Harald Fuchs Subject: Re: "like" and index Date: Wed, 25 Feb 2009 21:15:21 +0100 Lines: 50 Message-ID: References: <8f750b7c0902250259w6065515as350aca3b5d7d8173@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: srv.protecting.net X-Archive: encrypt User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:E6VivljZEX1QpFw//1RqTF4ujgg= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200902/214 X-Sequence-Number: 31435 In article <8f750b7c0902250259w6065515as350aca3b5d7d8173@mail.gmail.com>, Tony Liao writes: > hi all, >       I have a table table_A (id serial,prefix varchar),for example. >       now I want to get the id of "johnsmith"'s prefix match table_A.prefix,so > I do select id from table_A where 'johnsmith' like prefix||'%'  ,the table_A is > very large so I would like to make index. create table_A_index on table_A > (prefix) >       I try to explain analyze,but it doesn't work ,it use seq scan. >       I try another index. drop index table_A_index; create table_A_index on > table_A(prefix varchar_pattern_ops); it doesn't work,too. If I understand you correctly, the "prefix" contrib package is what you need: CREATE TABLE tableA ( id serial NOT NULL, prefix prefix_range NOT NULL, PRIMARY KEY (id) ); CREATE INDEX tableA_prefix_ix on tableA USING gist (prefix gist_prefix_range_ops); COPY tableA (prefix) FROM stdin; john tom anne jim \. INSERT INTO tableA (prefix) SELECT x || 'test' FROM generate_series (1, 10000) g(x); ANALYZE tableA; EXPLAIN ANALYZE SELECT id, prefix FROM tableA WHERE prefix @> 'johnsmith'; will return something like that: Bitmap Heap Scan on tablea (cost=4.33..32.10 rows=10 width=19) (actual time=0.035..0.036 rows=1 loops=1) Recheck Cond: (prefix @> 'johnsmith[]'::prefix_range) -> Bitmap Index Scan on tablea_prefix_ix (cost=0.00..4.33 rows=10 width=0) (actual time=0.026..0.026 rows=1 loops=1) Index Cond: (prefix @> 'johnsmith[]'::prefix_range) Total runtime: 0.133 ms