I have a database with two columns that is called using the following query:
SELECT TimesUsed, Word FROM Predictions WHERE (Word LIKE @partialWord) ORDER BY TimesUsed DESC
In my application it is taking a ridiculous amount of time to call this statement
PredTable = PredTA.GetDataByPartialWord(combo)
If PredTable.Count = 1 Then PredTA.Update(PredTable(0).TimesUsed + 1, combo)
If PredTable.Count = 0 Then PredTA.Insert(combo, 1)
Indexing does not help because it is only a two column database and is constantly being updated
Is there any other way that I can improve the speed of this statement?
Best Answer
Thanks for updating that you are using Microsoft SQL Server, which does support Full Text Indexing. (As do many other versions of SQL servers.)
First of all your query is using a
WHERE (Word LIKE @partialWord)
which means that if there is a prefix wildcard (e.g.'%tion'
) the whole table has to be scanned and the contents of theWord
column likewise scanned.That is the base cost of this query.
Therefore, you need to investigate some other method of finding the partial words. For example, full text indexing (if available to you) may give you other options to use when finding %partial% words.
Create a full text index on the
Predictions
table for theWord
column. This can be done through SSMS or by a script. This will populate the keywords table in the FTI catalog tables. Example from: http://msdn.microsoft.com/en-us/library/ms187317.aspxThen here is a sample script that would help you to then find all the rows with wildcards.
By building a full text query string that includes all strings containing the pattern joined by the OR operator, it allows Full Text Search to then directly access the rows that contain one (or more) of the words. This should be a lot faster than scanning the table.