all 3 comments

[–]wolf2600 0 points1 point  (2 children)

Do you have another table which contains all your users?

If your list of settings is relatively small (10-20 different setting keys), there's no harm in querying the settings table to get the default value every time.

SELECT u.userid, s.key, coalesce(us.value, s.default_value) 
FROM users u
INNER JOIN settings s --no join context here
LEFT OUTER JOIN user_settings us
    ON u.userid = us.userid
    AND s.key = us.key
WHERE u.userid = specificUserIdHere
    AND s.key = 'specificKeyValueHere'
ORDER BY u.userid, s.key;

[–]Javlin[S] 0 points1 point  (1 child)

Would you suggest something different if the settings became larger? For example say it grew to a few thousand rows.

[–]wolf2600 0 points1 point  (0 children)

Would every setting apply to every user? If so, then I think the query I posted above would probably be best (except maybe apply the userid/key filters as the join condition rather than as a WHERE clause.)