简体   繁体   中英

DataBind() on Gridview slow

I have an application using a gridview that is really slow.

After adding the Trace=true for the page, I tracked down where time is spent: when calling BindData() on a GridView. The GridView is connected to a LinqDataSource .

Here is the trace output:

GetContact        0.955485090710761        0.000339
  DataBind        0.97438854173692         0.018903
  PopulateStates  6.58986882347249         5.615480

Here is the sample asp.net page

<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">

In Code Behind:

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

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

The LINQ Data Source code looks like:

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

The SQL generated from this property is a canonical SELECT <all fields> FROM loc_access_contact .

And the table loc_access_contact looks like :

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]

The table contains about 287 contacts.

How come the DataBind is so slow ? How can I track further down what is happening ?

If you are using SQL Server - I would put a trace on the application then run again. Then I would pick out the actual SQL that is been run. Run that directly on the database then see if you can spot bottlenecks.

It looks as if you are bringing back all the data in loc_access_contacts so it could be the sheer volume of data.

Another possibly is the sorting. You are sorting by organisation, last_name, first_name. I would be tempted to put a non-clustered index on these columns to help with the sort efficient. Do check though that this table has a sensible clustered index ie primary key.

Of course I'm assuming here that you have control over your DB which I know a lot of people don't. In that case (or honestly in every case) use paging in your grid. If you can use some kind of sensible server side paging (ie by using Skip and Take LINQ operators) this will greatly improve your bind efficient. They will be simply a lot less data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM