Search code examples
c#sqlperformancesql-manager

C# SQL query is lagging


I developed an application which is manipulating data from a database. When I run the app from my PC, or PCs near me (in my company, same server) it's working properly, but when someone from the same company but another server (another Country) is trying to do the same, he has tremendous LAG.

I am aware that the query it's a bit "to much":

SqlCommand command = new SqlCommand("SELECT * FROM dbo.Person", con)

More data -> more Lag

But I kinda need all the data from the database to populate a ListView then export to an Excel document.

Things I've tried:

I created a background worker that will execute this command and when the worker completes I populate the ListView with the data like this:

private void ViewAllWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)    
{   
    while (Oreader.Read())
    {

        ListViewItem item = new ListViewItem(Oreader["ID"].ToString());

        item.SubItems.Add(Oreader["DecadeOfBirth"].ToString());
        // etc
    }
}

What would you do in this case? What could help reduce the lag? what is the right approach?


Solution

  • The only proper way to fix this is to reduce amount of data you bring back to the user.

    Let me give you an example:

    • Your table contains lots of rows which you need to export
    • A user wants to preview data and then export it to Excel
    • Allow user to see partial data
      • Top N records
      • Filters (by user name, department, date, etc)
      • Pagination (skip, take)
    • User is satisfied with what she sees, and then wants to export all the data
    • You run this on the server, and just give the user some progress feedback