简体   繁体   English

如何提高使用Linq-to-Entities和XElement生成XML的应用程序的性能

[英]How can I improve performance of my application which generates XML using Linq-to-Entities and XElement

I have built an application that generates a large XML file using Linq-to-Entities and XElement. 我建立了一个使用Linq-to-Entities和XElement生成大型XML文件的应用程序。 It takes a whole core of our 2GHz server for about half an hour and uses ~1GB of memory. 它占用了我们2GHz服务器的整个核心大约半小时,并使用了大约1GB的内存。

I'm doing the following type of work: 我正在做以下类型的工作:

var xml = from x in dbContext.Table1
          select new XElement("Table1",
                     new XElement("Field1", x.Field1),
                     new XElement("Field2", x.Field2),
                     new XElement("Field3", x.Field3),
                     new XElement("MoreFields",
                         new XElement("FieldA", x.MoreFields.FieldA),
                         new XElement("FieldA", x.MoreFields.FieldA),
                         new XElement("FieldA", x.MoreFields.FieldA.DoSomeWorkWithThisField())
    )
);

I have another level of depth or two and several of the fields have work done like parsing an int out of a string using RegEx.Match() 我有另外一两个深度级别,其中几个字段已经完成工作,例如使用RegEx.Match()从字符串中解析出一个int

Does anyone have any optimization or refactoring recommendations? 有没有人有任何优化或重构建议? I tried using XStreamingElement but it didn't seem to make any difference. 我尝试使用XStreamingElement但似乎没有任何区别。

It looks like you're fetching the whole of Table1 multiple times. 看来您要多次获取整个Table1

Can you fetch it to a List<T> and then use that repeatedly? 您能否将其获取到List<T>然后重复使用?

var list = dbContext.Table1.ToList();
var xml = new XElement("Table1",
    new XElement("Field1", list.Field1),
    new XElement("Field2", list.Field2),
    new XElement("Field3", list.Field3),
    new XElement("MoreFields",
        new XElement("FieldA", list.MoreFields.FieldA),
        new XElement("FieldA", list.MoreFields.FieldA),
        new XElement("FieldA", list.MoreFields.FieldA.DoSomeWorkWithThisField())
    )
);

I suspect there's rather more involved than this, but basically - fetch what you need into memory up-front, and then work from that. 我怀疑其中涉及的更多,但基本上是-将所需的内容预先存储到内存中,然后从中进行工作。 If you only need to use an item of data once, that's fine to do only when you need it - but avoid pulling the same data time and time again. 如果您只需要使用一次数据,那么只在需要时才可以使用它-避免一次又一次提取相同的数据。

How big is the document you're generating, and how much memory does your machine have? 您生成的文档有多大,并且计算机有多少内存? You might want to try looking at the performance counters - it's possible that you're spending most of the time garbage collecting. 您可能想尝试查看性能计数器-您可能会花费大部分时间进行垃圾收集。

对于大型文档,应考虑使用像XmlWriter这样的流式解决方案,而不是像XDocument这样将所有数据保留在内存中的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM