gpt4 book ai didi

c# - Gridview 上的 DataBind() 速度慢

转载 作者:行者123 更新时间:2023-11-30 18:00:58 40 4
gpt4 key购买 nike

我有一个使用 gridview 的应用程序,它非常慢。

添加 Trace=true 后对于页面,我追踪了时间花费的地方:在 GridView 上调用 BindData() 时。 GridView连接到 LinqDataSource .

这是跟踪输出:

GetContact        0.955485090710761        0.000339
DataBind 0.97438854173692 0.018903
PopulateStates 6.58986882347249 5.615480

这是示例 asp.net 页面

<asp:LinqDataSource ContextTypeName="DAL.BALDataContext" TableName="loc_access_contacts"
ID="_ldsContact" runat="server" OrderBy="organisation, last_name, first_name">
</asp:LinqDataSource>
<cc1:CustomGridView ID="gvContact" runat="server" DataSourceID="_ldsContact" AutoGenerateColumns="False" Width="100%"
DataKeyNames="person_id" ForeColor="#333333" GridLines="None" AllowPaging="False"
AllowSorting="True" BorderStyle="None" ShowFooter="false" CaptionAlign="Top"
PagerStyle-HorizontalAlign="Left" HeaderStayPolicy="NotStay" SessionKey="Contact.GridView.Columns.SortSettings"
SaveKey="ContactManagementSortSettings" OnRowCommand="gvContact_RowCommand" OnSorting="gvContact_Sorting">

在代码隐藏中:

Trace.Write("  DataBind");
gvContact.DataBind();

Trace.Write(" PopulateStates");
populateStates(contact);

LINQ 数据源代码如下所示:

public System.Data.Linq.Table<loc_access_contact> loc_access_contacts
{
get
{
return this.GetTable<loc_access_contact>();
}
}

从此属性生成的 SQL 是规范的 SELECT <all fields> FROM loc_access_contact .

和表loc_access_contact看起来像:

CREATE TABLE [dbo].[loc_access_contact](
[person_id] [int] IDENTITY(1,1) NOT NULL,
[organisation] [nvarchar](50) NULL,
[last_name] [nvarchar](50) NULL,
[first_name] [nvarchar](50) NULL,
[is_active] [tinyint] NULL,
[state_id] [char](2) NULL,
[postal_code] [nchar](4) NULL,
[town] [nvarchar](50) NOT NULL,
[phone_number] [nvarchar](20) NULL,
[mobile_number] [nvarchar](20) NULL,
[fax_number] [nvarchar](20) NULL,
[email_address] [nvarchar](255) NULL,
[street_name] [nvarchar](255) NULL,
[house_number] [nvarchar](20) NULL,
[postal_box] [nvarchar](20) NULL,
[language] [tinyint] NULL,
CONSTRAINT [PK_person] PRIMARY KEY CLUSTERED
(
[person_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

该表包含大约 287 个联系人。

为什么 DataBind 这么慢?我如何才能进一步追踪正在发生的事情?

最佳答案

如果您使用的是 SQL Server - 我会跟踪应用程序然后再次运行。然后我会挑选出实际运行的 SQL。直接在数据库上运行,然后看看是否可以发现瓶颈。

看起来您正在带回 loc_access_contacts 中的所有数据,所以它可能是绝对数据量。

另一个可能是排序。您正在按组织、姓氏、名字排序。我很想在这些列上放置一个非聚集索引以帮助提高排序效率。请检查此表是否具有合理的聚集索引,即主键。

当然,我在这里假设您可以控制您的数据库,我知道很多人都没有。在那种情况下(或者在任何情况下老实说)在你的网格中使用分页。如果您可以使用某种合理的服务器端分页(即通过使用 SkipTake LINQ 运算符),这将大大提高您的绑定(bind)效率。它们将只是少得多的数据。

关于c# - Gridview 上的 DataBind() 速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9567258/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com