My question is there any way to better manage this database implementation.
Background:
Web page worked with mysql, php and javascript.
Assuming the following table (simplified for the discussion):
*user_table*
user_id (PK)
user_name
*thread_table*
thread_id (PK)
user_id (Index)
thread_title
I want a view with a list of the thread titles together with the user publishing them.
So this would be my usual query:
SELECT thread_title, thread_table.user_id, user_name
FROM thread_table INNER JOIN user_table
WHERE user_table.user_id = thread_table.user_id
My question is, would be this the most efficient way (faster) to get the user_name having the user_id?
This is a simplified example, but I have more complex queries, joining different tables and it’s “painful” to have to join another table only to get the user_name.
So what could I do to improve the effectiveness of this execution? At least should I make a memory table of user_table with a hash key to better get the user_name having the user_id?
Thanks!!
This is the correct implementation. If JOINs are painful to you, then you are using the wrong tool! JOINs are an inherent part of SQL, and that fact that you need to use a JOIN to retrieve a specific piece of information does not mean that you should change your schema.
If this is a common pattern that you use over and over in other queries, then it is a perfect candidate for a view. Then you can use that view in other queries, and eliminate some typing.
If performance is really a concern, you can start to look into denomralization, but I am not getting the sense that you are at that stage here. Posting your query times and the output of
EXPLAIN PLANwould help diagnose this further.