Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 68E3A632D8C for ; Wed, 25 Feb 2009 07:41:52 -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 16996-06 for ; Wed, 25 Feb 2009 07:41:44 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailsrv1.zmi.at (mailsrv1.zmi.at [212.69.162.198]) by mail.postgresql.org (Postfix) with ESMTP id 6BA6F6324E5 for ; Wed, 25 Feb 2009 07:41:43 -0400 (AST) Received: from mailsrv2.i.zmi.at (h081217054243.dyn.cm.kabsi.at [81.217.54.243]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client CN "mailsrv2.i.zmi.at", Issuer "power4u.zmi.at" (not verified)) by mailsrv1.zmi.at (Postfix) with ESMTP id 94E6142BA for ; Wed, 25 Feb 2009 12:41:40 +0100 (CET) Received: from saturn.localnet (saturn.i.zmi.at [10.0.0.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mailsrv2.i.zmi.at (Postfix) with ESMTPSA id 1939C400174 for ; Wed, 25 Feb 2009 12:41:41 +0100 (CET) From: Michael Monnerie Organization: it-management http://it-management.at To: pgsql-admin@postgresql.org Subject: Re: "like" and index Date: Wed, 25 Feb 2009 12:41:40 +0100 User-Agent: KMail/1.10.3 (Linux/2.6.27.13-ZMI; KDE/4.1.3; x86_64; ; ) References: <8f750b7c0902250259w6065515as350aca3b5d7d8173@mail.gmail.com> In-Reply-To: <8f750b7c0902250259w6065515as350aca3b5d7d8173@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200902251241.40700@zmi.at> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200902/208 X-Sequence-Number: 31429 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%'; > ,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