Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database efficiency - table per user vs. table of users

For a website having users. Each user having the ability to create any amount of, we'll call it "posts":

Efficiency-wise - is it better to create one table for all of the posts, saving the user-id of the user which created the post, for each post - OR creating a different separate table for each user and putting there just the posts created by that user?

like image 205
Yuval A. Avatar asked Sep 25 '11 08:09

Yuval A.


People also ask

What are the 3 table relationships in a database?

There are three types of relationships between the data you are likely to encounter at this stage in the design: one-to-one, one-to-many, and many-to-many. To be able to identify these relationships, you need to examine the data and have an understanding of what business rules apply to the data and tables.

What are the advantages of using multiple related tables to store data?

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.

What should be in a user table?

Typically, a user table contains data about your company's customers, prospects, or products. For example, a user table might contain columns for customer account data such as Account ID, Account Type, and Balance.


4 Answers

The database layout should not change when you add more data to it, so the user data should definitely be in one table.

Also:

  • Having multiple tables means that you have to create queries dynamically.

  • The cached query plan for one table won't be used for any other of the tables.

  • Having a lot of data in one table doesn't affect performance much, but having a lot of tables does.

  • If you want to add an index to the table to make queries faster, it's a lot easier to do on a single table.

like image 150
Guffa Avatar answered Sep 22 '22 23:09

Guffa


Well to answer the specific question: In terms of efficiency of querying, it will always be better to have small tables, hence a table per user is likely to be the most efficient.

However, unless you have a lot of posts and users, this is not likely to matter. Even with millions of rows, you will get good performance with a well-placed index.

I would strongly advise against the table-per-user strategy, because it adds a lot of complexity to your solution. How would you query when you need to find, say, users that have posted on a subject within the year ?

Optimize when you need to. Not because you think/are afraid something will be slow. (And even if you need to optimize, there will be easier options than table-per-user)

like image 23
driis Avatar answered Sep 21 '22 23:09

driis


Schemas with a varying number of tables are, generally, bad. Use one single table for your posts.

like image 28
Mat Avatar answered Sep 21 '22 23:09

Mat


If performance is a concern, you should learn about database indexes. While indexes is not part of the SQL standard, nearly all databases support them to help improve performance.

I recommend that you create a single table for all users' posts and then add an indexes to this table to improve the performance of searching. For example you can add an index on the user column so that you can quickly find all posts for a given user. You may also want to consider adding other indexes, depending on your application's requirements.

like image 45
Mark Byers Avatar answered Sep 19 '22 23:09

Mark Byers