Re: Reg table design and query performances
Hi,
Then I suggest you merge the subqueries into the main query. I do not see that they serve any purpose. In other words:
select a.user_id, a.value as department, b.value as role, c.value as location
from attribute_value a join attribute_value b on a.user_id=b.user_id
join attribute_value b on a.user_id=c.user_id
where a.name='department' and a.value='Sales'
and b.name='role' and b.value!='broker'
and c.name='location' and c.value='Seattle';
MySQL 5.7 will do this rewrite automatically.
For the given query, an index on (value, name) is probably be better since then all brokers can easily be skipped.
Hope this helps,
Øystein Grøvlen,
Senior Principal Software Engineer,
MySQL Group, Oracle,
Trondheim, Norway