I'm designing a messaging application for a website.
I'm at a cross roads as to how to design the main message table, which will include id's for both the sender and receiver.
The main user table includes both a unique ID (aka '12345') and a username (aka 'johndoe123'). Attempting normalization, I'm undecided whether to base the messages on the ID, Username, or both.
With the current structure, the ID is the primary key of the user table, and the username is a unique, permananent varchar value. After the creation of a user, both the username and ID will not change.
If I use the user ID in the message table, anytime a record is called/searched/selected a subquery or join will need to be ran to fetch the related username of the user ID. So if I want to select all 'inbox' records for a user, those inbox message records will only have the unique ID's of those members that sent that user a message. If there are 1,000 messages, all from different users, the join or additional queries will need to fetch each username for each of the 1,000 user Id's of each sender. 1,000 queries. That's alot of extra load.
I'm thinking it would probably be best to include both the unique ID AND the unique username to prevent having to use more than just a single query to fetch message records. I know this isn't perfect normalization, but I think I'd rather err on the side of performance vs. a fully normalized table structure.
Seem logical?
I'm at a cross roads as to how to design the main message table, which will include id's for both the sender and receiver.
The main user table includes both a unique ID (aka '12345') and a username (aka 'johndoe123'). Attempting normalization, I'm undecided whether to base the messages on the ID, Username, or both.
With the current structure, the ID is the primary key of the user table, and the username is a unique, permananent varchar value. After the creation of a user, both the username and ID will not change.
If I use the user ID in the message table, anytime a record is called/searched/selected a subquery or join will need to be ran to fetch the related username of the user ID. So if I want to select all 'inbox' records for a user, those inbox message records will only have the unique ID's of those members that sent that user a message. If there are 1,000 messages, all from different users, the join or additional queries will need to fetch each username for each of the 1,000 user Id's of each sender. 1,000 queries. That's alot of extra load.
I'm thinking it would probably be best to include both the unique ID AND the unique username to prevent having to use more than just a single query to fetch message records. I know this isn't perfect normalization, but I think I'd rather err on the side of performance vs. a fully normalized table structure.
Seem logical?