I’m building a social network, and now I faced a problem.
So, which one is faster (to keep messages):
To have one database,
and to create new table (for messages) per new user?
Like this:
CREATE DATABASE 'user_messages';
CREATE TABLE 'user_id' (
id int(32) NOT NULL PRIMARY KEY,
new ENUM ('Y', 'N') NOT NULL DEFAULT 'Y',
time timestamp NOT NULL,
from_id int(32)
);
OR,
To keep all messages in one single table (with replication)???
(Using INDEXes)
What if there’re billion rows?
Like this:
INSERT INTO 'user_messages' (id, new, time, from_id) VALUES ('id_value', 'Y', now(), 'friend_id');
Creating one table per user will become a nightmare to query without using dynamicly generated SQL all the time.
A far better option is to create one single table and store all messages for all users in that table with a foreign key back to the users table. The foreign key will be indexed, and should not have a massive performance problem. If you thing you are going to have billions of rows (or messages), then your database archtecture should be scaled accordingly to handle that quantity of data, but you’re database design shouldn’t be changed because of this.