2012-03-05 99 views
3

我有一个应用程序使用非常慢的gridview。GridView上的DataBind()慢

在为页面添加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怎么这么慢?我怎样才能进一步追踪发生了什么?

+0

也许源文件中的某些属性评估速度慢? – gabba 2012-03-05 13:18:35

+0

我已经添加了一些关于源代码的信息,但是我仍然不明白它为什么如此缓慢?该表目前有287个条目。 – Pierre 2012-03-05 13:30:33

回答

1

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

它看起来好像你带回了loc_access_contacts中的所有数据,因此它可能是数据的绝对数量。

另一个可能是排序。您按组织,姓氏,名字排序。我会试图在这些列上放置一个非聚集索引来帮助提高排序效率。尽管如此,请检查该表是否具有合理的聚集索引即主键。

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

+0

使用SQL事件探查器查看在数据库端如何执行查询。 – PraveenVenu 2012-03-05 13:32:53

+0

绝对 - 对不起,我没有把它命名。使用SQL事件探查器 – 2012-03-05 13:45:31

+0

该查询是一个规范SELECT

。数据库中目前有287个条目。 – Pierre2012-03-05 14:58:15