I have the next tables
Users {id, name}
Messages {id, user_id, cache_user_name}
What I want is to do a JOIN only when cache_user_name is NULL for performance reasons.
For example:
SELECT Messages.*, Users.name FROM Messages INNER JOIN Users ON (Messages.user_id = Users.id)
// ON (ISNULL(Messages.cache_user_name) AND ...
The best way is doing 2 queries? 1 for rows without cache (join) and the other for cached rows with a join?
[EDIT]
The result I need is:
Users
ID: 1, NAME: Wiliam
Messages
ID: 1, USER_ID: 1, CACHE_USER_NAME: Wiliam
ID: 2, USER_ID: 1, CACHE_USER_NAME: null
Result
ID: 1, USER_ID: 1, CACHE_USER_NAME: Wiliam, USERS.NAME: null // No join, faster
ID: 2, USER_ID: 1, CACHE_USER_NAME: null, USERS.NAME: Wiliam // Join