X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.225.181]) by svr1.postgresql.org (Postfix) with ESMTP id ACA5DCC33B for ; Tue, 15 Jul 2003 18:31:05 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr5.postgresql.org [64.117.225.181]) (amavisd-new, port 10024) with ESMTP id 59941-10 for ; Tue, 15 Jul 2003 18:31:23 +0000 (GMT) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 7A9A7CC28B for ; Tue, 15 Jul 2003 15:30:53 -0300 (ADT) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.0.6) with ESMTP-TLS id 1824894; Tue, 15 Jul 2003 10:57:52 -0700 Message-ID: <3F144838.8070209@joeconway.com> Date: Tue, 15 Jul 2003 11:30:16 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 To: David Olbersen Cc: pgsql-sql@postgresql.org Subject: Re: Functional Indexes References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/193 X-Sequence-Number: 14307 David Olbersen wrote: > Now the question: is there a single index I can create that will be > used when my WHERE clause contains either urlhost or urltld? I could > create two functional indexes, but that seems a bit silly to me. > I can't think of how to do only one index in 7.3.x and earlier, but FWIW, this works in 7.4devel (which should be in beta next Monday): create or replace function tld(text) returns text as ' select split_part(split_part(substr($1,8,length($1)),''/'',1),''.'',3) ' language 'sql' STRICT IMMUTABLE; regression=# select tld('http://www.foobar.com/really/long/path/to/a/file'); tld ----- com (1 row) create or replace function sld(text) returns text as ' select split_part(split_part(substr($1,8,length($1)),''/'',1),''.'',2) ' language 'sql' STRICT IMMUTABLE; regression=# select sld('http://www.foobar.com/really/long/path/to/a/file'); sld -------- foobar (1 row) create table urls(f1 text); insert into urls values('http://www.foobar.com/really/long/path/to/a/file'); create index urls_idx1 on urls(tld(f1),sld(f1)); -- just to see index usage on toy table set enable_seqscan to off; regression=# explain analyze select * from urls where tld(f1) = 'com'; QUERY PLAN ---------------------------------------------------------------------- Index Scan using urls_idx1 on urls (cost=0.00..4.69 rows=1 width=32) (actual time=0.07..0.07 rows=1 loops=1) Index Cond: (split_part(split_part(substr(f1, 8, length(f1)), '/'::text, 1), '.'::text, 3) = 'com'::text) Total runtime: 0.18 msec (3 rows) regression=# explain analyze select * from urls where tld(f1) = 'com' and sld(f1) = 'foobar'; QUERY PLAN --------------------------------------------------------------------- Index Scan using urls_idx1 on urls (cost=0.00..4.70 rows=1 width=32) (actual time=0.08..0.09 rows=1 loops=1) Index Cond: ((split_part(split_part(substr(f1, 8, length(f1)), '/'::text, 1), '.'::text, 3) = 'com'::text) AND (split_part(split_part(substr(f1, 8, length(f1)), '/'::text, 1), '.'::text, 2) = 'foobar'::text)) Total runtime: 0.21 msec (3 rows) Joe