Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs	(revision 9560)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs	(revision 9561)
@@ -223,44 +223,26 @@
     private void UpdateTaskFacts(DimTime newTime, HiveDataContext db) {
       var lastFacts =
-        from f in db.FactTasks
-        group f by f.TaskId into grpFacts
-        select grpFacts.OrderByDescending(x => x.EndTime).First();
-
-      var tasksWithLastFact =
-        from t in db.Tasks
-        join lf in lastFacts on t.TaskId equals lf.TaskId into tLfJoin
-        from lWithLf in tLfJoin.DefaultIfEmpty()
-        where !t.IsParentTask
-        select new { Task = t, TaskDataSize = t.JobData.DataSize, LastFact = tLfJoin.FirstOrDefault() };
-
-      var tasksWithNewStateLogs =
-        from sl in db.StateLogs
-        join twlf in tasksWithLastFact on sl.TaskId equals twlf.Task.TaskId
-        where (twlf.LastFact == null || sl.DateTime > twlf.LastFact.EndTime) && sl.DateTime < newTime.Time
-        group sl by twlf.Task into factGroup
-        select new {
-          Task = factGroup.Key,
-          StateLogs = factGroup.OrderBy(x => x.DateTime),
-          LastSlave = factGroup.OrderByDescending(x => x.DateTime).First(x => x.SlaveId != null).SlaveId
-        };
-
-      var tasksWithLastFactsAndStateLogs =
-        from twsl in tasksWithNewStateLogs
-        join twlf in tasksWithLastFact on twsl.Task equals twlf.Task
-        select new { twlf.Task, twlf.TaskDataSize, twlf.LastFact, twsl.StateLogs, twsl.LastSlave };
-
-      var clients =
-        from cl in db.DimClients
-        //where cl.ExpirationTime == null
-        select cl;
-
-      var tasksWithLastFactsAndStateLogsAndLastResource =
-        from twlfasl in tasksWithLastFactsAndStateLogs
-        join cl in clients on twlfasl.LastSlave equals cl.ResourceId into clJoinGrp
-        from cl in clJoinGrp.DefaultIfEmpty()
-        select new { twlfasl.Task, twlfasl.TaskDataSize, twlfasl.LastFact, twlfasl.StateLogs, LastClient = cl };
+        from fact in db.FactTasks
+        group fact by fact.TaskId into factsPerTask
+        select factsPerTask.OrderByDescending(x => x.EndTime).First();
+
+      var lastFactAndStateLogsAndLastClientPerTask =
+        from task in db.Tasks.Where(task => !task.IsParentTask)
+        join lastFact in lastFacts on task.TaskId equals lastFact.TaskId into lastFactsPerTask
+        from lastFact in lastFactsPerTask.DefaultIfEmpty()
+        let newStateLogs = task.StateLogs.Where(stateLog => (lastFact == null || stateLog.DateTime > lastFact.EndTime) && stateLog.DateTime < newTime.Time)
+        let lastSlaveId = newStateLogs.OrderByDescending(stateLog => stateLog.DateTime).First(stateLog => stateLog.SlaveId != null).SlaveId
+        join client in db.DimClients.Where(client => client.ExpirationTime == null) on lastSlaveId equals client.ResourceId into clientPerTask
+        from client in clientPerTask.DefaultIfEmpty()
+        select new {
+          Task = task,
+          TaskDataSize = task.JobData.DataSize,
+          LastFact = lastFact,
+          StateLogs = newStateLogs.OrderBy(stateLog => stateLog.DateTime),
+          LastClientId = client != null ? client.Id : default(Guid?)
+        };
 
       var newTaskFacts =
-        from t in tasksWithLastFactsAndStateLogsAndLastResource.ToList()
+        from t in lastFactAndStateLogsAndLastClientPerTask.ToList()
         select new FactTask {
           TaskId = t.Task.TaskId,
@@ -268,5 +250,5 @@
           DimTimeStart = t.LastFact != null ? t.LastFact.DimTimeStart : newTime,
           DimTimeEnd = newTime,
-          LastClientId = t.LastClient != null ? t.LastClient.Id : (t.LastFact != null ? t.LastFact.LastClientId : Guid.Empty),
+          LastClientId = t.LastClientId ?? (t.LastFact != null ? t.LastFact.LastClientId : Guid.Empty),
           Priority = t.Task.Priority,
           CoresRequired = t.Task.CoresNeeded,
