The following query works, but it is very slow for 10 records (2 seconds). Profiling says creating a tmp table, but I'm not sure why.
Basically, I join the current user, acl groups, to get all the groups they are in, then join the groups to companies to get all the companies they are in, and then join the companies to orders to get all orders.
If I delete this line
ORDER BY orders.created_on DESC
then the request is executed in 0.06 s (more than acceptable).
Help, any ideas for optimization? Many thanks:)
SELECT
orders.uuid,
companies.name as company_name
FROM
users u
JOIN
users_acl_groups g on u.uuid = g.user_uuid
JOIN
users_acl acl on (acl.user_uuid = u.uuid or acl.group_uuid = g.group_uuid)
JOIN
companies on acl.item_uuid = companies.uuid
JOIN
orders on companies.uuid = orders.company_uuid
WHERE
u.uuid = 'DDEC8073-5056-C000-01ED583A51CBCA32' and orders.status <> ''
ORDER BY orders.created_on DESC
limit 0, 10;
UPDATE, request explanation.
1 SIMPLE orders ALL 9403 Use temporary; Using filesort
1 SIMPLE acl ALL 1859 Use where; Using the connection buffer
1 SIMPLE g ALL 2005 ;
1 eq_ref 52 table.orders.company_uuid 1
1 SIMPLE u ALL 33595 ; ;