我编写了以下查询,以便在应用分组,计算,比较后从多个不同的表中将一个表中的结果获取。但是,此查询根本没有经过优化超过2小时才能获取结果。我不是来自开发过程,而是编写此查询以获取一些测试结果。
我尝试使用以下查询
DECLARE @LOCTS1 table (LOCID varchar(70),
PERIL varchar(70),
ACCGRPID varchar(70),
TIV bigint,
LIMITAMT bigint,
DEDUCTAMT bigint);
INSERT INTO @LOCTS1 (LOCID,
PERIL,
ACCGRPID,
TIV,
LIMITAMT,
DEDUCTAMT)
SELECT lc.LOCID,
lc.PERIL,
p.ACCGRPID,
SUM(VALUEAMT) AS TIV,
SUM(LIMITAMT) AS LIMITAMT,
SUM(DEDUCTAMT) AS DEDUCTAMT
FROM loccvg lc
JOIN Property p ON lc.LOCID = p.LOCID
GROUP BY lc.LOCID,
lc.PERIL,
p.ACCGRPID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for EQDET on location grouped from first joined query**--
DECLARE @LOCTS2 table (LOCID varchar(70),
EQSITELIMAMT bigint,
EQSITEDEDAMT bigint,
EQCOMBINEDLIMAMT bigint,
EQCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS2 (LOCID,
EQSITELIMAMT,
EQSITEDEDAMT,
EQCOMBINEDLIMAMT,
EQCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS EQSITELIMAMT,
SUM(SITEDEDAMT) AS EQSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS EQCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS EQCOMBINEDDEDAMT
FROM eqdet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for HUDET on location grouped from first joined query**--
DECLARE @LOCTS3 table (LOCID varchar(70),
HUSITELIMAMT bigint,
HUSITEDEDAMT bigint,
HUCOMBINEDLIMAMT bigint,
HUCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS3 (LOCID,
HUSITELIMAMT,
HUSITEDEDAMT,
HUCOMBINEDLIMAMT,
HUCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS HUSITELIMAMT,
SUM(SITEDEDAMT) AS HUSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS HUCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS HUCOMBINEDDEDAMT
FROM hudet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for TODET on location grouped from first joined query**--
DECLARE @LOCTS4 table (LOCID varchar(70),
TOSITELIMAMT bigint,
TOSITEDEDAMT bigint,
TOCOMBINEDLIMAMT bigint,
TOCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS4 (LOCID,
TOSITELIMAMT,
TOSITEDEDAMT,
TOCOMBINEDLIMAMT,
TOCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS TOSITELIMAMT,
SUM(SITEDEDAMT) AS TOSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS TOCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS TOCOMBINEDDEDAMT
FROM todet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for FLDET on location grouped from first joined query**--
DECLARE @LOCTS5 table (LOCID varchar(70),
FLSITELIMAMT bigint,
FLSITEDEDAMT bigint,
FLCOMBINEDLIMAMT bigint,
FLCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS5 (LOCID,
FLSITELIMAMT,
FLSITEDEDAMT,
FLCOMBINEDLIMAMT,
FLCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS FLSITELIMAMT,
SUM(SITEDEDAMT) AS FLSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS FLCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS FLCOMBINEDDEDAMT
FROM fldet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for FRDET on location grouped from first joined query**--
DECLARE @LOCTS6 table (LOCID varchar(70),
FRSITELIMAMT bigint,
FRSITEDEDAMT bigint,
FRCOMBINEDLIMAMT bigint,
FRCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS6 (LOCID,
FRSITELIMAMT,
FRSITEDEDAMT,
FRCOMBINEDLIMAMT,
FRCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS FRSITELIMAMT,
SUM(SITEDEDAMT) AS FRSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS FRCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS FRCOMBINEDDEDAMT
FROM frdet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Pick SITELIM, SITEDED, COMBINEDLIM, COMBINEDDED for TRDET on location grouped from first joined query**--
DECLARE @LOCTS7 table (LOCID varchar(70),
TRSITELIMAMT bigint,
TRSITEDEDAMT bigint,
TRCOMBINEDLIMAMT bigint,
TRCOMBINEDDEDAMT bigint);
INSERT INTO @LOCTS7 (LOCID,
TRSITELIMAMT,
TRSITEDEDAMT,
TRCOMBINEDLIMAMT,
TRCOMBINEDDEDAMT)
SELECT LOCID,
SUM(SITELIMAMT) AS TRSITELIMAMT,
SUM(SITEDEDAMT) AS TRSITEDEDAMT,
SUM(COMBINEDLIMAMT) AS TRCOMBINEDLIMAMT,
SUM(COMBINEDDEDAMT) AS TRCOMBINEDDEDAMT
FROM trdet
WHERE LOCID IN (SELECT LOCID FROM @LOCTS1)
GROUP BY LOCID;
--**Final Cmbined table having MAXDED, MINLIM, TIV, PERIL against each ACCGRPID, LOCID, PERIL after comparing**--
--Declare @MINLIM BIGINT
--Declare @MAXDED BIGINT
--Declare @LOCTS8
--table(ACCGRPID varchar(70),PERIL varchar(70),LOCID varchar(70),TIV BIGINT,
--LOCLIMIT BIGINT, SITELIMAMT BIGINT,COMBINEDLIMAMT BIGINT,
--LOCDED BIGINT, SITEDEDAMT BIGINT,COMBINEDDEDAMT BIGINT)
--Insert into @LOCTS8(ACCGRPID,PERIL,LOCID,TIV,LOCLIMIT,SITELIMAMT,COMBINEDLIMAMT,LOCDED,SITEDEDAMT,COMBINEDDEDAMT)
SELECT ACCGRPID,
PERIL,
lts1.LOCID,
TIV,
LIMITAMT AS LOCLIMIT,
EQSITELIMAMT,
EQCOMBINEDLIMAMT,
HUSITELIMAMT,
HUCOMBINEDLIMAMT,
TOSITELIMAMT,
TOCOMBINEDLIMAMT,
FLSITELIMAMT,
FLCOMBINEDLIMAMT,
FRSITELIMAMT,
FRCOMBINEDLIMAMT,
TRSITELIMAMT,
TRCOMBINEDLIMAMT,
CASE WHEN (DEDUCTAMT <= 1) THEN (TIV * DEDUCTAMT) ELSE DEDUCTAMT END AS LOCDED,
--EQDED
CASE WHEN (EQSITEDEDAMT <= 1) THEN (TIV * EQSITEDEDAMT) ELSE EQSITEDEDAMT END AS EQSITEDEDAMT,
CASE WHEN (EQCOMBINEDDEDAMT <= 1) THEN (TIV * EQCOMBINEDDEDAMT) ELSE EQCOMBINEDDEDAMT END AS EQCOMBINEDDEDAMT,
--HUDED
CASE WHEN (HUSITEDEDAMT <= 1) THEN (TIV * HUSITEDEDAMT) ELSE HUSITEDEDAMT END AS HUSITEDEDAMT,
CASE WHEN (HUCOMBINEDDEDAMT <= 1) THEN (TIV * HUCOMBINEDDEDAMT) ELSE HUCOMBINEDDEDAMT END AS HUCOMBINEDDEDAMT,
--TODED
CASE WHEN (TOSITEDEDAMT <= 1) THEN (TIV * TOSITEDEDAMT) ELSE TOSITEDEDAMT END AS TOSITEDEDAMT,
CASE WHEN (TOCOMBINEDDEDAMT <= 1) THEN (TIV * TOCOMBINEDDEDAMT) ELSE TOCOMBINEDDEDAMT END AS TOCOMBINEDDEDAMT,
--FLDED
CASE WHEN (FLSITEDEDAMT <= 1) THEN (TIV * FLSITEDEDAMT) ELSE FLSITEDEDAMT END AS FLSITEDEDAMT,
CASE WHEN (FLCOMBINEDDEDAMT <= 1) THEN (TIV * FLCOMBINEDDEDAMT) ELSE FLCOMBINEDDEDAMT END AS FLCOMBINEDDEDAMT,
--FRDED
CASE WHEN (FRSITEDEDAMT <= 1) THEN (TIV * FRSITEDEDAMT) ELSE FRSITEDEDAMT END AS FRSITEDEDAMT,
CASE WHEN (FRCOMBINEDDEDAMT <= 1) THEN (TIV * FRCOMBINEDDEDAMT) ELSE FRCOMBINEDDEDAMT END AS FRCOMBINEDDEDAMT,
--TRDED
CASE WHEN (TRSITEDEDAMT <= 1) THEN (TIV * TRSITEDEDAMT) ELSE TRSITEDEDAMT END AS TRSITEDEDAMT,
CASE WHEN (TRCOMBINEDDEDAMT <= 1) THEN (TIV * TRCOMBINEDDEDAMT) ELSE TRCOMBINEDDEDAMT END AS TRCOMBINEDDEDAMT,
(SELECT MIN(Col)
FROM (VALUES (LIMITAMT),
(EQSITELIMAMT),
(EQCOMBINEDLIMAMT),
(HUSITELIMAMT),
(HUCOMBINEDLIMAMT),
(TOSITELIMAMT),
(TOCOMBINEDLIMAMT),
(FLSITELIMAMT),
(FLCOMBINEDLIMAMT),
(FRSITELIMAMT),
(FRCOMBINEDLIMAMT),
(TRSITELIMAMT),
(TRCOMBINEDLIMAMT)) X (Col) ) AS MINLIM,
(SELECT MAX(Col)
FROM (VALUES (DEDUCTAMT),
(EQSITEDEDAMT),
(EQCOMBINEDDEDAMT),
(HUSITEDEDAMT),
(HUCOMBINEDDEDAMT),
(TOSITEDEDAMT),
(TOCOMBINEDDEDAMT),
(FLSITEDEDAMT),
(FLCOMBINEDDEDAMT),
(FRSITEDEDAMT),
(FRCOMBINEDDEDAMT),
(TRSITEDEDAMT),
(TRCOMBINEDDEDAMT)) X (Col) ) AS MAXDED
FROM @LOCTS1 lts1
JOIN @LOCTS2 lts2 ON lts1.LOCID = lts2.LOCID
JOIN @LOCTS3 lts3 ON lts1.LOCID = lts3.LOCID
JOIN @LOCTS4 lts4 ON lts1.LOCID = lts4.LOCID
JOIN @LOCTS5 lts5 ON lts1.LOCID = lts5.LOCID
JOIN @LOCTS6 lts6 ON lts1.LOCID = lts6.LOCID
JOIN @LOCTS7 lts7 ON lts1.LOCID = lts7.LOCID
ORDER BY 3;
我希望它可以优化并以更快的方式运行。