Re: Creating an index-type for LIKE '%value%'

From: Steve Atkins <steve(at)blighty(dot)com>
To: Larry Rosenman <ler(at)lerctr(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Creating an index-type for LIKE '%value%'
Date: 2005-02-08 00:25:10
Message-ID: 20050208002510.GA21915@gp.word-to-the-wise.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Feb 07, 2005 at 03:16:51PM -0600, Larry Rosenman wrote:
> SELECT domain,message,'1' as truth FROM blacklist
> WHERE somedomain ~* '(?:.+\.|)' || domain || '\$')
>
> The somedomain is actually a constant passed in from Exim (it's the sender's
> righthand
> Side of an E-Mail address).
>
> I'm looking to see if the domain name is in my blacklist.
>
> I may just be SOL, but I figured I'd ask.
>
> The blacklist table is:
> exim=# \d blacklist
> Table "public.blacklist"
> Column | Type | Modifiers
> -------------+-----------------------------+--------------------------
> insert_when | timestamp(0) with time zone | default now()
> insert_who | text | default "current_user"()
> domain | text |
> message | text |
> Indexes:
> "blacklist_dom_idx" btree ("domain")
>
> exim=#
>
> And contains records like:
>
> exim=# select * from blacklist limit 1;
> insert_when | insert_who | domain | message
> ------------------------+------------+----------+---------------------------
> ------
> 2003-12-22 21:02:49-06 | ler | 008\.net | 127.0.0.1 MX, SPAMMER
> (008.net)

A functional btree index on reverse(domain) might get you what you're
looking for.

<digs in the Abacus source code...>

CREATE OR REPLACE FUNCTION reverse(text) RETURNS text AS '
DECLARE
original alias for $1;
reverse_str text;
i int4;
BEGIN
reverse_str = '''';
FOR i IN REVERSE LENGTH(original)..1 LOOP
reverse_str = reverse_str || substr(original,i,1);
END LOOP;
return reverse_str;
END;'
LANGUAGE 'plpgsql' IMMUTABLE;

Then do

CREATE INDEX foo_idx ON blacklist(reverse(domain));

SELECT * FROM blacklist WHERE reverse(domain) LIKE reverse(bar) || '%';

Cheers,
Steve

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-02-08 01:09:41 Re: a SELECT FOR UPDATE question
Previous Message Stephan Szabo 2005-02-07 23:48:57 Re: Sorting when "*" is the initial character