我有一个 MySQL 查询,它从不同的表中挑选不同的数据来构建报告。例子:
SELECT
u.id,
u.first_name,
u.last_name,
(select count(*) from monkeys where owner_id = u.id) as pet_monkeys,
((select count(*) from speeding_tickets where owner_id = u.id) + (select count(*) from parking_tickets where owner_id = u.id)) as moving_violations,
FROM
user as u
WHERE
u.id = 12345
在实际代码中,大约有 20 个子选择从不同的表中提取统计信息。这个查询也非常缓慢。
有没有更好的方法来使用 JOIN 或 UNION 或其他东西来组织上述查询?