我对 SQL 很陌生,我需要创建一个数据源,其中包含来自许多不同表的数据。我写了以下脚本。它适用于少量数据,但是当数据量增加时,无法通过此查询获取数据。我该如何改进这个脚本,可能的瓶颈和关键点是什么?提前致谢。
SELECT
dbo.IDW_COSTDETAIL.IDW_PROJECT_FK, dbo.IDW_PROJECT_DATA.CODE + ' - ' +
dbo.IDW_PROJECT_DATA.DESCRIPTION AS PROJECT,
dbo.IDW_PROJECT_ALTERNATIVE.CODE + ' - ' + dbo.IDW_PROJECT_ALTERNATIVE.DESCRIPTION AS PRJ_ALTERNATIVE,
dbo.IDW_COSTDETAIL.BOQ_NO,
dbo.IDW_COSTDETAIL.IDW_CONTROLLING_CODE_FK AS BUDGETCODE,
dbo.IDW_CONTROLLING_CODE.DESCRIPTION AS BC_DESCR,
dbo.IDW_ACTIVITY.CODE AS ACTIVITY_ID,
dbo.IDW_ACTIVITY.DESCRIPTION AS ACTIVITY_DESCR, dbo.IDW_BOQ_DETAIL.REFERENCE,
dbo.IDW_BOQ_DETAIL.OUTLINE_SPEC1, dbo.IDW_BOQ_DETAIL.UOM AS ITEM_UOM,
dbo.IDW_COSTDETAIL.COST_CODE,
dbo.IDW_COSTDETAIL.COMMODITY_CODE,
CASE
WHEN IDW_COSTDETAIL.COMMODITY_CODE IS NULL
THEN IDW_COST_CODE.DESCRIPTION
ELSE IDW_COMMODITIES.DESCRIPTION
END AS SUBITEM_DESCR,
dbo.IDW_BOQ_DETAIL.AQ_QUANTITY / (DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) + 1) AS ITEM_AQ,
dbo.IDW_COSTDETAIL.QUANTITY_BASED_AQ / (DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) + 1) AS SUBITEM_AQ,
CASE
WHEN IDW_COSTDETAIL.COMMODITY_CODE IS NULL
THEN IDW_COST_CODE.UOM
ELSE IDW_COMMODITIES.UOM
END AS SUBITEM_UOM,
dbo.IDW_COSTDETAIL.COST / (DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) + 1) AS UNIT_COST,
CASE
WHEN IDW_COSTDETAIL.COMMODITY_CODE IS NULL
THEN IDW_COST_CODE.CURRENCY
ELSE IDW_COMMODITIES.ESTIMATE_CURRENCY
END AS CATALOG_CUR,
dbo.IDW_BOQ_DETAIL.IQ_QUANTITY / (DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) + 1) AS ITEM_IQ,
dbo.IDW_COSTDETAIL_IQ.QUANTITY / (DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) + 1) AS SUBITEM_IQ,
dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE, DATEDIFF(day, dbo.IDW_ACTIVITY.START_DATE, dbo.IDW_ACTIVITY.END_DATE) AS DURATION,
dbo.SQLCALENDAR.DateKey AS FULLDATE,
dbo.IDW_COST_CODE_HIER_V.CODE + ' - ' + dbo.IDW_COST_CODE_HIER_V.DESCRIPTION AS CODE,
dbo.IDW_COST_CODE_HIER_V.COC_LVL1 + ' - ' + dbo.IDW_COST_CODE_HIER_V.COC_DESC_LVL1 AS COC_LVL1,
dbo.IDW_COST_CODE_HIER_V.COC_LVL2 + ' - ' + dbo.IDW_COST_CODE_HIER_V.COC_DESC_LVL2 AS COC_LVL2,
dbo.IDW_COST_CODE_HIER_V.COC_LVL3 + ' - ' + dbo.IDW_COST_CODE_HIER_V.COC_DESC_LVL3 AS COC_LVL3,
dbo.IDW_COST_CODE_HIER_V.COC_LVL4 + ' - ' + dbo.IDW_COST_CODE_HIER_V.COC_DESC_LVL4 AS COC_LVL4,
dbo.IDW_COST_CODE_HIER_V.COC_LVL5 + ' - ' + dbo.IDW_COST_CODE_HIER_V.COC_DESC_LVL5 AS COC_LVL5,
dbo.IDW_COMMODITIES_HIER_V.CODE + ' - ' + dbo.IDW_COMMODITIES_HIER_V.DESCRIPTION AS COMMODITY,
dbo.IDW_COMMODITIES_HIER_V.COM_LVL1 + ' - ' + dbo.IDW_COMMODITIES_HIER_V.COM_DESC_LVL1 AS COM_LVL1, dbo.IDW_COMMODITIES_HIER_V.COM_LVL2 + ' - ' + dbo.IDW_COMMODITIES_HIER_V.COM_DESC_LVL2 AS COM_LVL2,
dbo.IDW_COMMODITIES_HIER_V.COM_LVL3 + ' - ' + dbo.IDW_COMMODITIES_HIER_V.COM_DESC_LVL3 AS COM_LVL3, dbo.IDW_COMMODITIES_HIER_V.COM_LVL4 + ' - ' + dbo.IDW_COMMODITIES_HIER_V.COM_DESC_LVL4 AS COM_LVL4,
dbo.IDW_COMMODITIES_HIER_V.COM_LVL5 + ' - ' + dbo.IDW_COMMODITIES_HIER_V.COM_DESC_LVL5 AS COM_LVL5
FROM
dbo.IDW_PROJECT_DATA
INNER JOIN
dbo.IDW_ACTIVITY
INNER JOIN
dbo.IDW_COSTDETAIL ON dbo.IDW_ACTIVITY.IDW_PROJECT_FK = dbo.IDW_COSTDETAIL.IDW_PROJECT_FK AND dbo.IDW_ACTIVITY.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_COSTDETAIL.IDW_PROJECT_ALTERNATIVE_FK AND dbo.IDW_ACTIVITY.CODE = dbo.IDW_COSTDETAIL.IDW_ACTIVITY_FK
INNER JOIN
dbo.IDW_CONTROLLING_CODE ON dbo.IDW_COSTDETAIL.IDW_CONTROLLING_CODE_FK = dbo.IDW_CONTROLLING_CODE.CODE AND dbo.IDW_COSTDETAIL.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_CONTROLLING_CODE.IDW_PROJECT_ALTERNATIVE_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_CONTROLLING_CODE.IDW_PROJECT_FK ON dbo.IDW_PROJECT_DATA.IDW_PROJECT_FK = dbo.IDW_ACTIVITY.IDW_PROJECT_FK
INNER JOIN
dbo.IDW_PROJECT_ALTERNATIVE ON dbo.IDW_PROJECT_DATA.IDW_PROJECT_FK = dbo.IDW_PROJECT_ALTERNATIVE.IDW_PROJECT_FK AND dbo.IDW_ACTIVITY.IDW_PROJECT_FK = dbo.IDW_PROJECT_ALTERNATIVE.IDW_PROJECT_FK AND dbo.IDW_ACTIVITY.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_PROJECT_ALTERNATIVE.CODE AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_PROJECT_ALTERNATIVE.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_PROJECT_ALTERNATIVE.CODE AND dbo.IDW_CONTROLLING_CODE.IDW_PROJECT_FK = dbo.IDW_PROJECT_ALTERNATIVE.IDW_PROJECT_FK AND dbo.IDW_CONTROLLING_CODE.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_PROJECT_ALTERNATIVE.CODE
RIGHT OUTER JOIN
dbo.IDW_BOQ_DETAIL ON dbo.IDW_PROJECT_ALTERNATIVE.CODE = dbo.IDW_BOQ_DETAIL.IDW_PROJECT_ALTERNATIVE_FK AND dbo.IDW_PROJECT_ALTERNATIVE.IDW_PROJECT_FK = dbo.IDW_BOQ_DETAIL.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.ITEM_REFERENCE = dbo.IDW_BOQ_DETAIL.REFERENCE AND dbo.IDW_COSTDETAIL.BOQ_NO = dbo.IDW_BOQ_DETAIL.IDW_BOQ_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_BOQ_DETAIL.IDW_PROJECT_FK
LEFT OUTER JOIN
dbo.IDW_COST_CODE ON dbo.IDW_PROJECT_DATA.IDW_PROJECT_FK = dbo.IDW_COST_CODE.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_COST_CODE.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.COST_CODE = dbo.IDW_COST_CODE.CODE
LEFT OUTER JOIN
dbo.IDW_COMMODITIES ON dbo.IDW_PROJECT_DATA.IDW_PROJECT_FK = dbo.IDW_COMMODITIES.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_COMMODITIES.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.COMMODITY_CODE = dbo.IDW_COMMODITIES.CODE
LEFT OUTER JOIN
dbo.IDW_COST_CODE_HIER_V ON dbo.IDW_COSTDETAIL.COST_CODE = dbo.IDW_COST_CODE_HIER_V.CODE AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_COST_CODE_HIER_V.IDW_PROJECT_FK
LEFT OUTER JOIN
dbo.IDW_COMMODITIES_HIER_V ON dbo.IDW_COSTDETAIL.COMMODITY_CODE = dbo.IDW_COMMODITIES_HIER_V.CODE AND dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_COMMODITIES_HIER_V.IDW_PROJECT_FK
LEFT OUTER JOIN
dbo.IDW_COSTDETAIL_IQ ON dbo.IDW_COSTDETAIL.IDW_PROJECT_FK = dbo.IDW_COSTDETAIL_IQ.IDW_PROJECT_FK AND dbo.IDW_COSTDETAIL.IDW_PROJECT_ALTERNATIVE_FK = dbo.IDW_COSTDETAIL_IQ.IDW_PROJECT_ALTERNATIVE_FK AND dbo.IDW_COSTDETAIL.ID = dbo.IDW_COSTDETAIL_IQ.IDW_COSTDETAIL_FK
CROSS JOIN
dbo.SQLCALENDAR
WHERE
(dbo.IDW_BOQ_DETAIL.ITEM_KIND = 2)
AND (dbo.SQLCALENDAR.DateKey BETWEEN CONVERT(CHAR(8), dbo.IDW_ACTIVITY.START_DATE, 112) AND CONVERT(CHAR(8), dbo.IDW_ACTIVITY.END_DATE, 112))