I start by saying that I'm a beginner with C# and SQL, so sorry if my code contains some rubbish.
I've a sqlite table which is a Japanese Dictionary.
It has the following rows:
ID: an unique, autoincremental integer;
word: the word in Japanese "ideograms" (es.: 元気);
reading: the phonetic reading of the word (es.: げんき);
definition: the dictionary definition for the word;
A word may appear more than once inside the dictionary.
Sometimes there may be multiple entries where both "word" and "reding" are the same, and I need to retrieve all of them:
ID - word - reading - definition
1 - 愛 - あい - Love;
2 - 愛 - あい - The title of a 1800 book by ... ;
This is how I retrieve the result:
void Search(string _word, string _reading) //es.: Search(愛, あい);
{
...
using(var conn = new SQLiteConnection("Data Source=dictsdb.sqlite;Version=3;"))
{
conn.Open();
string sql = "SELECT * FROM dictionary WHERE word = '" + _word + "' AND reading = '" + _reading + "'";
using(var command = new SQLiteCommand(sql, conn))
{
using(var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while(reader.Read())
{
(...)
}
}
}
conn.Close();
}
...
}
The dictionary is not sorted in a particular way, so I can not use optimized search algorithms.
I can not sort the dictionary either because I need it in its actual order.
The slow part is the command.ExecuteReader()
.
I suppose because for each search it must check all the entries of the dictionary, which is huge.
Any suggestion to make it faster?
EDIT: Thank you all for your suggestions! The index trick solved the issue and now is teen times faster! Thank to the other suggestions too, like the parametrized input one!