【问题标题】:Selecting many columns/fields with lookup and junction tables. Out of memory exception使用查找表和联结表选择许多列/字段。内存不足异常
【发布时间】:2014-06-18 20:17:45
【问题描述】:

我有一个高度标准化的房地产数据库,用于存储列表数据。由于字段数量庞大,主要数据分布在 3 个表(Listings、ListingDataCommons 和 ListingDataOthers)之间,然后有几个连接表用于功能、类型等。

应用程序的用户使用 GUI 定义标准/字段,系统将生成(使用动态 linq)WhereSelectOrderBy 语句。问题是当Select 语句使用许多联结和/或查找表时,我得到一个Out Of Memory Exception

下面是一个强类型(为了便于阅读)的查询示例,该示例将引发错误。即使它只返回 MLS 编号的单个记录,它也会抛出 OOM 异常。

var ListingResult = context.Listings
    .Where(a => a.MLSNumber == "123456" || a.MLSNumber == "654321")
    .Select(a => new
    {
        //--- select some data from the Listings table
        a.MLSNumber,
        a.DateLastUpdated,
        a.DateLastImageUpdated,
        a.Address,
        a.ZipCode,
        a.DaysOnMarket,
        a.DisplayOnInternet,
        a.DisplayReviews,
        a.AuctionYN,
        a.ListPrice,
        a.LeasePrice,
        a.SystemID,
        a.DateLastPriceChange,
        a.DateLastStatusChange,
        a.DisplayAddressOnlineYN,
        a.ListingID,
        Status = a.Status.Name,
        PropertyType = a.PropertyType.Name,
        PropertyStyle = a.PropertyStyle.Name,
        Country = a.Country.Name,
        State = a.State.Name,
        County = a.County.Name,
        City = a.City.Name,
        SaleType = a.SaleType.Name,

        //--- select some data from the ListingDataCommons table
        BathsFull = a.ListingDataCommon.BathsFull,
        BathsHalf = a.ListingDataCommon.BathsHalf,
        Beds = a.ListingDataCommon.Beds,
        FireplaceYN = a.ListingDataCommon.FireplaceYN,
        GarageSpaces = a.ListingDataCommon.GarageSpaces,
        HOAYN = a.ListingDataCommon.HOAYN,
        LotAcres = a.ListingDataCommon.LotAcres,
        NewConstructionYN = a.ListingDataCommon.NewConstructionYN,
        PetsAllowedYN = a.ListingDataCommon.PetsAllowedYN,
        PetsMaxWeight = a.ListingDataCommon.PetsMaxWeight,
        PetsMaxNumber = a.ListingDataCommon.PetsMaxNumber,
        RemarksPublic = a.ListingDataCommon.RemarksPublic,
        SqftHeated = a.ListingDataCommon.SqftHeated,
        SubdivisionName = a.ListingDataCommon.SubdivisionName,
        Taxes = a.ListingDataCommon.Taxes,
        TaxYear = a.ListingDataCommon.TaxYear,
        YearBuilt = a.ListingDataCommon.YearBuilt,
        AirConditioning = a.ListingDataCommon.AirConditioning.Name,
        ConstructionStatus = a.ListingDataCommon.ConstructionStatus.Name,
        HousingForOlder = a.ListingDataCommon.HousingForOlder.Name,

        //--- select some data from the ListingDataOthers table
        CDDFee = a.ListingDataOther.CDDFee,
        CDDFeeYN = a.ListingDataOther.CDDFeeYN,
        CondoFee = a.ListingDataOther.CondoFee,
        HOAFee = a.ListingDataOther.HOAFee,
        HomesteadYN = a.ListingDataOther.HomesteadYN,
        LotDimensions = a.ListingDataOther.LotDimensions,
        LotSqft = a.ListingDataOther.LotSqft,
        NumberBays = a.ListingDataOther.NumberBays,
        NumberBuildings = a.ListingDataOther.NumberBuildings,
        NumberFloors = a.ListingDataOther.NumberFloors,
        NumberHotelRooms = a.ListingDataOther.NumberHotelRooms,
        NumberOffices = a.ListingDataOther.NumberOffices,
        NumberRestrooms = a.ListingDataOther.NumberRestrooms,
        ProjectedCompletionDate = a.ListingDataOther.ProjectedCompletionDate,
        SchoolElementary = a.ListingDataOther.SchoolElementary,
        SchoolMiddle = a.ListingDataOther.SchoolMiddle,
        SchoolHigh = a.ListingDataOther.SchoolHigh,
        SizePorch = a.ListingDataOther.SizePorch,
        SizeBedMaster = a.ListingDataOther.SizeBedMaster,
        SizeBed2 = a.ListingDataOther.SizeBed2,
        SizeBed3 = a.ListingDataOther.SizeBed3,
        SizeBed4 = a.ListingDataOther.SizeBed4,
        SizeBed5 = a.ListingDataOther.SizeBed5,
        SizeBonusRoom = a.ListingDataOther.SizeBonusRoom,
        SizeDinette = a.ListingDataOther.SizeDinette,
        SizeDiningRoom = a.ListingDataOther.SizeDiningRoom,
        SizeFamilyRoom = a.ListingDataOther.SizeFamilyRoom,
        SizeGreatRoom = a.ListingDataOther.SizeGreatRoom,
        SizeKitchen = a.ListingDataOther.SizeKitchen,
        SizeLivingRoom = a.ListingDataOther.SizeLivingRoom,
        SizeStudio = a.ListingDataOther.SizeStudio,
        SizeStudyDen = a.ListingDataOther.SizeStudyDen,
        SqftTotalBldg = a.ListingDataOther.SqftTotalBldg,
        TotalUnits = a.ListingDataOther.TotalUnits,
        VirtualTourLink = a.ListingDataOther.VirtualTourLink,
        WaterAccessYN = a.ListingDataOther.WaterAccessYN,
        WaterExtrasYN = a.ListingDataOther.WaterExtrasYN,
        WaterFrontageYN = a.ListingDataOther.WaterFrontageYN,
        WaterViewYN = a.ListingDataOther.WaterViewYN,
        ZipCodePlusFour = a.ListingDataOther.ZipCodePlusFour,
        Zoning = a.ListingDataOther.Zoning,
        AWCRemarks = a.ListingDataOther.AWCRemarks,
        ArchitecturalStyle = a.ListingDataOther.ArchitecturalStyle.Name,
        CondoFeeSchedule = a.ListingDataOther.TimeFrame.Name,
        FrontExposure = a.ListingDataOther.FrontExposure.Name,
        Foundation = a.ListingDataOther.Foundation.Name,
        Furnishing = a.ListingDataOther.Furnishing.Name,
        HOASchedule = a.ListingDataOther.TimeFrame.Name,
        MobileHomeWidth = a.ListingDataOther.MobileHomeWidth.Name,

        //--- select some data from the junction tables (which in turn use lookup tables)
        AdditionalRooms = a.ListingAdditionalRooms.Select(b => b.AdditionalRoom.Name),
        AppliancesIncluded = a.ListingAppliances.Select(b => b.Appliance.Name),
        CommunityFeatures = a.ListingCommunityFeatures.Select(b => b.CommunityFeature.Name),
        ExteriorConstructions = a.ListingExteriorConstructions.Select(b => b.ExteriorConstruction.Name),
        ExteriorFeatures = a.ListingExteriorFeatures.Select(b => b.ExteriorFeature.Name),
        Financings = a.ListingFinancings.Select(b => b.Financing.Name),
        FireplaceDescriptions = a.ListingFireplaceDescriptions.Select(b => b.FireplaceDescription.Name),
        FloorCoverings = a.ListingFloorCoverings.Select(b => b.FloorCovering.Name),
        FuelTypes = a.ListingFuelTypes.Select(b => b.FuelType.Name),
        GarageFeatures = a.ListingGarageFeatures.Select(b => b.GarageFeature.Name),
        GarageTypes = a.ListingGarageTypes.Select(b => b.GarageType.Name),
        HeatTypes = a.ListingHeatTypes.Select(b => b.HeatType.Name),
        InteriorFeatures = a.ListingInteriorFeatures.Select(b => b.InteriorFeature.Name),
        KitchenFeatures = a.ListingKitchenFeatures.Select(b => b.KitchenFeature.Name),
        LeaseIncludes = a.ListingLeaseIncludes.Select(b => b.LeaseInclude.Name),
        MasterBathFeatures = a.ListingMasterBathFeatures.Select(b => b.MasterBathFeature.Name),
        ParkingOptions = a.ListingParkingOptions.Select(b => b.ParkingOption.Name),
        PoolFeatures = a.ListingPoolFeatures.Select(b => b.PoolFeature.Name),
        PoolTypes = a.ListingPoolTypes.Select(b => b.PoolType.Name),
        PropertyUses = a.ListingPropertyUses.Select(b => b.PropertyUse.Name),
        RoofTypes = a.ListingRoofTypes.Select(b => b.RoofType.Name),
        WaterAccessTypes = a.ListingWaterAccessTypes.Select(b => b.WaterType.Name),
        WaterExtraTypes = a.ListingWaterExtraTypes.Select(b => b.WaterExtraType.Name),
        WaterFrontageTypes = a.ListingWaterFrontageTypes.Select(b => b.WaterType.Name),
        WaterViewTypes = a.ListingWaterViewTypes.Select(b => b.WaterType.Name),
    })
    .OrderBy(a => a.MLSNumber)
    .ToList();

