特别是SQL Query性能提升

时间:2017-09-26 11:03:48

标签: c# performance entity-framework sql-server-2008

我使用具有内存限制的Amazon Lambda来执行某些操作。而且这个查询检索了大量的数据(~100k +行)并且超出了这些限制。

SELECT EE.ID, ILE.Zip AS EventLocationZip, EET.Name as EventType, EE.TypeID as EventTypeID, E.Length, EL.Name as LengthUnit, 
                                        CP.Name as EventPlaceName, EE.FKPlaceID as EventPlaceID, IL.Name as EventLocationName, IL.ID as EventLocationId, 
                                        I.ID as InstituteID, I.Name as InstituteName, I.Email, I.Telephone, I.FAX, ILA.City, ILA.Zip, ILA.Street, ILA.Country,
                                        E.ID as EducationID, E.Name as EducationName, E.Link as EducationLink, CE.Name as EducationTypeName, CE.ID as EducationTypeID, 
                                        ESI.TypeID as EventStartInfoTypeID, ESI.Date as StartDate, ESI.EndDate, ESI.Month, ESI.MonthYear, ESI.FKSemesterID as Semester, ESI.SemesterYear, 
                                        EEP.Vat, EEP.Price, EC.Name as Currency, IP.Value as TextPropertyValue, IP.FKTextPropertyID as TextPropertyID, CPIL.Name AS EventLocationPlaceName, 
                                        CPIL2.Name AS EventLocationPlaceNameParent 
        FROM
            education.EducationEvents EE
            INNER JOIN education.EventStartInfos ESI ON EE.ID = ESI.FKEducationEventID
            INNER JOIN education.EducationEventPrices EEP ON EE.ID = EEP.FKEventID
            INNER JOIN education.TypeOfEvents EET ON EE.TypeID = EET.ID
            LEFT JOIN institute.InstituteLocations IL ON IL.ID = EE.FKLocationID
            LEFT JOIN core.Places CPIL ON CPIL.ID = IL.FKPlaceID
            LEFT JOIN core.Places CPIL2 ON CPIL2.ID = CPIL.FKParentID 
            LEFT JOIN institute.LocationAddresses ILE ON ILE.ID = IL.FKVisitingAddressID
            LEFT JOIN core.Places CP ON CP.ID = EE.FKPlaceID
            INNER JOIN economy.Currencies EC ON EEP.FKCurrencyID = EC.ID
            INNER JOIN education.Educations E ON EE.FKEducationID = E.ID
            LEFT JOIN education.LengthUnits EL ON E.FKLengthUnitID = EL.ID
            INNER JOIN core.EducationTypes CE ON CE.ID = E.FKEducationTypeID
            INNER JOIN institute.Institutes I ON E.FKInstituteID = I.ID
            LEFT JOIN institute.InstituteTextProperties IP ON IP.FKInstituteID = I.ID
            INNER JOIN institute.LocationAddresses ILA ON ILA.ID = I.FKMainAddressID
            INNER JOIN searchIndex.IndexInstitutes II ON I.ID = II.FKInstituteID
            INNER JOIN searchIndex.IndexEducations IE ON E.ID = IE.FKEducationID AND II.FKIndexID = IE.FKIndexID
        WHERE
            II.FKIndexID = @IndexID AND I.IsPublished = 1 AND ESI.Description IS NULL AND (IP.FKTextPropertyID = 1 OR IP.FKTextPropertyID IS NULL)
        ORDER BY I.ID DESC

使用Entity框架。扩展:

public static class DbContextExtensions
    {
        public static async Task<IReadOnlyList<T>> FromSql<T>(this DbContext context, string sql, params object[] parameters) where T : class
        {
            return await context.Set<T>().FromSql(sql, parameters).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }
    }

用法:

var data = await _context.FromSql<Model>("Query string", parameter);

优化特定查询的最佳永久解决方案是什么?谢谢你的回答

0 个答案:

没有答案