首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >实体框架核心多次执行

实体框架核心多次执行
EN

Stack Overflow用户
提问于 2018-06-21 06:04:36
回答 1查看 633关注 0票数 0

我有三张桌子。

  1. 产品(我存储有关该产品的一些基本信息,如sku、gtin等)
  2. 品牌(存储有关该品牌的一些基本信息,留下来加入该产品(左加入产品,每个产品可以有多个供应商和不同的SuplierId,如sku等)

现在我从用户那里得到了一个关键字,我想返回产品gtin、sku或任何suplier sku包含该关键字的不同记录。

所以我的问题是:

代码语言:javascript
运行
复制
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);

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

代码语言:javascript
运行
复制
var products = _productService.GetProductsByKeyword(keyword);
var response = products.ToList();
return Json(products);

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

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

下面是我的输出窗口:

代码语言:javascript
运行
复制
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中使用的代码:

代码语言:javascript
运行
复制
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秒。

EN

回答 1

Stack Overflow用户

发布于 2018-06-21 06:28:56

你这里有一个打字错误:

代码语言:javascript
运行
复制
var products = _productService.GetProductsByKeyword(keyword);
var response = products.ToList();
return Json(products);

您将使序列化程序再次调用数据库,而不是使用response的物化结果。另外,你真的应该在这里使用ToListAsync

代码语言:javascript
运行
复制
var products = await _productService.GetProductsByKeyword(keyword).ToListAsync();
return Json(products);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50957715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档