我想多次使用子查询(它采用不同的输入)并与父查询连接。问题是,查询需要几分钟才能执行。这是优化后续查询的最佳方法。
SELECT A.PROJECT_ID,A.PROJECT_BASIC_INFORMATION_ID,B.VALUE AS BU_START_DATE,C.VALUE AS BU_DURATION,D.VALUE AS BU_AMOUNT,E.VALUE AS PLUG FROM
PROJECT_BASIC_INFORMATION A,
CUSTOM_ATTRIBUTES_VALUES B,
CUSTOM_ATTRIBUTES_VALUES C,
CUSTOM_ATTRIBUTES_VALUES D,
CUSTOM_ATTRIBUTES_VALUES E
WHERE
A.TENANT_ID = '100' AND
B.MAP_ID = (SELECT MST_ATTRIBUTE_ID FROM `MST_TENANT_CUSTOM_ATTRIBUTES`
WHERE LABEL='Budget Project Savings Start Date' AND
TENANT_ID='100') AND
C.MAP_ID = (SELECT MST_ATTRIBUTE_ID FROM `MST_TENANT_CUSTOM_ATTRIBUTES`
WHERE LABEL='Budget Savings Duration' AND
TENANT_ID='100') AND
D.MAP_ID = (SELECT MST_ATTRIBUTE_ID FROM `MST_TENANT_CUSTOM_ATTRIBUTES`
WHERE LABEL='Budget Annualized Savings' AND
TENANT_ID='100') AND
E.MAP_ID = (SELECT MST_ATTRIBUTE_ID FROM `MST_TENANT_CUSTOM_ATTRIBUTES`
WHERE LABEL='Plug' AND
TENANT_ID='100')
GROUP BY A.PROJECT_BASIC_INFORMATION_ID ORDER BY A.PROJECT_ID
感谢。
答案 0 :(得分:0)
您正在使用master_attribute_id表中的所有子查询自杀。我已经改变了做JOIN的查询。从原始的“A”别名开始,然后对同一个“A.TENANT_ID”(非固定值)的每个属性表进行JOIN,但另外每个实例另外AND ... LABEL ='whatever'。因此,每个联接都按照相同的租户和标签获取自定义属性。现在,从每个中,通过MAP_ID连接到属性表。所以我的别名与“CAV”(custom_Addtribute_Values)表以及A,B,C,D相关联。因此,您只需要最外层WHERE子句中的实际租户ID。
现在,为了帮助确保查询的优化,你的MST_TENANT_CUSTOM_ATTRIBUTES表,我将有一个覆盖索引(tenant_id,label,map_id)。对于Custom_Attributes_Values表,我会有一个索引(map_id,value)
SELECT
A.PROJECT_ID,
A.PROJECT_BASIC_INFORMATION_ID,
CAV_A.VALUE AS BU_START_DATE,
CAV_B.VALUE AS BU_DURATION,
CAV_C.VALUE AS BU_AMOUNT,
E.VALUE AS PLUG
FROM
PROJECT_BASIC_INFORMATION A
JOIN MST_TENANT_CUSTOM_ATTRIBUTES MT_A
ON A.TENANT_ID = MT_A.TENANT_ID
AND MT_A.LABEL = 'Budget Project Savings Start Date'
JOIN CUSTOM_ATTRIBUTES_VALUES A
ON MT_A.MAP_ID = CAV_A.MAP_ID
JOIN MST_TENANT_CUSTOM_ATTRIBUTES MT_B
ON A.TENANT_ID = MT_B.TENANT_ID
AND MA_B.LABEL = 'Budget Savings Duration'
JOIN CUSTOM_ATTRIBUTES_VALUES B
ON MT_B.MAP_ID = CAV_B.MAP_ID
JOIN MST_TENANT_CUSTOM_ATTRIBUTES MT_C
ON A.TENANT_ID = MT_C.TENANT_ID
AND MA_C.LABEL = 'Budget Annualized Savings'
JOIN CUSTOM_ATTRIBUTES_VALUES C
ON MT_C.MAP_ID = CAV_C.MAP_ID
JOIN MST_TENANT_CUSTOM_ATTRIBUTES MT_D
ON A.TENANT_ID = MT_D.TENANT_ID
AND MA_D.LABEL = 'Plug'
JOIN CUSTOM_ATTRIBUTES_VALUES D
ON MT_D.MAP_ID = CAV_D.MAP_ID
WHERE
A.TENANT_ID = 100
GROUP BY
A.PROJECT_BASIC_INFORMATION_ID
ORDER BY
A.PROJECT_ID
答案 1 :(得分:0)
这应该是可能的。不幸的是,没有检查它以确保它是否有效。
SELECT
A.PROJECT_ID,
A.PROJECT_BASIC_INFORMATION_ID,
IF(MT_A.LABEL='Budget Project Savings Start Date',CAV.VALUE,NULL) AS BU_START_DATE,
IF(MT_A.LABEL='Budget Savings Duration',CAV.VALUE,NULL) AS BU_DURATION,
IF(MT_A.LABEL='Budget Annualized Savings',CAV.VALUE,NULL) AS BU_AMOUNT,
IF(MT_A.LABEL='Plug',CAV.VALUE,NULL) AS PLUG,
FROM
PROJECT_BASIC_INFORMATION A
JOIN MST_TENANT_CUSTOM_ATTRIBUTES MT_A
ON A.TENANT_ID = MT_A.TENANT_ID
AND MT_A.LABEL in ('Budget Project Savings Start Date',
'Budget Savings Duration',
'Budget Annualized Savings',
'Plug')
JOIN CUSTOM_ATTRIBUTES_VALUES CAV
ON MT_A.MAP_ID = CAV.MAP_ID
WHERE
A.TENANT_ID = 100
GROUP BY
A.PROJECT_ID,A.PROJECT_BASIC_INFORMATION_ID
ORDER BY
A.PROJECT_ID