How can I improve the speed of this LINQ query?
using (var txn = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
{
var myAlertsList = (from alerts in _db.Alerts.AsNoTracking()
where alerts.AlertID >= Id.AlertID && alerts.DataWystapienia >Id.DataWystapienia
select new MyAlerts { AlertID = alerts.AlertID, DataWystapienia = alerts.DataWystapienia, Message = alerts.Message })
.OrderByDescending(a => a.AlertID).Take(Id.NumberOfRows).Concat((
from subAlerts in _db.SubAlerts.AsNoTracking()
where subAlerts.AlertID >= Id.AlertID && subAlerts.DataWystapienia > Id.DataWystapienia
select new MyAlerts { AlertID = subAlerts.AlertID, DataWystapienia = subAlerts.DataWystapienia, Message = subAlerts.Message })
.OrderByDescending(c => c.AlertID).Take(Id.NumberOfRows)).OrderByDescending(a => a.AlertID).Take(Id.NumberOfRows);
return myAlertsList;
}
It seems like the order by date is the longest process in this query. Is this correct?
The database has about a million records. NumberOfRows
is max 200. Time of this query is 45ms (after using only one orderby
). Is there something more that I can do to improve that LINQ query?
OrderByDescending
s followed by takes of which you can remove all but the last \$\endgroup\$