Relative performance of prefix and suffix string matching

From: Andrew Rose <andrew(dot)rose(at)metaswitch(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Relative performance of prefix and suffix string matching
Date: 2011-09-23 09:47:15
Message-ID: 632BF61B4D0DEC4AAD03109304ADD5761912F6BF@ENFIRHMBX1.datcon.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Basic Question: In text fields, is prefix matching significantly faster than suffix matching?

Background:

I'm designing a database schema where a common operation will be "search for substring x either at the beginning or end of column 'str'".

1. I could have the client issue...

SELECT * FROM tbl WHERE str LIKE 'x%' OR str LIKE '%x'

2. Alternatively, I could store column 'rev_str' as a reversed version of column 'str' and have the client produce a reversed version of x on each query (call it r). Then the client would issue...

SELECT * FROM tbl WHERE str LIKE 'x%' OR rev_str LIKE 'r%'

...which would use prefix matches only instead of requiring suffix matches. Since I've seen this form used by others, I was wondering if it's necessary - i.e. if databases really do perform prefix matching faster?

3. Is there a solution I'm unaware of with even better performance?

Thanks,

Andrew

Responses

Browse pgsql-general by date

  From Date Subject
Next Message c k 2011-09-23 10:13:28 Re: [GENERAL] Date time value error in Ms Access using pass through queries
Previous Message hamann.w 2011-09-23 07:45:51 Re: looking for a faster way to do that