EF Core 3.1查询性能

时间:2020-05-04 15:09:04

标签: asp.net-core-mvc ef-core-3.0

我正在学习asp.net核心mvc 3.1。而且不明白为什么我的查询这么慢。 这是EFCore生成的查询(从DebugConsole复制)

SELECT [p].[TagID], 
   [p].[CategoryID], 
   [p].[PicID], 
   [p].[TagSysType], 
   [p].[TagDescription], 
   [p].[TagPriority], 
   [p].[TagName], 
   [p].[TagTypeID], 
   [p].[PeriodID], 
   [p].[LinkID], 
   [p].[SourceID], 
   [p0].[LinkID], 
   [p0].[LinkLongName], 
   [p0].[LinkName], 
   [p1].[PeriodID], 
   [p1].[PeriodName], 
   [s].[SourceID], 
   [s].[CategoryID], 
   [s].[SourceName], 
   [s].[SourcePriority], 
   [p2].[TagTypeID], 
   [p2].[TagTypeName], 
   [e].[PicID], 
   [e].[PicName], 
   [c].[CategoryID], 
   [c].[CategoryGroupID], 
   [c].[CategoryName], 
   [a].[TagID], 
   [a].[Cost], 
   [a].[Scale], 
   [a].[Date], 
   [a].[Partner]
   FROM [Tag] AS [p]
          INNER JOIN [Link] AS [p0] ON [p].[LinkID] = [p0].[LinkID]
          INNER JOIN [Period] AS [p1] ON [p].[PeriodID] = [p1].[PeriodID]
          INNER JOIN [Source] AS [s] ON [p].[SourceID] = [s].[SourceID]
          INNER JOIN [TagType] AS [p2] ON [p].[TagTypeID] = [p2].[TagTypeID]
          LEFT JOIN [Pic] AS [e] ON [p].[PicID] = [e].[PicID]
          INNER JOIN [Category] AS [c] ON [p].[CategoryID] = [c].[CategoryID]
          LEFT JOIN [Sale] AS [a] ON [p].[TagID] = [a].[TagID]

此查询的结果包含大约10000行,我需要在我的应用程序中全部使用它们(不过滤)。问题是此查询的执行时间约为25秒。我认为对于网络核心技术或糟糕的数据库体系结构而言,它的查询过于复杂(包含大量的JOIN),但是当我在SQL Studio Management中运行此查询时,如果我在Studio中的应用程序停止运行,它几乎立即就会执行。如果我的应用程序正在运行,则SQL Studio管理中查询的执行时间大约也需要25秒。 App(Net core 3.1)和数据库(SQL2008)可在一台服务器(Win2012)上运行。

为什么会这样?

我在Controller中的操作

    public IActionResult Index()
    {
        ViewBag.PagingVisible = false;

        IQueryable<Tag> tags = _context.Tag
                                .Include(s => s.Link)
                                .Include(s => s.Period)
                                .Include(s => s.Source)
                                .Include(s => s.TagType)
                                .Include(s => s.Pic);
                                .Include(s => s.Category);
                                .Include(s => s.Sale);

        return PartialView("_SearchResult", tags.ToList());
    }

0 个答案:

没有答案