For my university project, I'm developing a dynamic live chat website with rooms, user registration, etc. I've got the entire system planned out bar one aspect. The rooms.
A room is created by a user who is then an operator of that room. Users can join the room and talk within it. The system has to be scalable, accounting for hundreds of thousands if not millions of messages being sent a day.
I was going to create on table in my database called messages
, and have fields like this:
| r_id | u_id | message | timestamp |
r_id
and u_id
would be foreign keys to the room ID and user ID respectively. This would mean I would insert a new record when a user sends a message, and periodically run a SELECT statement for every client (say every 3 seconds or so) to get the recent messages. But because the table will be huge, running these statements might create a lot of overhead and take a long time.
Another way of implementing this would be to create a new database table for every room. Say a user creates 3 rooms called General
, Programming
and Gaming
. The database tables would be room_general
, room_programming
, and room_gaming
, each with fields like:
| u_id | message | timestamp |
This would drastically cut down on the amount of queries for each table, but might introduce problems when I come to program it.
So, what is the best way to do this?
I'm using MySQL with PHP and AJAX.