Re: Functional Indexes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David Olbersen" <DOlbersen(at)stbernard(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Functional Indexes
Date: 2003-07-15 18:12:29
Message-ID: 4245.1058292749@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"David Olbersen" <DOlbersen(at)stbernard(dot)com> writes:
> I have a function (urlhost) which finds the 'host' portion of a URL. In the case of http://www.foobar.com/really/long/path/to/a/file it returns "www.foobar.com".
> I also have a function (urltld) which returns the TLD of a URL. In the case of http://www.foobar.com/really/long/path/to/a/file it returns ".com" (the leading dot is OK).
> urltld uses urlhost to do it's job (how should be apparent).

> 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 do not see any way with functions declared like that. Quite aside
from implementation limitations, the portion of the 'host' string that
urltld is interested in would be the low-order part of the indexed
strings, and you can't usefully use an index to search for low-order
digits of the key.

Could you instead define an index over the reversed host name (eg,
com.foobar.www)? This would seem to provide about the same
functionality for searches on urlhost, and you could exploit the index
for TLD searching via prefixes. For example:

regression=# create table t1 (f1 text);
CREATE TABLE
regression=# create index t1i on t1 (lower(f1));
CREATE INDEX
regression=# explain select * from t1 where lower(f1) like 'com.%';
QUERY PLAN
----------------------------------------------------------------------------
Index Scan using t1i on t1 (cost=0.00..17.08 rows=5 width=32)
Index Cond: ((lower(f1) >= 'com.'::text) AND (lower(f1) < 'com/'::text))
Filter: (lower(f1) ~~ 'com.%'::text)
(3 rows)

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joe Conway 2003-07-15 18:30:16 Re: Functional Indexes
Previous Message Frank Bax 2003-07-15 18:06:06 Re: Functional Indexes