实体框架核心执行多次

时间:2018-06-20 22:04:36

标签: c# sql sql-server entity-framework-core

我有这3张桌子。

  1. 产品(我存储有关该产品的一些基本信息,例如sku,gtin等)
  2. 品牌(存储有关该品牌的一些基本信息,这些信息将留在产品中)
  3. SuplierSpecs(加入该产品,每个产品可以有多个供应商和不同的SuplierId,如sku等)

现在,我从用户那里得到了一个关键字,并且我想给出产品gtin或sku或任何其他sku包含关键字的明显记录。

这是我的查询:

var query = from p in _productRepository.Table
            join s in _suplierProductSpecsMapRepository.Table on p.Id equals s.ProductId into sp
            from z in sp.DefaultIfEmpty()
            join c in _brandRepository.Table on p.Brand.Id equals c.Id into br
            from b in br.DefaultIfEmpty()
            where (p.Gtin.Contains(Keyword) || p.Sku.Contains(Keyword) || z.SupplierSku.Contains(Keyword))
            select new { p.Gtin, p.Name, b.Name };
return query.Distinct().Take(10);

我从控制器调用查询,上面的代码在服务中:

var products = _productService.GetProductsByKeyword(keyword);
var response = products.ToList();
return Json(products);

现在,当我致电.toList()时,它需要6-7秒的时间来执行,我有大约25万个产品和45万个供应商规格。

我看着输出窗口,发现它正在执行约8个查询,不知道为什么,甚至有完全相同的查询。

这是我的输出窗口:

Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (846ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-06-20T21:34:52.5602316Z","tags":{"ai.location.ip":"127.0.0.1","ai.operation.name":"POST Product/*****","ai.internal.nodeName":"***","ai.operation.id":"c690445c-4f21f9b370fd66f2","ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"***","ai.operation.parentId":"|c690445c-4f21f9b370fd66f2.","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (846ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']\r\nSELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]\r\nFROM [Products] AS [p]\r\nLEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]\r\nLEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]\r\nWHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))","severityLevel":"Information","properties":{"{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","parameters":"@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command","DeveloperMode":"true","commandText":"SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]\r\nFROM [Products] AS [p]\r\nLEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]\r\nLEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]\r\nWHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))","AspNetCoreEnvironment":"Development","elapsed":"846","commandTimeout":"30","commandType":"Text"}}}}
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (846ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Microsoft.EntityFrameworkCore.Database.Command Information: 20101 : Executed DbCommand (846ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor:Information: Executing JsonResult, writing value Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType44`3[System.String,System.String,System.String]].
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-06-20T21:34:52.5661523Z","tags":{"ai.location.ip":"127.0.0.1","ai.operation.name":"POST Product/*****","ai.internal.nodeName":"***","ai.operation.id":"c690445c-4f21f9b370fd66f2","ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"***","ai.operation.parentId":"|c690445c-4f21f9b370fd66f2.","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executing JsonResult, writing value Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType44`3[System.String,System.String,System.String]].","severityLevel":"Information","properties":{"{OriginalFormat}":"Executing JsonResult, writing value {Value}.","Value":"Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType44`3[System.String,System.String,System.String]]","CategoryName":"Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor","DeveloperMode":"true","AspNetCoreEnvironment":"Development"}}}}
Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor:Information: Executing JsonResult, writing value Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType44`3[System.String,System.String,System.String]].
Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor Information: 1 : Executing JsonResult, writing value Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType44`3[System.String,System.String,System.String]].
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (856ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-06-20T21:34:54.4290810Z","tags":{"ai.location.ip":"127.0.0.1","ai.operation.name":"POST Product/*****","ai.internal.nodeName":"***","ai.operation.id":"c690445c-4f21f9b370fd66f2","ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"***","ai.operation.parentId":"|c690445c-4f21f9b370fd66f2.","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (856ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']\r\nSELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]\r\nFROM [Products] AS [p]\r\nLEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]\r\nLEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]\r\nWHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))","severityLevel":"Information","properties":{"{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","parameters":"@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command","DeveloperMode":"true","commandText":"SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]\r\nFROM [Products] AS [p]\r\nLEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]\r\nLEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]\r\nWHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))","AspNetCoreEnvironment":"Development","elapsed":"856","commandTimeout":"30","commandType":"Text"}}}}
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (856ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Microsoft.EntityFrameworkCore.Database.Command Information: 20101 : Executed DbCommand (856ms) [Parameters=[@__p_3='?', @__Keyword_0='?' (Size = 4000), @__Keyword_1='?' (Size = 4000), @__Keyword_2='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT DISTINCT TOP(@__p_3) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
LEFT JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
LEFT JOIN [Brands] AS [c] ON [p].[BrandId] = [c].[Id]
WHERE (((CHARINDEX(@__Keyword_0, [p].[Gtin]) > 0) OR (@__Keyword_0 = N'')) OR ((CHARINDEX(@__Keyword_1, [p].[Sku]) > 0) OR (@__Keyword_1 = N''))) OR ((CHARINDEX(@__Keyword_2, [s].[SupplierSku]) > 0) OR (@__Keyword_2 = N''))
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action ***.Controllers.ProductController.*** (***) in 3737.9813ms

有人可以帮我弄清楚那个吗?

我是否在这里纠正了它大约8次执行以获取结果的情况?如果是这样,为什么?如果我接受相同的查询并在数据库中执行该查询,则只需不到一秒钟的时间

这是我在sql中使用的代码:

SELECT DISTINCT Top(10) [p].[Gtin], [p].[Name], [c].[Name]
FROM [Products] AS [p]
Left JOIN [SupplierSpecs] AS [s] ON [p].[Id] = [s].[ProductId]
Left Join [Brands] as [C] ON [p].BrandId = [c].Id
WHERE (((CHARINDEX('test', [p].[Gtin]) > 0) OR ('test' = N'')) OR ((CHARINDEX('test', [s].[SupplierSku]) > 0) OR ('test' = N''))) OR ((CHARINDEX('test', [p].[Sku]) > 0) OR ('test' = N''))

但是在代码中大约需要7秒钟。

1 个答案:

答案 0 :(得分:0)

您在这里有错字:

var products = _productService.GetProductsByKeyword(keyword);
var response = products.ToList();
return Json(products);

您要使序列化程序再次调用数据库,而不是使用response的物化结果。另外,您实际上应该在此处使用ToListAsync

var products = await _productService.GetProductsByKeyword(keyword).ToListAsync();
return Json(products);