由于某种原因,当我使用kendo扩展ToDataSourceResult时,我的EF导入函数的运行速度比直接在SQL Server中慢很多倍。
我的API:
public HttpResponseMessage Get([ModelBinder(typeof(Kendo.Mvc.UI.WebAPI.DataSourceRequestModelBinder))] DataSourceRequest dataSourceRequest)
{
DataSourceResult result = orderAccess.Get(UserID).ToDataSourceResult(dataSourceRequest);
}
我的获取功能:
public IQueryable Get(Guid userID)
{
return db.fnOrdersGet(userID).AsQueryable();
}
fnOrdersGet已导入EF:
[DbFunction("EntityModel", "fnOrdersGet")]
public virtual IQueryable<fnOrdersResult> fnOrdersGet(Nullable<System.Guid> userID)
{
var userIDParameter = userID.HasValue ?
new ObjectParameter("UserID", userID) :
new ObjectParameter("UserID", typeof(System.Guid));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<fnOrdersResult>("[EntityModel].[fnOrdersGet](@UserID)", userIDParameter);
}
使用Glimpse,为count生成的查询是(运行需要10秒):
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[fnOrdersGet](@UserID) AS [Extent1]
) AS [GroupBy1]
分页查询(运行时也需要10秒):
SELECT
[Project1].[C1] AS [C1],
[Project1].[OrderID] AS [OrderID]
FROM ( SELECT
[Extent1].[OrderID] AS [OrderID],
1 AS [C1]
FROM [dbo].[fnOrdersGet](@UserID) AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[OrderID] DESC
OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
如果我将上面显示的查询复制到SQL Server中,它们都会在几毫秒内运行。
可以做些什么来改善这个?