简体   繁体   English

如何为地理零售位置层次结构建模

[英]How to model geographical retail location hierarchy

I am trying to create the database structure for the locations of a retail company where the base is store and a district is a group of stores grouped geographically and so on. 我正在尝试为零售公司的位置创建数据库结构,该公司的基础是商店,而一个地区是按地理位置分组的一组商店,依此类推。 From largest to smallest(area wise) it goes: 从最大到最小(区域明智):

Area -> Region -> District -> Store 区域->地区->区域->商店

Each level has the same basic columns: 每个级别都有相同的基本列:

ID
Name
LeaderID
CreatedOn
UpdatedOn
Active

Is it better to have each of these levels in their own table, or condense it down to one 'Locations' table with a 'ParentLocation' column? 将这些级别中的每一个都放在自己的表中,或者将其压缩为一个带有“ ParentLocation”列的“ Locations”表,会更好吗? Would there be a noticeable performance hit since going up the hierarchy will required joining on the same table multiple times? 由于上级将需要多次连接到同一张表上,因此性能会受到明显影响吗?

It is better to have one table of Locations and have a parent location column. 最好有一个位置表并有一个父位置列。

As for the traversal of the tree there are many ways to do this to handle different scenarios. 至于遍历树,有许多方法可以处理不同的情况。 The basic answer will be it depends on how much data you have in the table and how you are using it. 基本的答案将取决于表中有多少数据以及如何使用它们。

Some of the different options that you have with this are: 您可以使用以下一些不同的选项:

  1. Create a path index (eg AreaId|RegionId|District ) 创建路径索引(例如AreaId | RegionId | District)
  2. Recursively traverse the tree 递归遍历树
  3. Self join 自我加入

I'm sure there are others as this isn't a complete list but what you're going to have to do is create some test metrics and then decide on what's best for your problem. 我敢肯定还有其他人,因为这还不完整,但是您要做的就是创建一些测试指标,然后确定最适合您的问题的方法。

You need a table for level with parent child relationship, as long you create proper index for the join fields the performance wont be affected. 您需要一个具有父子关系级别的表,只要您为连接字段创建正确的索引,性能就不会受到影响。

Area: { area_id (pk), area }
Region: { region_id (pk), area_id (fk), region }
District: { district_id (pk), region_id (fk), district } 

Then your Store Table will have a reference to the district the more specific location. 然后,您的“ Store表”将具有对该区域更具体位置的引用。

Store: { store_id (pk), district_id (fk), <other store fields>}

If you want Stores from some specific Area you join all tables 如果要从某个特定区域中的商店,请加入所有表

 SELECT S.*
 FROM Stores S
 JOIN District D ON S.distric_id = D.distric_id
 JOIN Region R on D.region_id = R.region_id
 JOIN Area A on R.area_id = A.area_id
 WHERE A.area = 'some area'

EDIT: Maybe I understand the geography backwards. 编辑:也许我向后理解了地理。 For my example I assume Area is the bigger geometry and District are the smaller one. 在我的示例中,我假设Area是较大的几何图形,而District是较小的几何图形。

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

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