有没有更好的方式来构建它?即使在查询上调用.ToString()查看生成的SQL也会抛出OOM异常。

更新:

回复@Gert Arnold,您能否进一步解释一下为什么数据库没有被规范化?让我们以Status 的字段为例,我有Status = a.Status.Name。数据库中有一个名为Statuses 的表,它有两列StatusIDName,数据类似于1|Active2|Pending3|SoldListings 表上的字段是StatusID,它包含对Statuses 表上StatusID 字段的引用。为了得到实际的名字而不是状态的ID,我必须做a.Status.Name。这与 PropertyType、PropertyStyle、Country、State、County、City、SaleType 的结构完全相同。

然后对于 ListingDataCommonsListingDataOthers 表,这些表是与 Listings 表的 1:1 关系创建的。创建它们是因为列表中有数百个字段,而不是将它们转储到一个巨大的表中,而是根据每个字段被查询的频率进行拆分。在这些表中,有一些列引用了查找表的 ID,而不是上面状态中解释的重复字符串值。

还有诸如ListingAdditionalRooms 之类的联结表,它具有 1:Many 关系,其中 1 个列表可以有许多额外的房间。 ListingAdditionalRooms 表(以及所有其他联结表)有 2 列(ListingID | AdditionalRoomID)引用Listings 表和AdditionalRooms 表中的相应记录。

