【问题标题】:Databind() in GridView VERY slow!! VB.NETGridView 中的 Databind() 非常慢!! VB.NET
【发布时间】:2023-03-19 08:58:01
【问题描述】:

我的代码遇到了一个可怕的问题。当我执行 DataBind 方法时,页面加载并加载,几乎 5 分钟后 GridView 被填充。这不是 SQL 查询问题!

我使用 Visual Studio 调试器进行了测试,代码在 DataBind() 上停止

Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        /* This is not the correct code but at the end, the result will be same as below */

        Dim feedback As String = "positive"
        Dim date As String = "2013"

        bindDataShowDetails(feedback, date) // <----- I call this method ( below )

  End Sub
Protected Sub bindDataShowDetails(ByVal feedback As String, ByVal Data As String())

        feedbackGlobal = feedback

        Dim strSql As String = ""

        strSql = " select "
        strSql += " feedback, zendesk_ticket_id,feedback_text as Comment, date_ins as Ticket_date, date_feedback as Feedback_date, comment_review, review_status "
        strSql += "  from feedbacks_support "
        strSql += " where "
        strSql += " feedback = '" & feedback & "'" // <---- 'positive'
        strSql += " and YEAR(date_feedback) = " & Date // <---- '2013'


        Dim myreader As SqlDataReader = admin2.ExecReader(strSql) // <--- Class 'admin2' calls the method (ExecReader) thats executes the SQL query and return the result.

        GridView1.DataSource = myreader
        GridView1.DataBind() <----------- Problem is here!!!

        Me.ModalPopupExtender1.Show()


    End Sub

我在 SQL Server 中直接运行了 SQL 查询,它运行良好!

我真的不知道怎么了! 非常感谢您的支持!

【问题讨论】:

  • 查询返回多少条记录?
  • 使用参数来避免 SQL 注入,即使是很小的事情。我会避免使用“日期”这个词作为变量。参数虽然是“数据”,而不是“日期”。
  • @YuriyGalanter 2445 条记录
  • @RodrigoAbib 一次显示超过 2000 条记录并不是一个好主意,分页可能是更好的选择。同时查看将数据检索到 DataTable 是否会产生更好的性能(并且您可以缓存该数据表以进行分页)

标签: vb.net performance gridview data-binding


【解决方案1】:

一次显示数千条记录不是一个好主意 - 这可能是性能问题的主要原因。缓解这种情况的一种方法是将数据检索到 DataTable 中,缓存该数据表并在分页中使用它来显示,例如每页 50 条记录。 (这样您就不必为每个页面更改/重新绑定都打数据库)。

即使这种方法也适用于有限数量的记录,如果该数量变得巨大 - 即使将所有记录检索到内存 DataTable 中也不是一种选择,您将不得不实现服务器端分页以仅检索一部分数据一次来自数据库。

也就是说,当从 .NET 代码调用查询而不是在 SSMS 中直接执行时,另一个常见的减速原因可能是参数嗅探。如果 SQL Server 构建和缓存的执行计划对于当前查询执行不是最佳的 - 它可能真的会减慢它的速度。尝试在构建 SQL 语句的代码末尾添加以下行:

strSql += " OPTION (RECOMPILE) ";

缓解这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多