【问题标题】:Retrieve Big data using EF使用 EF 检索大数据
【发布时间】:2015-11-16 16:09:35
【问题描述】:

我有两张桌子“Kelime”

public class Kelime
{

    public int ID { get; set; }

    public string Word { get; set; }

    public DateTime Date { get; set; }
}

和“安南”

public class Anlam
{

    public int ID { get; set; }

    public string Meaning { get; set; }

    public DateTime Date { get; set; }

    public int Kelimesi_ID { get; set; }
    [ForeignKey("Kelimesi_ID")]
    public virtual Kelime kelime { get; set; }
}

两个表都包含超过 80k 的数据。我认为它们不是很大,但我在这个查询中遇到了问题:

Repository<Kelime> _rk = new Repository<Kelime>();
Repository<Anlam> _ra = new Repository<Anlam>();

IEnumerable<int> kelimeIdler = _ra.All().Select(s => s.Kelimesi_ID).Distinct();
int _kelimecik= _rk.Find(w => !kelimeIdler.Contains(w.ID)).ID;

Kelime _kelimecim = _rk.All().Where(w =&gt; !kelimeIdler.Contains(w.ID)).FirstOrDefault();

我正在尝试获取“Kelime”、“Kelime 列表”或其“id”,无论哪个不在我的“Anlam”表中都没有关系。 “包含”部分有超时。我试图编写非聚集索引,但它不接受子查询。我应该怎么做才能实现我想要的?非常感谢。

【问题讨论】:

  • 80k 不是大数据。 SQL Server 引发了超时,因此 EF 并不是真正的问题。您可以在 SQL Server (Profiler) 中查看已运行的查询,然后通过添加正确的索引对其进行优化
  • 感谢您的回复 sql server profiler 没有帮助,但正如您所说,ef 不是问题所在。问题是基于我的“数据上下文”,我使用“单例模式”,我的问题就消失了。

标签: sql performance entity-framework indexing repository


【解决方案1】:
private static DataContext _context;

public static DataContext ContextOlustur()
{
    if (_context == null)
    {
        _context = new DataContext();
    }

    return _context;
}

将此模式添加到我的数据上下文类解决了我的问题。因为我的查询使用了两个不同的上下文,这就是连接数据库出现问题并超时的原因。这种模式可以防止创建另一个上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    相关资源
    最近更新 更多