Skip to content

Commit f3c5f37

Browse files
committed
alternative performance fix?
1 parent 95affe3 commit f3c5f37

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Zero-K.info/Controllers/LobbyController.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ public async Task<ActionResult> ChatMessages(ChatModel model)
183183
model = model ?? new ChatModel();
184184

185185
var db = new ZkDataContext();
186-
var ret = db.LobbyChatHistories.AsQueryable();
187186
bool isMuted = Punishment.GetActivePunishment(Global.AccountID, Request.UserHostAddress, 0, null, x => x.BanMute) != null;
188187
if (!string.IsNullOrEmpty(model.Channel))
189188
{
@@ -203,9 +202,11 @@ await Global.Server.GhostSay(new Say()
203202
User = Global.Account.Name,
204203
});
205204
}
206-
ret = ret
207-
.Where(x => x.Target == model.Channel && x.SayPlace == SayPlace.Channel)
208-
.OrderByDescending(x => x.LobbyChatHistoryID).Take(200);
205+
string channelName = model.Channel;
206+
model.Data = db.LobbyChatHistories
207+
.SqlQuery("SELECT TOP 30 * FROM [dbo].[LobbyChatHistories] WHERE [Target] = {0} AND [SayPlace] = {1} AND [Time] > {2} ORDER BY [Time] ASC", channelName, SayPlace.Channel, DateTime.UtcNow.AddDays(-30))
208+
.ToList().AsQueryable();
209+
//Note if using Take(), it will be slow for uncommon channels like zktourney when ordering by Time and slow for common channels like zk if ordering by ID
209210
}
210211
else if (!string.IsNullOrEmpty(model.User))
211212
{
@@ -223,17 +224,18 @@ await Global.Server.GhostSay(new Say()
223224
User = Global.Account.Name,
224225
});
225226
}
227+
string otherName = model.User;
228+
string myName = Global.Account.Name;
226229
//Users can abuse rename to gain access to other users PMs, it's a feature
227-
ret = ret
228-
.Where(x => (x.User == model.User && x.Target == Global.Account.Name || x.User == Global.Account.Name && x.Target == model.User) && x.SayPlace == SayPlace.User)
229-
.OrderByDescending(x => x.LobbyChatHistoryID);
230+
model.Data = db.LobbyChatHistories
231+
.Where(x => (x.User == otherName && x.Target == myName || x.User == myName && x.Target == otherName) && x.SayPlace == SayPlace.User)
232+
.OrderByDescending(x => x.Time);
230233
}
231234
else
232235
{
233236
return PartialView("LobbyChatMessages", model);
234237
}
235238

236-
model.Data = ret;
237239
model.Message = "";
238240

239241
return PartialView("LobbyChatMessages", model);

0 commit comments

Comments
 (0)