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

后端 未结 3 1791
萌比男神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:55

    100 columns in a single table is bad design in most situations.

    Read this page: http://www.tutorialspoint.com/sql/sql-rdbms-concepts.htm

    Break your data up into related chunks and give each of them their own table.

    You said you have this information (users,comments,videos,photos,messages) so you should have something like these tables.

    1. Users which contains (User ID, Name, Email etc)
    2. Comments which contains (Comment ID, User ID, Comment Text etc)
    3. Videos which contains (Video ID, User ID, Comment ID, Video Data etc)
    4. Photos which contains (Photo ID, User ID, Comment ID, Photo Data etc)
    5. Messages which contains (Message ID, User ID, Message Text etc)

    Then when your writing your SQL you can write proper SQL to query based on exactly what information you need.

    SELECT UserID, MessageID, MessageText
    FROM Users as USR
        JOIN Messages as MSG
            on USR.UserID = MSG.UserID
    WHERE USR.UserID = 1234567
    

    With your current query your having to deal with rows containing data that you dont need or care about.

    EDIT Just to give some further information to the OP as to why this is better design.

    Lets take the "Users" as a starting example.

    In a proper database design you would have a table called Users which has all the required columns that are required for a user to exist. Username, email, id number etc.

    Now we want to create a new user so we want to insert Username, email and id number. But wait i still have to populate 97 other columns with totally unrelated information to our process of creating a new user! Even if you store NULL in all columns its going to use some space in the database.

    Also imagine you have hundreds of users all trying to select, update and delete from a single database table. There is a high chance of the table being locked. But if you had one user updating the Users table, another user Inserting into the Messages table then the work is spread out.

    And as other users have said, purely performance. The database needs to get all information and filter out what you want. If you have alot of columns this is unnecessary work.

    Performance Example.

    Lets say your database has been running for years. You have 5000 users, 2,000,000 comments, 300,000 pictures, 1,000,000 messages. Your single table now contains 3,305,000 records.

    Now you want to find a User with the ID of 12345 who has more than 20 pictures. You need to search through all 3,305,000 records to get this result.

    If you had a split table design then you would only need to search through 305,000 records.

    Obvious performance gain!!

    EDIT 2

    Performance TEST.

    I created a dummy table containing 2 million rows and 1 column. I ran the below query which took 120ms on average over 10 executions.

    SELECT MyDate1 from dbo.DummyTable where MyDate1 BETWEEN '2015-02-15 16:59:00.000' and '2015-02-15 16:59:59.000'
    

    I then truncated the table and created 6 more columns and populated them with 2 million rows of test data and ran the same query. It took 210ms on average over 10 executions.

    So adding more columns decreases performance even though your not viewing the extra data.

提交回复
热议问题