2013-09-22 141 views
0

美好的一天!長時間執行請求

List<TotalServiseCalls> TSC = (
    from scall in contextOMNIDB.UserFields698.AsEnumerable() 
    where scall.f2_creationd >= referenceDate 
    group scall by scall.f2_creationd.Month into scalls 
    select new TotalServiseCalls 
    { 
     mountN = (from sc in contextOMNIDB.UserFields698.AsEnumerable() 
      where sc.f2_creationd.Month == scalls.Key 
      select sc.f2_creationd).FirstOrDefault(), 
     date = (from sc in contextOMNIDB.UserFields698.AsEnumerable() 
      where sc.f2_creationd.Month == scalls.Key 
      select sc.f2_creationd.ToString("MMMM yyyy")).FirstOrDefault(), 
     totalCount = scalls.Count() 
    }).OrderBy(p => p.mountN).ToList(); 

MSSQL服務器有很多應用程序,它的負載非常高。 該查詢執行了四十秒。這是因爲服務器擁塞或查詢的複雜性?

這些表格有大約一萬條記錄,大小爲一兆字節。

+0

你肯定可以把'contextOMNIDB.UserFields698.AsEnumerable()'放到一個變量中來加快速度。你叫它三次。 – meilke

+0

然後,這個查詢被執行兩次:'from sc in contextOMNIDB.UserFields698.AsEnumerable()where sc.f2_creationd.Month == scalls.Key select sc.f2_creationd' – meilke

+0

@meilke非常感謝你!我是MVC 4和C#的新手,你能推薦一個用好的例子來學習的項目嗎? –

回答

1

很難說從你提供的信息來看它是查詢還是擁塞。兩件事情,你可以改善:

  1. contextOMNIDB.UserFields698.AsEnumerable()到一個變量
  2. 它放入一個可重複使用的格式,以及:from sc in contextOMNIDB.UserFields698.AsEnumerable() where sc.f2_creationd.Month == scalls.Key select sc.f2_creationd

這裏是一個可能的重構版本:

var userFields = contextOMNIDB.UserFields698.AsEnumerable(); 
List<TotalServiseCalls> TSC = (
    from scall in userFields 
    where scall.f2_creationd >= referenceDate 
    group scall by scall.f2_creationd.Month into scalls 
    select new TotalServiseCalls 
    { 
     mountN = Helper(userFields, scalls.Key).FirstOrDefault(), 
     date = Helper(userFields, scalls.Key).Select(o => o.ToString("MMMM yyyy")).FirstOrDefault(), 
     totalCount = scalls.Count() 
    }).OrderBy(p => p.mountN).ToList(); 

隨着輔助方法(我不得不使用object,因爲我沒有足夠的關於您的業務對象):

private IEnumerable<object> Helper(IEnumerable<object> userFields, object key) 
{ 
    return from sc in userFields 
     where sc.f2_creationd.Month == key 
     select sc.f2_creationd; 
} 
+1

爲你+1,但我認爲有更有效的方法來做到這一點,烏斯特從分組中取得第一項 –

1

您不必查詢contextOMNIDB.UserFields698.AsEnumerable() 3次,你可以得到第一個記錄從組,你可以得到每月分組的關鍵:

List<TotalServiseCalls> TSC = (
    from scall in contextOMNIDB.UserFields698.AsEnumerable() 
    where scall.f2_creationd >= referenceDate 
    group scall by scall.f2_creationd.Month into scalls 
    select new { 
     mountN = scalls.Key, 
     date = scalls.Select(x => x.ToString("MMMM yyyy").First(), 
     // You can use scalls.OrderBy(x => x (or any other order)).First() 
     // if you want to get specific date 
     totalCount = scalls.Count() 
    } 
).OrderBy(p => p.mountN).ToList(); 

這裏有一個簡單的例子:

var list = new List<Tuple<string, string>>(); 
list.Add(new Tuple<string, string>("test1", "2")); 
list.Add(new Tuple<string, string>("test1", "1")); 
list.Add(new Tuple<string, string>("test2", "1")); 
list.Add(new Tuple<string, string>("test2", "6")); 
list.GroupBy(x => x.Item1) 
    .Select(x => 
      new { 
       a = x.Key, 
       b = x.OrderByDescending(y => y).First(), 
       c = x.Count() 
      } 
    ).ToList()