简体   繁体   中英

Improve SQL query execution time

I have a dataGridView that displays data from a table, after I export these data as a xml file, I add the unique field to another table so I can display only non-exported data.

How I display only data that are not yet exported :

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    SqlCommand queryLocal = new SqlCommand("SELECT *uniqueField* FROM myTable 
WHERE *uniqueField* = " + dataGridView1.Rows[i].Cells[3].Value.ToString().Trim().Replace("'","''"), con);
    var reader = queryLocal.ExecuteReader();
    if (reader.Read())
    {
        dataGridView1.Rows.RemoveAt(i);
        i--;
    }
    reader.Close();
}

The problem is that it takes more than 20 seconds to filter less than 400 rows. How could I improve the performance here ?

You could try this:

SqlCommand queryLocal = new SqlCommand("SELECT DISTINCT *uniqueField* FROM myTable");
var reader = queryLocal.ExecuteReader();

List<string> uniqueFields = new List<string>();
while (reader.Read())
    uniqueFields.Add(reader[0]);
reader.Close();

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (uniqueFields.Contains(dataGridView1.Rows[i])
    {
        dataGridView1.Rows.RemoveAt(i);
        i--;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM