【问题标题】:Big Tables in Entity Framework实体框架中的大表
【发布时间】:2013-08-05 22:32:02
【问题描述】:

我在 Windows Azure Server 中有一个 WEB SQL,我需要在一个有 40.000 行的表中搜索一个项目。查询的执行时间是一分钟,对于 Web 应用程序(或任何类型的应用程序..)来说太长了。 ai 做了什么来减少这个时间?

我的问题类似这样:Entity Framework Very Large Table to List,但是因为分页的方式太大,所以答案是不能接受的。

带搜索的代码:

    public ActionResult SearchNcm(string typeSearch, string searchString)
    {
        var ncms = repository.VIEWNCM.ToList();

        if (Request.IsAjaxRequest())
        {
            if (!String.IsNullOrEmpty(searchString))
            {
                switch (typeSearch)
                {
                    case "cod":
                        ncms = ncms.Where(e => e.CODIGO_LEITURA.ToLower().Contains(searchString.ToLower()) || e.CODIGO.ToLower().Contains(searchString.ToLower())).ToList();
                        break;
                    default:
                        ncms = ncms.Where(e => e.DESCRICAO.ToLower().Contains(searchString.ToLower())).ToList();
                        break;
                }
            }
        }



        return PartialView("BuscarNcm", ncms);
    }

【问题讨论】:

  • 你想要 40k 行做什么?你真的需要将它们全部加载到内存中吗?
  • 对不起,我更新了这个问题。我真的不需要内存中的 40 000 行,但是在这个表上搜索非常耗时。
  • 发布 LINQ 查询和其他相关代码。
  • repository.VIEWNCM.ToList() 是否会将所有 40,000 行返回到内存中?看起来您正在加载整个表格,然后在内存中进行过滤/搜索。
  • 顺便说一句 - 任何涉及包含的搜索都会很慢。您可能想查看 Lucene.NET 进行文本搜索(因为我相信 Azure 不支持全文索引)。

标签: c# sql-server azure


【解决方案1】:

不是答案,但我需要空间来扩展我上面的评论:

请记住,在您迭代或调用 ToList() 之前,IQueryable 和 IEnumerable 不会做任何事情。这意味着您可以执行以下操作:

var ncms = repository.VIEWNCM; // this should be IQueryable or IEnumerable - no query yet

if(Request.IsAjaxRequest())
{
    if(!string.IsNullOrEmpty(searchString))
    {
        switch(typeSearch)
        {
                case "cod":
                    // No query here either!
                    ncms = ncms.Where(e => e.CODIGO_LEITURA.ToLower().Contains(searchString.ToLower()) || e.CODIGO.ToLower().Contains(searchString.ToLower()));
                    break;
                default:
                    // Nor here!
                    ncms = ncms.Where(e => e.DESCRICAO.ToLower().Contains(searchString.ToLower()));
                    break;
            }
        }
    }
}
// This is the important bit - what happens if the request is not an AJAX request?
else
{
    ncms = ncms.Take(1000); // eg, limit to first 1000 rows
}

return PartialView("BuscarNcm", ncms.ToList()); // finally here we execute the query before going to the View

如果 searchString 为空,您可能还需要一个默认过滤器

【讨论】:

  • 这回答了我的问题。
猜你喜欢
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2022-01-23
相关资源
最近更新 更多