I have a table in a postgres 8.1 db with information about approximately 370,000 customers. This table includes the fields sn (surname) and gn (given name). I would like to enable users to search for customers full names using the form or or simply . My first attempt to construct a query was like this:
SELECT sn || ', ' || gn as name from users
WHERE sn || ' ' || gn like '%Johnson David%'
or gn || ' ' || sn like '%Johnson David%'
This worked fine, but was quite slow, clocking in at 600/623 ms. In order to optimise, I created an index on the sn field only, as I guessed that the gn field would contain so much duplication as to be useless for indexing. Unfortunately, indexing surname didn't improve performance at all as the query didn't use the index.
Seq Scan on users (cost=0.00..18296.06 rows=1 width=64) (actual time=57.935..588.755 rows=8 loops=1)
My guess is that the reason for this is that described in this thread. I considered using a multicolumn index, but I guessed that it would mean that I could only search in one of the two styles I mention above, i.e. or but not both.
I have also considered creating a full text index, but it seems unsuitable for name values, as I would get a lot of stemming and so on that isn't relevant. Does anyone have any suggestions for indexing strategies? It seems like it should be quite a common use case.