2011-09-16 120 views
0

我正在嘗試爲網站設計一個數據庫結構,該網站提供人們打開個人資料。 我想弄清楚我選擇的設計是否足夠好。我懷疑它可能有問題的原因是我使用很多表之間的許多關係。這樣,當一個人的頁面被加載時,幕後會有很多JOIN,這肯定會造成一個瓶頸。如果您能幫我弄清楚設計是否合適或者我應該重新考慮它,我將不勝感激。用Django設計數據庫結構

於是我想出了以下設計:

class Person(models.Model): 

    GENDER_CHOICES = (
        ('M', 'Male'), 
        ('F', 'Female'), 
    ) 

    name = models.CharField(max_length=200) 
    # Each person can have one profession 
    profession = models.ForeignKey(Profession) 
    email = models.CharField(max_length=100, blank=True) 
    website = models.CharField(max_length=200, blank=True) 
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)) 
    birth_date = models.DateField(blank=True, null=True)  

class Profession(models.Model):  
    type = models.CharField(max_length=20, blank=False, choices = ("Teacher","Pilot","Politician")) 

#Each person can have several geolocations (represent the person's home/office/other address) 
class Geolocation(models.Model):  
    latitude = models.FloatField() 
    longitude = models.FloatField() 
    address = models.TextField() 
    related_person = models.ForeignKey(Person, blank=False, null=False) 

#Each person can have several medias (you tube movies) 
class Media(models.Model): 
    youtube_id = models.CharField(max_length=100) 
    related_person = models.ForeignKey(Person, blank=False, null=False) 

#Each person can have several websites 
class Websites(models.Model): 
    website_url = models.CharField(max_length=200, blank=False) 
    website_name = models.CharField(max_length=200) 
    related_person = models.ForeignKey(Person, blank=False, null=False) 
    #See below... 
    related_link_farm = models.ForeignKey(LinkFarms, blank=True, null=True) 

#Each website that refers to a person must be also refered to a link farm 
class LinkFarms(models.Model): 
    farm_name = models.CharField(max_length=200, blank=False) 

注意,每個頁面加載至少有5臺連接。

感謝,

梅厄

回答

0

這是確定標準化的數據。這就是關係數據庫的用途!你的設計對我來說看起來很好。如果它成爲一個問題,你總是可以緩存頁面渲染。

0

也許你可以刪除專業表是這樣的:

class Person(models.Model): 
    ... 

    TEACHER_ID = 'te' 
    PILOT_ID = 'pi' 
    POLITITAN_ID = 'po' 
    PROFESSION_CHOICES = (
     (TEACHER_ID, 'Teacher'), 
     (PILOT_ID, 'Pilot'), 
     (POLITITAN_ID, 'Polititian') 
    ) 
    ... 
    profession = db.CharField(max_length=2, choices=PROFESSION_CHOICES) 

然後你就可以查詢所有polititian這樣的:

polititans = Person.objects.filter(profession=Person.POLITITAN_ID)