有5张桌子
- 用户个人数据(用户_记录=120000)。
- 工作的用户(users_positions_record=150000)。
- 组织(组织_记录=2500)。
- 分区(分区_记录=3500)。
- 职位(职位_记录=4500)。
我想加入这些表,但最好的查询是什么?
我写了这个查询,但它很慢:
SELECT
*
FROM
(SELECT
p.*
FROM
`user_positions` p
LEFT JOIN `organizations` u_o
ON u_o.`id` = p.`org_title`
LEFT JOIN `divisions` u_d
ON u_d.`id` = p.`division`
LEFT JOIN `positions` u_p
ON u_p.`id` = p.`position`
WHERE u_o.`status` = 1
GROUP BY p.`PRN`) u_a
LEFT JOIN `users` u
ON u.`PRN` = u_a.`PRN`
示例 - 简单结构
table users
-- PRN --- FirstName --- Age....
-- 001 --- david --- 24 ....
-- 052 --- george --- 27 ....
table users_positions
-- PRN --- Organization_ID --- Division_ID --- Positionn_ID....
-- 001 --- 1 --- 5 --- 1....
-- 001 --- 2 --- 10 --- 5....
-- 052 --- 1 --- 1 --- 1....
table organizations
-- ID --- Name ....
-- 1 --- test 1 ....
-- 2 --- test 2 ....
table divisions
-- ID --- Name --- Age....
-- 1 --- IT Department ....
-- 5 --- Sale Department ....
-- 10 --- Administration Department ....
table positions
-- ID --- Name --- Age....
-- 1 --- Programmer ....
-- 5 --- Manager ....