我正在尝试为网站设计数据库结构,以便人们打开个人资料。 我试图弄清楚我选择的设计是否足够好。我怀疑它可能有问题的原因是我在许多表之间使用了很多关系。这样,当一个人的页面被加载时,幕后有许多JOIN,这肯定会造成瓶颈。如果你能帮助我弄清楚设计是否合适或我是否应该重新考虑它,我将不胜感激。
所以我想出了以下设计:
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)
可以规范化数据。这就是关系数据库的用途!你的设计看起来很好。如果它成为一个问题,你总是可以缓存页面渲染。
答案 1 :(得分: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)
然后你可以像这样查询所有的政治家:
polititans = Person.objects.filter(profession=Person.POLITITAN_ID)