如果这是您见过的最糟糕的数据库设计之一,您建议如何改进它?我是否应该拥有一个包含近 300 列的 Listings 表,通过记录重复存储字符串值?这似乎不是一个好的解决方案。请简要描述你将如何去做(Listings 表有数百万条记录)。不求图表,只求简要说明。

对于建议,将其分成更小的块并在 2 个请求中请求数据似乎确实可以解决问题(一个请求中的联结表数据,另一个请求中的所有其他数据)。

关于从未在任何 UI 中显示的数据量,这是不正确的。尽管此查询只是测试限制,但绝对有必要向用户显示列表的完整详细信息。

期待您对数据库结构的建议。

【问题讨论】:

  • 重新设计这不是几个cmets可以完成的事情。它需要大量的领域知识。一件事可能是有一个特征表,所以Listing 可以有一个Features 集合,而不是底部的这些众多导航属性。但是开始辨别领域实体。就像,Listing 什么都不是,它不是一个实体。您在谈论建筑物(属性)、房间(或部分)、地段、价格、地址、家具……这些都应该是模型中的 entities,而不是 properties 一个大桶的(或字段)。阅读“无处不在的语言”。
  • 您建议的Features 表是EAV 模式吗?我对 DDD 了解不多,但只是看不到制作列表的 PriceAddress 单个实体之类的东西,而不是列表对象的属性,特别是因为每个列表只有一个价格和一个地址。感谢您的建议。
  • 我不知道。只要您可以将功能描述为“具有”关联。但是,就此而言,当“事物”具有不断变化的属性数量时,EAV 可能是唯一的选择,但我不会太快使用它。地址(可能还有价格)通常被建模为值类型。但我认为您最大的挑战是尝试将这个Listing 对象从您的系统中取出。上市是一种行动。 DDD 中的陷阱 #1 是将动作(动词)建模为实体。

标签: c# linq entity-framework out-of-memory entity-framework-6


【解决方案1】:

实体框架在它必须生成的所有连接中窒息。只有在 a.<some property> 部分中,您才有 35 个不同的导航属性!除此之外,您还可以在嵌套的 Select 语句中访问大量导航属性。

核心问题是,这是迄今为止我见过的最糟糕的数据库设计之一。这些表格只是一堆不相关且重复的数据。没有任何标准化。

您唯一的希望是对数据模型进行大修,基本上是一种新的设计。 Entity Framework 是一个 ORM 对象关系映射器,因此应该从一些关系开始,使其成为一个有用的工具。

如果设计不在您手中,您可以考虑两件事:

  • 一块一块地获取内存中的数据,并从这些构建块构建客户端对象。

  • 必须可以使用较小的模型。我无法想象有一个 UI 视图可以同时显示所有这些数据。为每个视图构建专用视图模型。

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2010-10-05
    相关资源
    最近更新 更多