2010-11-30 89 views
0

我玩弄兩個模式,我不能決定哪個更具可擴展性。該模式適用於Q & A,並且它是在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) 

那麼什麼樣的縮放比較好?我知道可以添加一些額外的字段來存儲計數,因此不需要連接。但都需要相同數量的連接,我不能下定決心。

+0

我在閱讀你的問題時並沒有走得太遠,但爲什麼你會爲questionLikes和questionDislikes有兩個不同的表?並且我猜想相同的評論可以進一步應用於你的模式。 – 2010-11-30 13:44:22

+0

由於問題和答案可能具有相同的ID,因爲它們是不同的對象。 – Mohamad 2010-11-30 13:55:08

回答

1

我會走得比2更遠。問題是,你的模型中有什麼實體?答:用戶和帖子。帖子可以是問題,答案,投票,評論或其他內容,但它總是一個帖子。因此

posts(id, postTypeId, userId, title, body) 
postTypeId(id, postType) 

順便說一句,無論你提到的選擇的檢索一切(還是他們只是爲了顯示最壞的加入?)。

我會看不到我自己取他的問題他的回答他的意見 ...所有一氣呵成。哪個用例需要這樣的一切?

相關問題