【发布时间】:2017-05-22 16:34:49
【问题描述】:
我有以下层次实体:
Country, City, Street.
Every country has cities, every city has streets.
我为我想运行的东西写了伪代码:
handle_country_before(country)
for city in country:
handle_city_before(city)
for street in city:
handle_street_before(street)
handle_street_after(street)
handle_city_after(city)
handle_country_after(country)
我尝试了以下方法:
document(noSql) 方法:
我以扁平方式保存了所有数据:
{
country : {
# Country "x1" info corresponding with the street
},
city : {
# City "y1" info corresponding with the street
},
street : {
# street info...
}
}
{
country : {
# Country "x1" info corresponding with the street
},
city : {
# City "y1" info corresponding with the street
},
street : {
# street info...
}
}
{
country : {
# Country "x1" info corresponding with the street
},
city : {
# City "y1" info corresponding with the street
},
street : {
# street info...
}
}
{
country : {
# Country "x1" info corresponding with the street
},
city : {
# City "y2" info corresponding with the street
},
street : {
# street info...
}
}
{
country : {
# Country "x1" info corresponding with the street
},
city : {
# City "y2" info corresponding with the street
},
street : {
# street info...
}
}
使用这种方法,我不得不使用下面的伪代码:
last_country = 0
last_city = 0
last_street = 0
for element in elements:
if element.country.id != last_country_id:
if (0 != last_country) :
handle_country_after(last_country)
handle_country_before(element.country)
if element.city.id != city:
if (0 != last_city) :
handle_country_after(last_city)
handle_country_before(element.city)
if element.street.id != street:
if (0 != last_street) :
handle_country_after(last_street)
handle_country_before(element.street)
缺点:我觉得这种方式有点矫枉过正,使用扁平结构不适合我的情况,而且速度非常慢,空间效率低。
SQL 方法:
我将每个实体保存在一个表中:Country、City、Street 并使用以下代码对其进行迭代:
country_cursor = query('select * from countries')
for country in country_cursor:
handle_country_before(country)
city_cursor = query('select * from cities where parent_country_ref=%s' % (country.id))
for city in city_cursor:
street_cursor = query('select * from streets where parent_city_ref=%s' % (city.id))
...
...
...
handle_country_after(country)
一开始,它看起来是最好的方法。但是随着我添加更多元数据表并且必须使用 JOIN 语句,它变得越来越慢,然后我尝试使用物化视图来加快速度,但得到的结果与使用文档相同。
自定义格式方法:
我尝试以我自己的二进制序列化格式保存信息:
<number of countries>[1st-country-data]<number of citieis>[1nd-city-data]<number of streets>[1st-street-data][2nd-street-data][3rd-street...]...
缺点:这无法扩展,我无法更新信息,我无法获取特定的城市/街道,每次搜索都是 O(n)。
我正在寻找的是一种序列化格式/数据库,它将是:
能够为现有元素添加/更新字段
高效的速度、空间和内存
符合 C 标准(无 CPP)
【问题讨论】:
-
But as I added more metadata tables and had to use JOIN statements it became increasingly slower为什么要添加更多元数据?为什么需要加入?你用过索引吗? -
@Lashane,假设每个城市都有一个市长,我还想在迭代时得到该城市的现任市长。我必须做城市和市长的笛卡尔积并选择与城市匹配的市长。
-
城市能有几个市长?一个市长可以管理多少个城市?
-
@Lashane,市长表也包含了该市的所有前任市长,但只有当前的市长记录参考了该市,该城市不为空
-
它没有回答我的问题
标签: sql c database design-patterns data-structures