a Large one table with 100 column vs a lot of little tables

后端 未结 3 1788
萌比男神i
萌比男神i 2021-02-19 22:36

I created some website which contain users,comments,videos,photos,messages and more.All of the data is in the one table which contain 100 column.I thought one table is better th

3条回答
  •  清歌不尽
    2021-02-19 22:53

    For your situation it is better to have multiple tables. The reason for this is because if you put all your data into one table then you will have update anomalies. For example, if a user decides to update his username, you will have to update every single row in your big table that has that user's username. But if you split it into multiple tables then you will only need to update one row in your User table and all the rows in your other tables will reference that updated row.

    As far as speed, having one table will be faster than multiple tables with SELECT statements because joining tables is slow. INSERT statements will be about the same speed in either situation because you will be inserting one row. However, updating someone's username with an UPDATE statement will be very slow with one table if they have a lot of data about them because it has to go through each row and update every one of them as opposed to only having to update one row in the User table.

    So, you should create tables for everything you mentioned in your first sentence (users, comments, videos, photos, and messages) and connect them using Ids like this:

    User
    -Id
    -Username
    
    Video
    -Id
    -UploaderId references User.Id
    -VideoUrl
    
    Photo
    -Id
    -UploaderId references User.Id
    -PhotoUrl
    
    VideoComment
    -CommenterId references User.Id
    -VideoId references Video.Id
    -CommentText
    
    PhotoComment
    -CommenterId reference User.Id
    -PhotoId references Photo.Id
    -CommentText
    
    Message
    -SenderId references User.Id
    -ReceiverId references User.Id
    -MessageText
    

提交回复
热议问题