Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

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 than more because user need just connect one table but I heard that some programmer doesnt like this method.And Can someone say me which one is better?One very large table or a lot of little tables. and Why I need use a lot tables?Why it is useful?Which one is fast for user? What is the advantages and disadvantages of large table and a lot of little tables?

like image 711
Ibrahim Hasanov Avatar asked Apr 11 '16 12:04

Ibrahim Hasanov


People also ask

Why is it better to have multiple separate tables?

In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

Is it better to have multiple tables?

(a) if different people are going to be developing applications involving different tables, it makes sense to split them. (b) If you want to give different kind of authorities to different people for different part of the data collection, it may be more convenient to split them.

How many columns in a table is too many?

There isn't a number that's too many. That said, it sounds like, based on your description, that you're not dealing with a properly structured table. 180 columns to define a user and 280 columns to define a thing... That can't possibly be normalized or a fact table.

What is considered a large database table?

"Large" is like "smart" - it's relative. 10 million rows is a good size, but whether the table is large depends on a number of factors: how many columns and what are their data types?


1 Answers

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.

like image 170
CathalMF Avatar answered Nov 12 '22 03:11

CathalMF