简体   繁体   中英

Speed up EF5 Code First query

Following on from previous questions using EF5 Code First, SQL Server 2008 R2 db, Generic Repository and Unit Of Work patterns

I have the following function

public static void MoveGraphicPosition(int queueId, int graphicId, bool moveUp)
{
    using (var unitOfWork = new GraphicUnitOfWork(ConnGraphics, false))
    {
        var sourceGraphic = unitOfWork.GraphicRepository.FindSingle(g => g.Id == graphicId);

        if (sourceGraphic == null) return;

        var startPosition = sourceGraphic.QueuePosition;

        Graphic targetGraphic;

        if (moveUp)
        {
            targetGraphic =
                unitOfWork.PlayoutQueueRepository.FindSingle(q => q.Id == queueId, q => q.Graphics)
                          .Graphics.Where(g => g.QueuePosition < startPosition)
                          .OrderByDescending(g => g.QueuePosition)
                          .Take(1).FirstOrDefault();
        }
        else
        {
            targetGraphic =
                unitOfWork.PlayoutQueueRepository.FindSingle(q=> q.Id == queueId, q => q.Graphics)
                          .Graphics.Where(g => g.QueuePosition > startPosition)
                          .OrderBy(g => g.QueuePosition)
                          .Take(1).FirstOrDefault();
        }

        // Swop the positions
        if (targetGraphic == null) return;

        sourceGraphic.QueuePosition = targetGraphic.QueuePosition;

        targetGraphic.QueuePosition = startPosition;

        unitOfWork.GraphicRepository.Update(sourceGraphic);
        unitOfWork.GraphicRepository.Update(targetGraphic);

        // Save to database
        unitOfWork.Save();
    }
} 

Running this method via a Web Api call it takes about 2 seconds to run, I am bit puzzled as to why it takes this long, was expecting less than a second, is there any advice as to speed this up.

All we are trying to do is change queue positions of two graphic objects - swop queue positions around - our current one with the next one in the queue based on position.

Not sure whether this is EF5 or my LINQ query being inefficient.

FindSingle on the repository looks like this

public T FindSingle(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includes)
{
    var set = FindIncluding(includes);
    return (predicate == null) ? set.FirstOrDefault() : set.FirstOrDefault(predicate);
} 

public IQueryable<T> FindIncluding(params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> set = _context.GetEntitySet<T>();

    if (includeProperties != null)
    {
        foreach (var include in includeProperties)
        {
            set = set.Include(include);
        }
    }

    return set.AsQueryable();
}

I have a feeling might have to change this to take two objects in and then just do the update but this may be masking a problem which we will have to address at a later date as this should be straightforward.

这是我们在保存调用中设置的通知服务(因此其他服务/应用程序知道数据库中已更改的内容)-这导致了异常,从而增加了延迟,一旦我们输入了正确的IP,延迟就在0.7ms这是我所期望的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM