简体   繁体   中英

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? 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 )
  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_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.

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