Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 68CBB1337B51 for ; Mon, 14 Feb 2011 12:53:36 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 26320-10 for ; Mon, 14 Feb 2011 16:53:28 +0000 (UTC) X-Greylist: delayed 00:09:58.654144 by SQLgrey-1.7.6 Received: from m.wordtothewise.com (b.wordtothewise.com [208.187.80.136]) by mail.postgresql.org (Postfix) with ESMTP id 3174A1337B4F for ; Mon, 14 Feb 2011 12:53:28 -0400 (AST) Received: from platter.wordtothewise.com (184.wordtothewise.com [208.187.80.184]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: steve) by m.wordtothewise.com (Postfix) with ESMTPSA id ECE2B2DED3 for ; Mon, 14 Feb 2011 08:43:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wordtothewise.com; s=1.wttw; t=1297701807; bh=VEhN7nhkAjGj31DHtkIun58/zfN3vOlzX/Et1EHW/vU=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date: Content-Transfer-Encoding:Message-Id:References:To; b=lpu2/uDw8ik21Yj7EZgTlW68sZkhoGH4ti1081F4r0bdNADpe24EK1wVMCmin6BwL 7ZerSRlU2ye7/1RjOhu1tBUHiqFds7/OHw9eG61PSNmon+LnjhhjZ9uwjf3riXv/kE KRz6RPIt3GxvlfzEczEQVvee7HnJNibZTjfBQoxg= Content-Type: text/plain; charset=iso-8859-2 Mime-Version: 1.0 (Apple Message framework v1082) Subject: Re: How to boost performance of queries containing pattern matching characters From: Steve Atkins In-Reply-To: <003d01cbcc1e$91241fa0$b36c5ee0$@ang.com.pl> Date: Mon, 14 Feb 2011 08:43:24 -0800 Content-Transfer-Encoding: quoted-printable Message-Id: <6A5D2BA2-000E-438F-B726-AFE3DD90FFE6@blighty.com> References: <004601cbcc14$acfe99d0$06fbcd70$@com> <4D58D752.2000100@archonet.com> <004701cbcc18$c54c8980$4fe59c80$@com> <003801cbcc1a$37d2f7e0$a778e7a0$@ang.com.pl> <4D58DEA2.3040202@archonet.com> <003d01cbcc1e$91241fa0$b36c5ee0$@ang.com.pl> To: PostgreSQL Performance X-Mailer: Apple Mail (2.1082) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201102/386 X-Sequence-Number: 42524 On Feb 14, 2011, at 12:09 AM, Artur Zaj=B1c wrote: >> Looks like you've almost re-invented the trigram module: >> http://www.postgresql.org/docs/9.0/static/pgtrgm.html >=20 > I didn't know about this module. > Idea to use three letters strings and use Full Text Search is the = same, but > the rest is not. >=20 > Is the idea to use similarity for this problem is really correct? How = should > be query constructed to return really all records matching ILIKE = criteria? If what you really want is the ability to select email addresses based = on subdomain, you might want to do this instead: create email_domain_idx on mytable (reverse(lower(split_part(email, '@', = 2)))); Then you can do things like this ... delete from mytable where reverse(lower(split_part(email, '@', 2))) =3D = reverse('aol.com'); ... to delete all aol.com users or like this ... delete from mytable where reverse(lower(split_part(email, '@', 2))) like = reverse('%.aol.com'); ... to delete all email addresses that are in a subdomain of "aol.com". You need a reverse() function to do that. Here's one in plpgsql: CREATE OR REPLACE FUNCTION reverse(text) RETURNS text AS ' DECLARE original alias for $1; reverse_str text; i int4; BEGIN reverse_str =3D ''''; FOR i IN REVERSE LENGTH(original)..1 LOOP reverse_str =3D reverse_str || substr(original,i,1); END LOOP; return reverse_str; END;' LANGUAGE 'plpgsql' IMMUTABLE; (Normalizing the email address so that you store local part and domain = part separately is even better, but an index on the reverse of the = domain is still useful for working with subdomains). Cheers, Steve