2

I have to deal with the following schema (simplified):

CREATE TABLE user
    (`id` int,
     `name` varchar(255),
     PRIMARY KEY (id));

CREATE TABLE user_extra
    (`user_id` int,
     `user_extra_name` varchar(64),
     `value` varchar(255),
     UNIQUE INDEX user_id_extra_name (user_id, user_extra_name));

INSERT INTO user
    (`id`, `name`)
VALUES
    (1, 'John Doe'),
    (2, 'Don Joe');

INSERT INTO user_extra
  (`user_id`, `user_extra_name`, `value`)
VALUES
    (1, 'phone', '1234444'),
    (1, 'email', 'doe@example.com'),
    (2, 'email', 'joe@example.com'),
    (1, 'nickname', 'johnny'),
    (2, 'nickname', 'donny');

I want to get the following data in the most efficient way possible:

id  name        phone   email               nickname
1   John Doe    1234444 doe@example.com     johnny
2   Don Joe     (null)  joe@example.com     donny

There will be many users and potentially more extra entries, besides phone, email and nickname.

What I do so far is multiple left joins on user_extra:

SELECT
  u.id, u.name,
  uephone.value AS phone,
  ueemail.value AS email,
  uenick.value AS nickname
FROM
  user u
LEFT JOIN
  user_extra uephone ON
    uephone.user_id = u.id AND
    uephone.user_extra_name = 'phone'
LEFT JOIN
  user_extra ueemail ON
    ueemail.user_id = u.id AND
    ueemail.user_extra_name = 'email'
LEFT JOIN
  user_extra uenick ON
    uenick.user_id = u.id AND
    uenick.user_extra_name = 'nickname'

Is there a better way to approach this problem? Is this liable to cause performance issues as the number of users grow or if there would be, say, a dozen such left joins?

SQLFiddle

1
  • Are you using it like this and does it cause you problems at the moment? If not, there is not necessarily a need for premature optimization - it may be better to keep a slightly less performant but also less complex solution over a 'smarter' one if that doesn't cause a performance problem. May 27, 2016 at 2:51

2 Answers 2

1

Since you published simplified tables schema (with no index) I can not really say if your query has any performance issue. Probably if user.id is primary key and user_extra.user_id is indexed as well your query is pretty good.

And if you will add complex index to user_extra against (user_id,user_extra_name) that could be even better.

But for small tables and just to simplify reading this query here is another approach:

http://sqlfiddle.com/#!9/0ab56b/20

SELECT
  u.id, u.name,
  ue.phone,
  ue.email,
  ue.nickname
FROM
  user u
LEFT JOIN (
  SELECT 
    user_id,
    MAX(IF(user_extra_name = 'phone',`value`,null)) AS phone,
    MAX(IF(user_extra_name = 'email',`value`,null)) AS email,
    MAX(IF(user_extra_name = 'nickname',`value`,null)) AS nickname
  FROM user_extra
  GROUP BY user_id
  ) ue
ON
    ue.user_id = u.id 

But really, if you have indexes on your table I believe your query is good.

1
  • 1
    I checked and the existing schema indeed has a primary key on the user table and a complex index on user_extra, just like it should. Thanks for your advice/reassurance :)
    – lampyridae
    May 27, 2016 at 3:55
1

The question boils down to you - do you notice any bottlenecks in your application due to this snippet of SQL?

Alex's answer is a great way to go, but if you feel like using CASE's, then a solution like this could suit you well! It only requires one LEFT JOIN:

SELECT
  u.id, u.name,
  (CASE WHEN uextra.user_extra_name = 'phone' THEN uextra.value ELSE null END) AS phone, 
  (CASE WHEN uextra.user_extra_name = 'email' THEN uextra.value ELSE null END) AS email, 
  (CASE WHEN uextra.user_extra_name = 'nickname' THEN uextra.value ELSE null END) AS nickname
FROM
  user u
LEFT JOIN
  user_extra AS uextra
ON
  uextra.user_id = u.id
GROUP BY
  u.name
ORDER BY
  u.id
ASC

http://sqlfiddle.com/#!9/0ab56b/28

Not knowing much about your index's either.

1
  • This would be beautiful, but each result row only seems to pick up the value of the first user extra field :(
    – lampyridae
    May 27, 2016 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.