【问题标题】:Telerik: Generated SQL is different from sample projectTelerik:生成的 SQL 与示例项目不同
【发布时间】:2019-08-20 18:42:16
【问题描述】:

我正在使用 Progress® Kendo UI® Grid for ASP.NET MVC 来显示类别表中的数据

在 Telerik 示例应用程序和我的应用程序上生成的 SQL 代码不同 (注:Telerik(2017.1.223.545) 和 .net framework(4.5.1) 所有版本在两个项目中都相同)

为什么在我的应用程序中生成不同的 SQL?我希望我的应用程序以相同的方式生成,以解决其他具有大量数据的项目中的性能问题。

我的申请:

public ActionResult ApplicationUserRole_Read([DataSourceRequest] DataSourceRequest request)
        {
            NorthwindEntities context = new NorthwindEntities();
            context.Categories.ToDataSourceResult(request);
            return Json("nothing");                  
        }

我的应用程序(生成的 SQL):

2019-08-20 12:46:22,320 [54] INFO RollingFileDBAppender - SELECT 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[CategoryName] AS [CategoryName], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[Picture] AS [Picture]
    FROM [dbo].[Categories] AS [Extent1]
    ORDER BY [Extent1].[CategoryID] ASC
    OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY 

例子:

public ActionResult Paging_Categories([DataSourceRequest] DataSourceRequest request)
        {
            var northwind = new SampleEntities();
            northwind.Categories.ToDataSourceResult(request);
            return Json("nothing");
        }

示例(生成的 SQL):

2019-08-20 12:19:10,952 [26] INFO Kendo.Mvc.Examples.Models.SampleEntities+<>c line 25 - SELECT TOP (10) 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[CategoryName] AS [CategoryName], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[Picture] AS [Picture]
    FROM ( SELECT [Extent1].[CategoryID] AS [CategoryID], [Extent1].[CategoryName] AS [CategoryName], [Extent1].[Description] AS [Description], [Extent1].[Picture] AS [Picture], row_number() OVER (ORDER BY [Extent1].[CategoryID] ASC) AS [row_number]
        FROM [dbo].[Categories] AS [Extent1]
    )  AS [Extent1]
    WHERE [Extent1].[row_number] > 0
    ORDER BY [Extent1].[CategoryID] ASC

【问题讨论】:

  • 我会检查 Kendo UI 配置是否有任何不同之处,例如与 SQL Server 版本的兼容性。示例生成的 SQL 看起来像是为不支持 OFFSET/FETCH 的 SQL Server 2008 生成的。实际上两者都是平等的。
  • @StevePy,感谢您的输入,在我的情况下,与 OFFSET/FETCH 相比,行号查询非常快。(注意:我的实际查询更复杂,表中有大量数据. 我提供了上述查询作为等效示例)。我已经向 Telerik 团队开了一张票,我会在收到他们的消息后更新。
  • Offset/Fetch 应该比 RowNum 方法快得多。我怀疑您的两个场景以某种方式运行两个完全不同的查询,这些查询隐藏了索引/执行计划问题,或者您可能面临延迟加载之类的问题。您是否在该网格填充以捕获所有查询时针对数据库运行了分析器,然后测试了查询执行计划?当问题示例是橘子时,很难就苹果与苹果的比较给出建议。 :)
  • @StevePy 我错了,两个项目的实体框架版本不一样,这就是生成的sql不同的原因。

标签: entity-framework entity-framework-6 telerik-grid telerik-mvc


【解决方案1】:

两个项目中使用的实体框架不同。 出于某种原因,当我使用实体框架版本 6.0.0 时,我们的 Web 应用程序的性能会更快

EF 在不同版本中生成 SQL 的方式不同。

Entity Framework - 6.0.0

SELECT TOP (15) 
    [Extent1].[EmpId] AS [EmpId], 
    [Extent1].[Age] AS [Age], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[Lastname] AS [Lastname]
    FROM ( SELECT [Extent1].[EmpId] AS [EmpId], [Extent1].[FirstName] AS [FirstName], [Extent1].[Lastname] AS [Lastname], [Extent1].[Age] AS [Age], row_number() OVER (ORDER BY [Extent1].[EmpId] ASC) AS [row_number]
        FROM [dbo].[Employees] AS [Extent1]
    )  AS [Extent1]
    WHERE [Extent1].[row_number] > 0
    ORDER BY [Extent1].[EmpId] ASC

Entity Framework - 6.1.3

SELECT 
    [Extent1].[EmpId] AS [EmpId], 
    [Extent1].[Age] AS [Age], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[Lastname] AS [Lastname]
    FROM [dbo].[Employees] AS [Extent1]
    ORDER BY [Extent1].[EmpId] ASC
    OFFSET 0 ROWS FETCH NEXT 15 ROWS ONLY 

Entity Framework - 6.2.0

SELECT 
    [Extent1].[EmpId] AS [EmpId], 
    [Extent1].[Age] AS [Age], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[Lastname] AS [Lastname]
    FROM [dbo].[Employees] AS [Extent1]
    ORDER BY row_number() OVER (ORDER BY [Extent1].[EmpId] ASC)
    OFFSET 0 ROWS FETCH NEXT 15 ROWS ONLY 

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2020-06-12
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多