简体   繁体   English

具有多对多关系的递归关系 MySQL 表

[英]Recursive relationship MySQL table with many-to-many relationship

I'm creating a database structure that contains property listings.我正在创建一个包含属性列表的数据库结构。 Each listing contains various amenities that I need to store, in a listing_amenities table.每个列表都包含我需要存储的各种便利设施,在listing_amenities表中。

This listing_amenities table will contain recursive records.listing_amenities表将包含递归记录。 For eg.例如。 A listing amenity might be "internet".上市便利设施可能是“互联网”。 Under this record we need to store children records such as what kind of internet it is (ADSL, Fibre etc.), the speed (20mbs, 50mbs etc.), as well as the ISP.在此记录下,我们需要存储子记录,例如它是哪种互联网(ADSL、Fiber 等)、速度(20mbs、50mbs 等)以及 ISP。

My question is whether a recursive model is the right solution here and how I would go about building the database structure for this or if there is a better solution for this kind of problem.我的问题是递归 model 是否是正确的解决方案,以及我将如何 go 来构建数据库结构,或者是否有更好的解决方案来解决此类问题。 In other words, would it be better to simply store all the amenities and their sub_properties in a big JSON blob column on the listings table?换句话说,将所有便利设施及其子属性简单地存储在列表表上的大 JSON blob 列中会更好吗?

The reason for creating a recursive table is to make querying better so that eventually we can easily query for property listings that have ADSL internet, for example, and sort this by location so that we could possibly target specific areas for marketing purposes to upgrade to fibre.创建递归表的原因是为了更好地查询,以便最终我们可以轻松地查询具有 ADSL 互联网的房产列表,并按位置对其进行排序,以便我们可以针对特定区域进行营销以升级到光纤.

First, I would recommend against a JSON BLOB, because that will not be easy and reliable to query.首先,我建议不要使用 JSON BLOB,因为这样查询起来并不容易且可靠。

If you identify a finite number of levels you are breaking down your sub_properties into, like two for amenity and sub_property, that will make it easier to deal with.如果您确定将 sub_properties 分解为有限数量的级别,例如 amenity 和 sub_property 的两个级别,这将更容易处理。

The reason to use one table, with self references to sub- or super- properties would be to support an indefinite number of levels you can break things down by.使用一个表,自引用子或超属性的原因是支持无限数量的级别,您可以将其分解。 However, that may be cumbersome, because you never know how many levels there are, so you don't know how many levels to use in a join.但是,这可能很麻烦,因为您永远不知道有多少级别,因此您不知道要在联接中使用多少级别。 If you can avoid this, it might be better.如果你能避免这种情况,它可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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