I have a datagrid (WPF/C#), that's pulling in two fields from a rather large MySQL view. However, it takes far too long to render onto the screen and I wanted to know if there is a quicker way?

private void SetupDataGrid()
{
    try
    {
        _con.Open();
        var com = new MySqlCommand("SELECT `Record ID`, `Company Name` FROM tblTest.all;") { Connection = _con, CommandType = CommandType.Text };
        var dt = new DataTable();
        var sdt = new MySqlDataAdapter(com);
        sdt.Fill(dt);
        DataGridActivities.DataContext = dt;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    _con.Close();
}
有帮助吗?

解决方案

You could introduce paging into the grid, and only extract from the database the records which you are going to display. Another approach would be to extract data, cache it, and then access the cache - but in this case first time round would still be slow.

If extracting a large data set is the problem, I would consider only extracting the items for the current page.

With MySQL you can do this in the following approach

SELECT * FROM [TABLE] LIMIT 5,10;  

其他提示

Which part is slowing down? Have you tried to time the SQL query?

Never used MySQL this way, but usual optimization techniques are stored procedures and indexes.

Unless you really want to display all the records in the same page, you should consider adding pagination in your query.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top