Unplanned
Last Updated: 28 Feb 2023 09:21 by Valentin
Valentin
Created on: 28 Feb 2023 09:21
Category: Grid
Type: Feature Request
3
Optimize the Grid to call COUNT just once

We found out during SQL profiling that when bound to an IQueryable (linked to DbContext) with pagination enabled, the Telerik data grid called SQL to count elements twice. This is not a pre-render problem as those deplicated queries are actually executed twice (one pair for each render).

Our simplified code:

<TelerikGrid TItem="Item"
             Data="_data"
             PageSize="10"
             Pageable="true"
             Sortable="true">
    <GridColumns>
        <GridColumn Field="@nameof(Item.Id)" />
        <GridColumn Field="@nameof(Item.Code)" />
    </GridColumns>
</TelerikGrid>

@code {
    [Inject] protected DbContext DbContext { get; set; }

    private IEnumerable<Item> _data;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        _data = DbContext.Items
            .Select(x => new Item()
            {
                Id = x.Id,
                Code = x.Code
            });
    }
}

===

ADMIN EDIT:

There are two possible ways to avoid the double COUNT:

  • Enumerate the collection before binding the Grid. For example, call ToList().
  • Bind the Grid via OnRead event. This gives full control over the data binding process and possible optimizations.
0 comments