L
L
LuckyReveal2012-11-02 11:34:36
.NET
LuckyReveal, 2012-11-02 11:34:36

Optimizing a resource-intensive function (linq)?

Good afternoon.
The function to be optimized is called approximately 100,000 times a day. As you can see, the project is in the azura cloud…
In general. (The code has been cut down to the required function)
www.everfall.com/paste/id.php?wqfnoehtgdp1
As you can see, I have already carried out a small optimization in the form of making a query to the database into a compiled function.
There is an Exchanger table (5.000 records, an increase of 200 per day). It stores user data, Id, points, like and follow activity and some other data.
There is a LikeExchanger_Data table (500,000 records, an increase of 20,000 per day). It stores data about likes (So that one photo is not shown several times to the same user.).
The user likes it, LikeExchanger_Data is filled with data about the like, LikesList is called (a function above in the code), which will return a new list of photos that will not include photos that already have likes.
At the moment, from the moment of the POST request from the site through the API to the return of the result - ~ 250ms. It is necessary to reduce at least to 150-180.
I had an idea to break the logic. On the server itself, store a hot cache not only for Exchanger, but also for LikeExchanger_Data, and send requests to the database to be eaten by the backend through the Azure Service Bus.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2012-11-02
@LuckyReveal

Maybe so?

public class ExchangerProvider
{
    public static IEnumerable<Robofollower.Exchanger> LikesList(int userId)
    {
        using (var context = new DataClassesDataContext())
        {
            return context.Exchanger
                .Where(e => e.UserId != userId)
                .Where(e => e.LikeActive)
                .Where(e => e.Points >= 1)
                .Where
                (e => 
                    context.LikeExchanger_Data
                        .Where(l => l.UserId == userId)
                        .All(l => l.MediaId != e.MediaId)
                )
                .OrderByDescending(e => e.Points)
                .Take(5)
                .ToList();
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question