如何利用大型集合提高linq性能(Code first POCO class)

时间:2015-02-02 17:28:50

标签: c# performance linq

我想计算最佳预订。

当我在PL / SQL中执行它时,它非常慢且昂贵。所以我尝试使用LINQ。我有两个收藏品,订单和库存。但它也很慢。这对我来说还不够快。

我无法使用编译查询。我不能使用数据字典索引(我不使用唯一数据)。 AsParallel()没有工作(至少不适合我)。

如何提高代码的性能?

var orders = deserialize<Order>(@"C:\orders.bin");
  var stocks = deserialize<Stock>(@"C:\Stocks.bin");
for (int i = 0; i < 1000; i++)
{

  var order1 = (from p in orders
                where p.QTY_DEMAND > 0
                group p by new { p.OUTER_GROUP_ID, p.ORG_CODE, p.ARTICLE_CODE, p.FROM_ACCOUNTNO, p.AVAILABILITY_STATE } into g
                select new
                {
                    OUTER_GROUP_ID = g.Key.OUTER_GROUP_ID,
                    ORG_CODE = g.Key.ORG_CODE,
                    Article_code = g.Key.ARTICLE_CODE,
                    FROM_ACCOUNTNO = g.Key.FROM_ACCOUNTNO,
                    AVAILABILITY_STATE = g.Key.AVAILABILITY_STATE,
                    QUANTITY = g.Sum(p => p.QTY_DEMAND)
                });
  var order2 = from p in order1
                join vw1 in stocks on new { org_code = p.ORG_CODE, article_code = p.Article_code, account_no = p.FROM_ACCOUNTNO, av_state = p.AVAILABILITY_STATE }
                equals new { org_code = vw1.ORG_CODE, article_code = vw1.ARTICLE_CODE, account_no = vw1.ACCOUNTNO, av_state = vw1.AVAILABILITY_STATE }
                where vw1.QTY_STOCK > 0
                group new { p, o = vw1 } by new { baseValue, vw1.LOCATION_TYPE, vw1.PLACE_TYPE, p.Article_code, p.QUANTITY, vw1.STANDS_ON_THM } into g
                let SumQTY_STOCK = g.Sum(o => o.o.QTY_STOCK)
                select new
                {
                    BASE_VALUE = g.Key.baseValue,
                    g.Key.Article_code,
                    ...
                }
   var order3 = from p in order2
                group p by new { p.BASE_VALUE, p.LOCATION_TYPE, p.PLACE_TYPE } into g
                select new
                {
                    ...
                }
   var order4 = from a in order3
                ...
   foreach (var item in order4)
   {
       //doing somethings
   }
}

public List<T> deserialize<T>(string path)
        {
            using (var file = File.OpenRead(path))
            {
                BinaryFormatter ser = new BinaryFormatter();
                return (List<T>)ser.Deserialize(file);
            }
        }

1 个答案:

答案 0 :(得分:0)

首先......

将对象初始值设定项移出循环。您无需加载集合1000次。

更改

for (int i = 0; i < 1000; i++)
{
   var orders = deserialize<Order>(@"C:\orders.bin");
   var stocks = deserialize<Stock>(@"C:\Stocks.bin");

var orders = deserialize<Order>(@"C:\orders.bin");
var stocks = deserialize<Stock>(@"C:\Stocks.bin");
for (int i = 0; i < 1000; ++i)
{
    ...

我来到这个实现是因为你在每次传递上加载相同的文件,这是昂贵的。您只需要加载一次。