SELECT
p.sid,
p.name,
p.domain,
p.timeZone,
p.isactived AS isActived,
p.createtime AS createTime,
p.updateTime,
p.lp_enabled AS lpEnabled,
p.ab_enabled AS abEnabled,
p.ac_enabled AS acEnabled,
p.pt_cross_domain_setting AS ptCrossDomainSetting,
p.status,
ap.type,
p.is_np_package AS isNpPackage,
p.is_virtual AS isVirtual,
p.web_type AS webType,
p.web_target AS webTarget,
p.need_function AS needFunction,
p.is_union_config AS isUnionConfig,
ap.type AS userRole,
(
CASE
ap.type
WHEN '0' THEN a.email
ELSE (
SELECT
a1.email
FROM
account_profile ap1
LEFT JOIN account a1 ON a1.id = ap1.aid
WHERE
ap1.sid = p.sid
AND ap1.type = '0'
LIMIT
1
)
END
) AS ownerEmail,
p.additional_id AS additionalId
FROM
account_profile ap
LEFT JOIN account a ON a.id = ap.aid
INNER JOIN profile p ON ap.sid = p.sid
WHERE
ap.aid = '7b71d2aa-dc0d-4179-96c0-27330587fb50'
AND p.status != '-1'
AND ap.status = '1';
# 查询耗时
300 rows in set (1.44 sec)
WITH OwnerEmails AS (
SELECT
ap1.sid,
a1.email
FROM
account_profile ap1
LEFT JOIN account a1 ON a1.id = ap1.aid
WHERE
ap1.type = ‘0’
)
SELECT
p.sid,
p.name,
p.domain,
p.timeZone,
p.isactived AS isActived,
p.createtime AS createTime,
p.updateTime,
p.lp_enabled AS lpEnabled,
p.ab_enabled AS abEnabled,
p.ac_enabled AS acEnabled,
p.pt_cross_domain_setting AS ptCrossDomainSetting,
p.status,
ap.type,
p.is_np_package AS isNpPackage,
p.is_virtual AS isVirtual,
p.web_type AS webType,
p.web_target AS webTarget,
p.need_function AS needFunction,
p.is_union_config AS isUnionConfig,
ap.type AS userRole,
COALESCE(
CASE
WHEN ap.type = ‘0’ THEN a.email
END,
oe.email
) AS ownerEmail,
p.additional_id AS additionalId
FROM
account_profile ap
LEFT JOIN account a ON a.id = ap.aid
INNER JOIN profile p ON ap.sid = p.sid
LEFT JOIN OwnerEmails oe ON oe.sid = p.sid
WHERE
ap.aid = ‘7b71d2aa-dc0d-4179-96c0-27330587fb50’
AND p.status != ‘-1’
AND ap.status = ‘1’;
...
p.is_union_config AS isUnionConfig,
CASE
WHEN ap.type = '0' THEN a.email
ELSE a_owner.email
END AS ownerEmail,
p.additional_id AS additionalId
FROM
account_profile ap
LEFT JOIN account a ON a.id = ap.aid -- 当前用户的 email
INNER JOIN profile p ON ap.sid = p.sid
LEFT JOIN account_profile ap_owner ON ap_owner.sid = p.sid AND ap_owner.type = '0' -- 获取主账户的 profile
LEFT JOIN account a_owner ON a_owner.id = ap_owner.aid -- 获取主账户的 email
WHERE
ap.aid = '7b71d2aa-dc0d-4179-96c0-27330587fb50'
AND p.status != '-1'
AND ap.status = '1';
SELECT
p.sid,
p.name,
p.domain,
p.timeZone,
p.isactived AS isActived,
p.createtime AS createTime,
p.updateTime,
p.lp_enabled AS lpEnabled,
p.ab_enabled AS abEnabled,
p.ac_enabled AS acEnabled,
p.pt_cross_domain_setting AS ptCrossDomainSetting,
p.status,
ap.type,
p.is_np_package AS isNpPackage,
p.is_virtual AS isVirtual,
p.web_type AS webType,
p.web_target AS webTarget,
p.need_function AS needFunction,
p.is_union_config AS isUnionConfig,
COALESCE(
CASE
WHEN ap.type = ‘0’ THEN a.email
ELSE NULL
END,
a1.email
) AS ownerEmail,
p.additional_id AS additionalId
FROM
account_profile ap
LEFT JOIN account a ON a.id = ap.aid
INNER JOIN profile p ON ap.sid = p.sid
LEFT JOIN (
SELECT
ap1.sid,
a1.email
FROM
account_profile ap1
LEFT JOIN account a1 ON a1.id = ap1.aid
WHERE
ap1.type = ‘0’
) a1 ON a1.sid = p.sid
WHERE
ap.aid = ‘7b71d2aa-dc0d-4179-96c0-27330587fb50’
AND p.status != ‘-1’
AND ap.status = ‘1’;
试下一下改写后的sql。