I need to load data from SQLite to memory, and when the table contains more than 40k entries this process lasts for a few minutes. Data in db is encrypted so I decrypt it to load in memory.
Basically, I am:
- loading all the data from the table
- while it is reading I decrypt the info and add it to a dictionary
The code:
internal static void LoadInfo(Dictionary<int, InfoMemory> dic)
{
using (SQLiteConnection con = new SQLiteConnection(conString))
{
using (SQLiteCommand cmd = con.CreateCommand())
{
cmd.CommandText = String.Format("SELECT ID, Version, Code FROM Employee;");
int ID, version, code;
try
{
con.Open();
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
try { ID = Convert.ToInt32(Decrypt(rdr[0].ToString())); }
catch { ID = -1; }
try { version = Convert.ToInt32(Decrypt(rdr[1].ToString())); }
catch { version = -1; }
try { code = Convert.ToInt32(Decrypt(rdr[2].ToString())); }
catch { code = -1; }
if (ID != -1)
{
if (!dic.ContainsKey(ID)) dic.Add(ID, new InfoMemory(version, code));
}
}
rdr.Close();
}
}
catch (Exception ex) { Log.Error(ex.Message); }
finally { con.Close(); GC.Collect(); }
}
}
}
How can I make this process faster? One way I try is by loading encrypted data to memory and decrypt when needed, but if I do that I consume more memory. Since I am working in mobile devices I want to maintain the memory consumption as low as possible.