samedi 15 août 2015

c# SQLite code optimization

I am a hobbyist programmer and writing a reporting tool to export values from an SQLlite database to excel. The excel part is written and working, the data i am retrieving from SQLite is creating a block in the program and taking several minutes to process. I have rewritten the code out using generic values to help illustrate the processes. The initial populateList module is taking a negligible amount of time, but I have included it below as that is the providing the data for the doStuff module, the populateList currently retrieves approximately 500 distinct records. I need the program to iterate through all the values retrieved by the populateList and do several counts. It is then populating another list 'valuesCount' with the counted values. I tried to improve the speed by looping through the list without closing the SQLite connection, but the improvement wasnt enough. Is there a more efficient way to retrieve this information from the databse? Thank you

public list<string>populateList()
{
List<string>values = new list<string>();
using (SQLiteConnection con = new SQLiteConnection(Passer.connstr))
{
    con.Open();
    string distinctValues = "SELECT DISTINCT \"value list\" FROM valueTable order by \"value list\" ";
    using (SQLiteCommand cmd = new SQLiteCommand(distinctValues, con))
    {
        SQLiteDataReader sqReader;
        sqReader = cmd.ExecuteReader();
        while (sqReader.Read())
        {
            values.Add(sqReader["value list"].ToString());
        }
    }
}
return values;
}




public void doStuff()
{
bool blanks = false;
string singleValue = string.Empty
string query = string.Empty;

List<string> getInitialValues = populateList();
list<string> valuesCount = new list<string>();

using (SQLiteConnection con = new SQLiteConnection(Passer.connstr))
{
    con.Open();
    for(int i = 0; i < getInitialValues.Count; i++)
    {
        blanks = false;
        singleValue = getInitialValues[i];
        if(singlevalue == "")
        {
            singleValue = \"\";
            blanks = true;
        }
        for (int x = 0; x < 6; x++)
        {
            string statement = string.Empty;
            switch(x)
            {
                case 0:
                    statement = "SELECT COUNT(*) from valueTable where \"column1\" = ";
                    break;
                case 1:
                    statement = "SELECT COUNT(*) from valueTable where \"column2\" = \"condition 1\" and \"column1\" = ";
                    break;
                case 2:
                    statement = "SELECT COUNT(*) from valueTable where \"column3\" = \"condition 3\" and \"column1\" = ";
                    break;
                case 3:
                    statement = "SELECT COUNT(*) from valueTable where \"column4\" = \"condition 4\" and \"column1\" = ";
                    break;
                case 4:
                    statement = "SELECT COUNT(*) from valueTable where \"column5\" = \"condition 5\" and \"column1\" = ";
                    break;
                case 5:
                    statement = "SELECT COUNT(*) from valueTable where \"column6\" = \"condition 6\" and \"column1\" = ";
                    break;                      
            }
            if (blanks == true)
            {
                query = System.String.Format("{0}{1}", statement, singleValue);
            }
            else 
            {
                query = System.string.format("{0}\"{1}\"", statement, singleValue);
            }
            using (SQLiteCommand cmd = new SQLiteCommand(query, con))
            {
                string countValues = cmd.ExecuteScalar().ToString();
                valuesCount.Add(countValues);
            }
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire