我们有一个包含大量数据的表(数百万行)。我们使用存储过程来获取记录并使用mvc方法将该数据与kendo网格绑定。但我们想从表中只获取所需的行,而不是一次性获取整个数据集,然后对其进行过滤,排序,分页选项。
是否可以一次只获取一页行?请帮助并建议一些方法。
答案 0 :(得分:1)
首先,您的存储过程中应该有一些分页逻辑。您可以查看此博客文章以获取建议:https://sqlperformance.com/2015/01/t-sql-queries/pagination-with-offset-fetch
然后,在您的视图中,设置网格的数据源以在控制器中调用自定义操作。确保启用分页。像这样:
.Pageable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("CustomAjaxBinding_Read", "Grid"))
)
最后在您的控制器中创建操作并使其期望DataSourceRequest参数。该参数将从网格接收一个对象,其中包括页面大小和当前页码。您可以使用它们将它们传递给存储过程,并使其仅获取该页面的数据。它将是这样的:
public ActionResult CustomAjaxBinding_Read([DataSourceRequest] DataSourceRequest request)
{
// Get the page number and size, which the grid is requesting
var pageNumber = request.Page;
var pageSize = request.PageSize;
// Call your stored procedure here
// ...
// Now return the data to the grid, formatted as JSON
return Json(result);
}
您可以在Telerik的网站上看到ajax数据源的示例:查看http://demos.telerik.com/aspnet-mvc/grid/customajaxbinding。
答案 1 :(得分:0)
我刚刚这样做了(注意:我使用的是Dapper和EF)
var gridBinder = new GridBinder(request);
var filters = gridBinder.GetFilterDescriptor();
var sorting = gridBinder.SortInfo.Member.HasValue() ? string.Format("{0} {1}", gridBinder.SortInfo.Member, gridBinder.SortInfo.Direction) : "";
var p = new DynamicParameters();
p.Add("@Page", gridBinder.PageNumber, DbType.Int32, ParameterDirection.Input);
p.Add("@PageSize", gridBinder.PageSize, DbType.Int32, ParameterDirection.Input);
p.Add("@Filter", filters);
p.Add("@SortOrder", sorting);
p.Add("@TotalRowCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
var data = _db.Connection().Query<IndexVm>("dbo.People_GetAll", p, commandType: System.Data.CommandType.StoredProcedure, transaction: _db.Database.CurrentTransaction.GetDbTransaction());
gridBinder.RecordCount = data.FirstOrDefault().TotalRowCount;
return Json(new DataSourceResult
{
Total = gridBinder.RecordCount,
Data = data
});
GridBinder
public class GridBinder
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
public int RecordCount { get; set; }
public SortInfo SortInfo { get; set; } = new SortInfo() { Direction = SortDirection.Asc, Member = string.Empty };
private readonly DataSourceRequest _command;
public GridBinder(DataSourceRequest command)
{
_command = command;
PageNumber = command.Page;
PageSize = command.PageSize;
GetSortDescriptor();
}
private void GetSortDescriptor()
{
foreach (SortDescriptor descriptor in _command.Sorts)
{
SortInfo.Member = descriptor.Member;
SortInfo.Direction = descriptor.SortDirection == ListSortDirection.Ascending ? SortDirection.Asc : SortDirection.Desc;
}
}
public string GetFilterDescriptor()
{
string filters = string.Empty;
foreach (IFilterDescriptor filter in _command.Filters)
{
filters += ApplyFilter(filter);
}
return filters;
}
private static string ApplyFilter(IFilterDescriptor filter)
{
var filters = "";
if (filter is CompositeFilterDescriptor)
{
filters += "(";
var compositeFilterDescriptor = (CompositeFilterDescriptor)filter;
foreach (IFilterDescriptor childFilter in compositeFilterDescriptor.FilterDescriptors)
{
filters += ApplyFilter(childFilter);
filters += " " + compositeFilterDescriptor.LogicalOperator.ToString() + " ";
}
}
else
{
string filterDescriptor = "{0} {1} {2}";
var descriptor = (FilterDescriptor)filter;
if (descriptor.Operator == FilterOperator.StartsWith)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'" + descriptor.Value + "%'");
}
else if (descriptor.Operator == FilterOperator.EndsWith)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'%" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.Contains)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'%" + descriptor.Value + "%'");
}
else if (descriptor.Operator == FilterOperator.DoesNotContain)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "NOT LIKE", "'%" + descriptor.Value + "%'");
}
else if (descriptor.Operator == FilterOperator.IsEqualTo)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "=", "'" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.IsNotEqualTo)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<>", "'" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.IsGreaterThan)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, ">", "'" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.IsGreaterThanOrEqualTo)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, ">=", "'" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.IsLessThan)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<", "'" + descriptor.Value + "'");
}
else if (descriptor.Operator == FilterOperator.IsLessThanOrEqualTo)
{
filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<=", "'" + descriptor.Value + "'");
}
filters = filterDescriptor;
}
filters = filters.EndsWith("And ") == true ? filters.Substring(0, filters.Length - 4) + ")" : filters;
filters = filters.EndsWith("Or ") == true ? filters.Substring(0, filters.Length - 4) + ")" : filters;
return filters;
}
}
public class SortInfo
{
public string Member { get; set; }
public SortDirection Direction { get; set; }
}
public enum SortDirection
{
Asc, Desc
}