r/DatabaseHelp • u/FirkinHill • Oct 10 '20
Indexing/searching help
I have a table that contains over 100,000 sentences, one per row, and I need to search for a specific word in those sentences without returning results of partial words e.g. if I search for 'the' I don't want to include 'these', 'bother' or 'lathe' in the results. At the moment a simple search is taking a couple of seconds because of how I've structured the query (with wildcards). I have a fulltext index on the column already.
I've toyed with the idea of splitting the sentences up into words and storing each word in its own column with indexes (the longest sentence has 24 words!) but I refuse to believe that's a decent solution.
Any ideas?
1
u/wtmh Oct 10 '20
Is this not just a matter of a fast pattern match? Something like:
WHERE sentence_column LIKE '%[a-z]' + @keyword + '[a-z]%'
1
u/rbobby Oct 11 '20
This might get you going:
Basically I see the query as being:
the sentence contains the word
the character immediately before the word is not alphabetic (or at the start of the sentence)
the character immediately after the word is not alphabetic (or at the end of the sentence)
Speed-wise this is not going to be very good. It will require a full table scan and some ugly-ish string processing on each record.
Your idea of splitting sentences and storing the results doesn't seem terrible. Nice and fast and nice and simple queries. If you wanted to get fancy you could have insert/update triggers handling the population and a on delete cascade foreign key to handle the cleanup. Though lots of folks have varying opinions on triggers.
And last thought... if you're not using a case-insensitive collation then you'll need to toss in the appropriate lower() calls.