Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0A437632D8C for ; Wed, 25 Feb 2009 07:48:17 -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 26329-05 for ; Wed, 25 Feb 2009 07:48:11 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.168]) by mail.postgresql.org (Postfix) with ESMTP id 64E406324E5 for ; Wed, 25 Feb 2009 07:48:10 -0400 (AST) Received: by wf-out-1314.google.com with SMTP id 23so3603602wfg.28 for ; Wed, 25 Feb 2009 03:48:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.142.240.9 with SMTP id n9mr26850wfh.0.1235562489039; Wed, 25 Feb 2009 03:48:09 -0800 (PST) In-Reply-To: <200902251241.40700@zmi.at> References: <8f750b7c0902250259w6065515as350aca3b5d7d8173@mail.gmail.com> <200902251241.40700@zmi.at> Date: Wed, 25 Feb 2009 19:48:08 +0800 Message-ID: <8f750b7c0902250348jfee8784k5065c14725555a12@mail.gmail.com> Subject: Re: "like" and index From: Tony Liao To: Michael Monnerie Cc: pgsql-admin@postgresql.org Content-Type: multipart/alternative; boundary=000e0cd32a0e41cb9b0463bccf5b X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200902/209 X-Sequence-Number: 31430 --000e0cd32a0e41cb9b0463bccf5b Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/2/25 Michael Monnerie > On Mittwoch 25 Februar 2009 Tony Liao wrote: > > 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||'%' > > SELECT id FROM table_A WHERE prefix LIKE 'johnsmith%'; * in my table,the values of prefix are ('john','tom','anne','jim'......),so I don't think my SELECT was wrong.* by the way,my postgres-server version is 8.36.thanks > > > > ,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. > > Because your SELECT was wrong. > > > 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. > > thanks > > ps:I have another table table_B would use table_B.prefix= > > table_A.prefix.so how can I create the index? > > That's a foreign key, if I understand you correctly. > > ALTER TABLE ONLY B > ADD CONSTRAINT B_prefix_fkey FOREIGN KEY (prefix) REFERENCES > A(prefix) ON UPDATE CASCADE ON DELETE CASCADE; > > mfg zmi > -- > // Michael Monnerie, Ing.BSc ----- http://it-management.at > // Tel: 0660 / 415 65 31 .network.your.ideas. > // PGP Key: "curl -s http://zmi.at/zmi.asc | gpg --import" > // Fingerprint: AC19 F9D5 36ED CD8A EF38 500E CE14 91F7 1C12 09B4 > // Keyserver: wwwkeys.eu.pgp.net Key-ID: 1C1209B4 > > > -- > Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-admin > --000e0cd32a0e41cb9b0463bccf5b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

2009/2/25 Michael Monn= erie <michael.monnerie@is.it-management.at>
On Mittwoch 25 Februar 2009 Tony Liao wrote:
> hi all,
> =A0 =A0 =A0 I have a table table_A (id serial,prefix varchar),for exam= ple.
> =A0 =A0 =A0 now I want to get the id of "johnsmith"'s pr= efix match
> table_A.prefix,so I do select id from table_A where 'johnsmith'= ; like
> prefix||'%'

SELECT id FROM table_A WHERE prefix LIKE 'johnsmith%';
=A0 =A0 =A0 in my tabl= e,the values of prefix are ('john','tom','anne',= 9;jim'......),so I don't think my SELECT was wrong.
=A0=A0=A0=A0=A0=A0 by the way,my pos= tgres-server version is 8.36.thanks


> ,the table_A is very large so I would like to make
> index. create table_A_index on table_A(prefix)
> =A0 =A0 =A0 I try to explain analyze,but it doesn't work ,it use s= eq scan.

Because your SELECT was wrong.

> =A0 =A0 =A0 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.
> =A0 =A0 =A0 thanks
> =A0 =A0 =A0 =A0ps:I have another table table_B would use table_B.prefi= x=3D
> table_A.prefix.= so how can I create the index?

That's a foreign key, if I understand you correctly.

ALTER TABLE ONLY B
=A0 =A0ADD CONSTRAINT B_prefix_fkey FOREIGN KEY (prefix) REFERENCES
A(prefix) ON UPDATE CASCADE ON DELETE CASCADE;

mfg zmi
--
// Michael Monnerie, Ing.BSc =A0 =A0----- =A0 =A0 =A0http://it-management.at
// Tel: 0660 / 415 65 31 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0.networ= k.your.ideas.
// PGP Key: =A0 =A0 =A0 =A0 "curl -s http://zmi.at/zmi.asc | gpg --import"
// Fingerprint: AC19 F9D5 36ED CD8A EF38 =A0500E CE14 91F7 1C12 09B4
// Keyserver: wwwke= ys.eu.pgp.net =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Key-ID: 1C1209B4


--
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin

--000e0cd32a0e41cb9b0463bccf5b--