【发布时间】:2010-11-30 13:20:20
【问题描述】:
我正在玩弄两种模式,但我无法决定哪种模式更具可扩展性。该模式用于问答,它内置在 MySQL 中。人们发布问题/答案并喜欢/不喜欢/最喜欢的问题和答案。一个问题可以有很多答案/喜欢/不喜欢,一个答案也可以。
要向用户阅读问题,两种模式都需要相同数量的连接,但连接的处理方式不同:
架构 1
questions(id, title, body, userId)
questionLikes(id, questionId, userId)
questionDislikes(id, questionId, userId)
quetionComments(id, questionId, body, userId)
answers(id, questionId, body, userId)
answerLikes(id, answerId, userId)
answerDislikes(id, answerId, userId)
answerComments(id, answerId, userId, body)
favourites(id, questionId, userId)
这更规范化,更容易开发,但可扩展?似乎有很多重复的信息。抓取问题的连接序列是针对用户的(我们希望包括他的喜欢/不喜欢活动)
select question
join answers
join questionLikes
join questionDislikes
join questionComments
join favouites
join answers to answerLikes
join answers to answerDislikes
join answers to answerComments (multiply answer joins by number of answers)
架构 2
posts(id, postTypeId, userId, title, body)
postTypeId(id, postType)
comments(id, postId, userId)
votes(id, voteTypeId, userId)
voteTypeId(id, voteType)
这不太规范化和紧凑,似乎可以更好地扩展,自联接和其他开发问题(条件验证)会让人头疼。抓取问题的连接序列是
select question and its answers in the same read using where @id for question, and @questionId for answers; each row, join the following:
join votes on as likes on voteType 1
join votes as dislikes on votetype 2
join comments
join favouites (multiply joins by number of rows)
那么什么会更好地扩展?我知道可以添加一些额外的字段来存储计数,因此不需要连接。但是两者都需要相同数量的连接,我无法下定决心。
【问题讨论】:
-
我在阅读您的问题时并没有走得太远,但是为什么您会有 2 个不同的表用于 questionLikes 和 questionDislikes ???我想同样的评论可以进一步应用于你的架构。
-
因为问题和答案可能具有相同的 ID,因为它们是不同的对象。