简体   繁体   中英

right mysql structure for geo filtering

We are rearanging our database for home rentals and have following. We have different apis, where from the homes and apartments come and land. From apis we have only Country->Region->City(or village) Structure mainly. Also we have apartments for 1 extra country where we have Region->City->City Part->Street Structure

So we have made 2 mysql schemes. Could you critic a bit and maybe hint to a better way? 第一谢姆

And Second one

第二方案

Could you please criticize and maybe give a better way to do the db, which will lead to better performance, less joins etc? We are not mysql experts :(((

Images are small for viewing here, so the links

1st Scheme Image big one

2nd Scheme Image big one

The whole schema is wrong! What do you know about database normalisation? Your schema doesn't match to requirements of 3rd normal form at least. For partical example:

You have countries:

-CountryName1
-CountryName2
-CountryName3

You have regions:

-RegionOfCountry1_a
 ...
-RegionOfCountry3_a

You have cities:

-City_1_Of_Country1_Region_a
 ...
-City_3_Of_Country3_Region_a

In example data in tree view will be:

-CountryName1
|_ -RegionOfCountry1_a
    |_ -CityOf_Country1_Region_a
       |_ -City_1_Of_Country1_Region_a
       |_ -City_2_Of_Country1_Region_a
       |_ -City_3_Of_Country1_Region_a
    |_ -CityOf_Country1_Region_b
       |_ -City_1_Of_Country1_Region_b
       |_ -City_2_Of_Country1_Region_b
       |_ -City_3_Of_Country1_Region_b
    |_ -CityOf_Country1_Region_c
       |_ -City_1_Of_Country1_Region_c
       |_ -City_2_Of_Country1_Region_c
       |_ -City_3_Of_Country1_Region_c
|_ -RegionOfCountry1_b
    |_ -CityOf_Country2_Region_a
       |_ -City_1_Of_Country2_Region_a
    |_ -CityOf_Country2_Region_b
       |_ -City_1_Of_Country2_Region_b
    |_ -CityOf_Country2_Region_c
       |_ -City_1_Of_Country2_Region_c
|_ -RegionOfCountry1_c
    |_ -CityOf_Country2_Region_a
       |_ -City_1_Of_Country2_Region_c
...
-CountryName2
|_ -SecondCountry
       |_ -MegaCityCountry3
...
-CountryName3
|_ -MegaRegionCountry3
       |_ -MegaCityCountry3

You have houses table which have a constraints of foreign keys (country_id, city_id, region_id) which "links" to these 3 tables. It give a possibility to create incorrect record Such as:

*name = "MyHouse with address data from a far galaxy"
country_id = CountryName**1**
region_id = RegionOfSecondCountry (from CountrName**2**)
city_id = MegaCityCountry3 (from -CountryName**3**)*

It means next: you don't know normalization requirements or you don't forecast transitive dependencies

Try to do next

----------------
Table "**houses**"
----------------
id
houseNumber
description
*fk_city_id*
----------------

fk_city_id references to cities .id:

----------------
table "**cities**"
----------------
id
name
*fk_region_id*
----------------

fk_region_id references to regions .id:

----------------
table "**regions**"
----------------
id
name
*fk_country_id*
----------------

fk_country_id references to countries .id:

----------------
table "**countries**"
----------------
id
name
----------------

PS >> Sorry for my bad English

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