下面的插入查询需要 7 秒才能运行,并且由于它在一个循环中运行多次,所以时间加起来了。我需要帮助来加快速度,90% 的成本用于聚集索引插入,10% 用于 FN_qryPSLA() 的表扫描。
INSERT INTO tblTPS (fldPK, fldDTA, fldCI, fldMN, fldMDN, fldIQ, fldSD, fldNDS, fldNIN, fldNL, fldMin, fldMax, fldUNC, fldAVA, fldBA)
SELECT fldPK, fldDTA, fldCI, fldMN, fldMDN, fldIQ, fldSD, fldNDS, fldNIN, fldNL, fldMin, fldMax, fldUNC, fldAVA, 1 AS fldBA
FROM FN_qryPSLA()
WHERE (((fldPK)= 37923 ) AND ((fldMN)<>-254));
以下是 FN_qryPSLA()... Distinct Sort 上的成本为 85%(如果删除,成本为 80% 哈希匹配(聚合))
FUNCTION [dbo].[FN_qryPSLA](@PK INT)
RETURNS @tmpTblPSLA TABLE (
fldPK BIGINT NOT NULL,
fldDTA DATETIME NULL,
fldCI FLOAT NULL,
fldMN FLOAT NULL,
fldMDN FLOAT NULL,
fldIQ FLOAT NULL,
fldSD FLOAT NULL,
fldNDS BIGINT NULL,
fldNIN BIGINT NULL,
fldNL BIGINT NULL,
fldMin FLOAT NULL,
fldMax FLOAT NULL,
fldUNC VARCHAR(5) NULL,
fldAVA TINYINT NULL
)
AS
BEGIN
WITH gDPS AS
(SELECT fldPK, Max(fldDTA) AS fldDTA
FROM tblAPS
GROUP BY fldPK)
SELECT fldPK, fldDTA, fldCI, fldMN, fldMDN, fldIQ, fldSD, fldNDS, fldNIN, fldNL, fldMin, fldMax, fldUNC, fldAVA
FROM tblAPS INNER JOIN getDPS ON (tblAPS.fldDTA = gDPS.fldDTA) AND (tblAPS.fldPK = gDPS.fldPK)
GROUP BY fldPK, fldDTA, fldCI, fldMN, fldMDN, fldIQ, fldSD, fldNDS, fldNIN, fldNL, fldMin, fldMax, fldUNC, fldAVA
HAVING tblAPS.fldMN<>-254
ORDER BY tblAPS.fldPK, tblAPS.fldCI;
RETURN;
END;
涉及的字段(正在查询的表中的 200 万条记录) fldPSD 上的聚集索引...
fldPK BIGINT NOT NULL,
fldDTA DATETIME NULL,
fldCI FLOAT NULL,
fldMN FLOAT NULL,
fldMDN FLOAT NULL,
fldIQ FLOAT NULL,
fldSD FLOAT NULL,
fldNDS BIGINT NULL,
fldNIN BIGINT NULL,
fldNL BIGINT NULL,
fldMin FLOAT NULL,
fldMax FLOAT NULL,
fldUNC VARCHAR(5) NULL,
fldAVA TINYINT NULL