pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Artur Zając <azajac@ang.com.pl>
To: gnanam@zoniac.com
To: 'Richard Huxton' <dev@archonet.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: How to boost performance of queries containing pattern matching characters
Date: Mon, 14 Feb 2011 08:38:48 +0100
Message-ID: <003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl> (raw)
In-Reply-To: <004701cbcc18$c54c8980$4fe59c80$@com>
References: <004601cbcc14$acfe99d0$06fbcd70$@com>
	<4D58D752.2000100@archonet.com>
	<004701cbcc18$c54c8980$4fe59c80$@com>

>How can we boost performance of queries containing pattern matching 
>characters?  In my case, we're using a percent sign (%) that matches any
string of  zero or more characters.
>
> QUERY:  DELETE FROM MYTABLE WHERE EMAIL ILIKE '%domain.com%'
>
> EMAIL column is VARCHAR(256).
> 
> As it is clear from the above query, email is matched "partially and
case-insensitively", which my application requirement demands.  
> 
> In case, if it were a full match, I could easily define a functional 
> INDEX on EMAIL column (lower(EMAIL)) and I could rewrite my DELETE where
criteria like lower(EMAIL) = 'someemail@domain.com'.
> 
> MYTABLE currently contains 2 million records and grows consistently.

I had almost the same problem.
To resolve it, I created my own text search parser (myftscfg) which divides
text in column into three letters parts, for example:

someemail@domain.com is divided to som, ome,mee,eem,ema,mai,ail,il@,
l@d,@do,dom,oma,mai,ain,in.,n.c,.co,com

There should be also index on email column:

CREATE INDEX "email _fts" on mytable using gin
(to_tsvector('myftscfg'::regconfig, email))

Every query like email ilike '%domain.com%' should be rewrited to:

WHERE
to_tsvector('myftscfg',email) @@ to_tsquery('dom') AND
to_tsvector('myftscfg',email) @@ to_tsquery('oma') AND
to_tsvector('myftscfg',email) @@ to_tsquery('mai') AND
to_tsvector('myftscfg',email) @@ to_tsquery('ain') AND
to_tsvector('myftscfg',email) @@ to_tsquery('in.') AND
to_tsvector('myftscfg',email) @@ to_tsquery('n.c') AND
to_tsvector('myftscfg',email) @@ to_tsquery('.co') AND
to_tsvector('myftscfg',email) @@ to_tsquery('com') AND email ILIKE
'%domain.com%';

Index is reducing number of records and clause email ILIKE '%domain.com%' is
selecting only valid records.

I didn't found better solution.

-------------------------------------------
Artur Zajac




view thread (13+ messages)  latest in thread

Message-ID: <003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl>
Permalink:  ../003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl/
Also on:    postgresql.org/message-id/003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: azajac@ang.com.pl, gnanam@zoniac.com, dev@archonet.com
  Subject: Re: How to boost performance of queries containing pattern matching characters
  In-Reply-To: <003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox