Re: Matching on keyword or phrases within a field that is delimited with an "or" operator "|"

From: Jim Ostler <jowenostler(at)yahoo(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Matching on keyword or phrases within a field that is delimited with an "or" operator "|"
Date: 2012-03-13 16:49:34
Message-ID: 1331657374.36198.YahooMailNeo@web124901.mail.ne1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David,

Thanks for the tip on the Regular Expression, as well as the advice to use an example statement.

So, I played with the expression you gave me and that works well. The question I now have is if I am trying to select all data for any row where that condition is true, is it possible to index the column I am selecting on to help with this expression? Given the table I am dealing with is over 20 GB, speed is a concern.

For example, If I wanted to select all rows from table_1 where ‘a’ existed in column ‘diagnosis’ and where two potential fields of diagnosis could be ‘a|b|c’ or  ‘d|b|f’  I could use the statement

SELECT  * FROM table_1
Where 'a' = ANY(regexp_split_to_array('diagnosis’, '\|')); 

Is there any way to index the column ‘diagnosis’ to speed this up?  I have tried to find information on using a Regular Expression in an index, but that doesn’t seem to work. 

One option I am playing with but stuck on is creating a new field with the array created by the Regular Expression. Will this work, or will I have to create a column for each element of the array? If I were to do this could I use a multiple column index? It would then be an index of 20 columns The postgres documentation states it is usually not efficient to have more than 3 columns, so I am not sure if that is a good idea or not. In general what I have read about multicolumn indexes leads me to believe that is not a good idea.

Also, given there is not a standard set of keywords in the column 'diagnosis' I don't think a GIN index would work.

Any ideas on other options I could try?
 
--Jim 

________________________________
From: David Johnston <polobo(at)yahoo(dot)com>
To: 'Jim Ostler' <jowenostler(at)yahoo(dot)com>; pgsql-general(at)postgresql(dot)org
Sent: Monday, March 12, 2012 5:27 PM
Subject: RE: [GENERAL] Matching on keyword or phrases within a field that is delimited with an "or" operator "|"

>> From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Jim Ostler
>> Sent: Monday, March 12, 2012 6:57 PM
>> To: pgsql-general(at)postgresql(dot)org
>> Subject: [GENERAL] Matching on keyword or phrases within a field that is
delimited with an "or" operator "|"
>>
>> I have a table that is around 20 GB, so I need to optimize as best as
possible the matching with another table on keywords across multiple fields.
I have around 10 fields that have keywords or phrases delimited with the
"or"
>> operator  "|". So it would be in the form of  "a | b  |  and jack  | cd"
. There are around 20 keywords or phrases per field, and these keywords
could be any word. 
>>
>> Because of the size of the database suing a "like" match would take too
long. I am not sure if tsvector would work, or if there is a way to indicate
how you want it parsed? 
>>
>> If I could index these fields somehow that would be best, but I don't
want to do the traditional full text indexing as I only want
to match whatever is between the " | " whether it is one word or more.
>>
>> The original use of this was as it appears, to have the field "a  |  b 
|  c" be read "a or b or c" etc. If there is a way to match using this type
of logic with an index that would be great. 
>>
>> I hope this is clear enough. Thanks for any help as I am fairly new at
this so any direction would be helpful.
>>
>> --Jim

=================================================================

Start with this:

SELECT 'a' = ANY(regexp_split_to_array('a|b|c', '\|'));  -- In this query
the "ANY" is providing the OR capability; use "ALL" for AND

and adapt as needed.

Regular Expressions are friendly creatures - everybody should have at least
one.

Given the lack of an example, but functioning, query that currently does
what you want it is hard to provide suggestions on improvements.  Whether
the above even is useful for you I do not know due to the lack of details.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Samba 2012-03-13 18:58:59 Re: Call for Google Summer of Code (GSoC) 2012: Project ideas?
Previous Message Carson Gross 2012-03-13 16:43:16 Re: Upgrade questions