Elaine Lin
Elaine Lin

Reputation: 541

StackExchange.Exceptional Get All Errors is too slow

I am trying to quickly load the most recent exceptions using ErrorStore in StackExchange.Exceptional in C#.

Is there any way I can get all of the exceptions since a recent date without having to load and sort all of the exceptions? How can I speed up my code?

My current code gets the 100 most recent exceptions, but it is very slow.

string[] applications = new[] {loc1, loc2};
var errors = new List<Error>();
applications.ForEach(app => ErrorStore.Default.GetAll(errors, app));
return errors.OrderByDescending(m => m.CreationDate).Take(100);

Here is the documentation for ErrorStore.

Upvotes: 4

Views: 463

Answers (1)

Emmett Lin
Emmett Lin

Reputation: 666

Your code is probably slow because of your third line of code

applications.ForEach(app => ErrorStore.Default.GetAll(errors, app));

Here is ErrorStore.GetAll

public int GetAll(List<Error> errors, string applicationName = null)
{
    if (_isInRetry)
    {
        errors.AddRange(WriteQueue);
        return errors.Count;
    }

    try { return GetAllErrors(errors, applicationName); }
    catch (Exception ex) { BeginRetry(ex); }
    return 0;
}

And here is one implementation of GetAllErrors

protected override int GetAllErrors(List<Error> errors, string applicationName = null)
{
    using (var c = GetConnection())
    {
        errors.AddRange(c.Query<Error>(@"
Select Top (@max) * 
From Exceptions 
Where DeletionDate Is Null
And ApplicationName = @ApplicationName
Order By CreationDate Desc", new { max = _displayCount, ApplicationName = applicationName.IsNullOrEmptyReturn(ApplicationName) }));
    }

    return errors.Count;
}

The sql query in GetAllErrors will select all exceptions, not just the top 100. Suppose there are n total exceptions per application, and two applications. Thus, your third line of code would have to run in f(n) = 2n time.

To speed up your program, you can write a method similar to GetAllErrors, but modify the sql query to select only 100 exceptions from the error database, so even if there are billion exceptions, the code will finish after finding the first 100 that match.

You could also move the code to search for all application names to sql so you would not need to order by the creation date again.

Upvotes: 5

Related Questions