Kendo MVC ToDataSourceResult极其缓慢,具有较大的IQueryable

Tim*_*ell 6 c# asp.net datasource asp.net-mvc-4 kendo-ui

我已经DataSourceResult ToDataSourceResult(this IQueryable enumerable, DataSourceRequest request);广泛使用了Kendo 扩展,直到现在查询一个包含4000万条记录的表时才发现性能问题.我写的10个查询作为基准,因为它与传入的请求相同.

这是我的阅读动作:

public ActionResult ReadAll([DataSourceRequest] DataSourceRequest 
{
    var startTimer = DateTime.Now;
    var context = Helpers.EFTools.GetCADataContext();
    Debug.WriteLine(string.Format("{0} : Got Context", DateTime.Now - startTimer));

    var events = from e in context.Events
                 select
                     new Models.Event()
                         {
                             Id = e.Id,
                             DateTime = e.EventDateTime,
                             HostId = e.Door.HostId,
                             SiteId = e.Door.Host.SiteId,
                             UserId = (int)e.UserId,
                             UserName = e.User.FirstName + " " + e.User.Surname,
                             DoorId = e.DoorId,
                             Door = e.Door.Name,
                             Description = e.Description,
                             SubDescription = e.SubDescription
                         };
    Debug.WriteLine(string.Format("{0} : Built Query", DateTime.Now - startTimer));

    var tenRecods = events.OrderByDescending(i => i.DateTime).Take(10).ToList();
    Debug.WriteLine(string.Format("{0} : Taken 10", DateTime.Now - startTimer));

    var result = events.ToDataSourceResult(request);
    Debug.WriteLine(string.Format("{0} : Datasource Result", DateTime.Now - startTimer));

    return this.Json(result);
}
Run Code Online (Sandbox Code Playgroud)

Debug的输出:

00:00:00.1316569 : Got Context
00:00:00.1332584 : Built Query
00:00:00.2407656 : Taken 10
00:00:21.5013946 : Datasource Result
Run Code Online (Sandbox Code Playgroud)

虽然有时查询超时.使用dbMonitor我捕获了两个查询,首先手册取10:

"Project1".id,
"Project1"."C1",
"Project1".hostid,
"Project1".siteid,
"Project1".userid,
"Project1"."C2",
"Project1".doorid,
"Project1"."name",
"Project1".description,
"Project1".subdescription
FROM ( SELECT 
    "Extent1".id,
    "Extent1".userid,
    "Extent1".description,
    "Extent1".subdescription,
    "Extent1".doorid,
    "Extent2"."name",
    "Extent2".hostid,
    "Extent3".siteid,
     CAST("Extent1".eventdatetime AS timestamp) AS "C1",
    "Extent4".firstname || ' ' || "Extent4".surname AS "C2"
    FROM    public.events AS "Extent1"
    INNER JOIN public.doors AS "Extent2" ON "Extent1".doorid = "Extent2".id
    INNER JOIN public.hosts AS "Extent3" ON "Extent2".hostid = "Extent3".id
    INNER JOIN public.users AS "Extent4" ON "Extent1".userid = "Extent4".id
)  AS "Project1"
ORDER BY "Project1"."C1" DESC
LIMIT 10
Run Code Online (Sandbox Code Playgroud)

和ToDataSourceRequest查询:

SELECT 
"GroupBy1"."A1" AS "C1"
FROM ( SELECT Count(1) AS "A1"
    FROM  public.events AS "Extent1"
    INNER JOIN public.doors AS "Extent2" ON "Extent1".doorid = "Extent2".id
)  AS "GroupBy1"
Run Code Online (Sandbox Code Playgroud)

这是DataSourceRequest request传入的参数:

request.Aggregates Count = 0
request.Filters Count = 0
request.Groups Count = 0
request.Page 1
request.PageSize 10
request.Sorts Count = 1
Run Code Online (Sandbox Code Playgroud)

这是结果 var result = events.ToDataSourceResult(request);

result.AggregateResults null
result.Data Count = 10
result.Errors null
result.Total 43642809
Run Code Online (Sandbox Code Playgroud)

如何以更高效,更快捷的方式DataSourceResult从事件IQueryable中获取DataSourceRequest

Tim*_*ell 4

在实现具有大量调试输出时间戳的自定义绑定(由Atanas Korchev建议)后,很明显导致性能问题的原因是总数。

看看我捕获的 SQL 支持这一点,不知道为什么我之前没有看到它。

快速获取总行数是另一个问题,但我会在这里发布我找到的任何答案。

  • 这篇文章太旧了,但是这个问题仍然存在......您对缓慢的“总计数”有什么解决方案吗? (2认同)