执行时间非常长,如何减少它

时间:2017-06-25 06:13:16

标签: sql performance oracle10g query-optimization

我们的生产数据库中有一个表格。 它的大小为220 gb 当我执行此处指定的以下查询时 虚假场景需要花费很多时间。 我们可以减少以下查询的时间。

帮助将受到高度赞赏。

DELETE FROM testcpy1_bkp
WHERE    (num, dt, cd) IN (  SELECT num, dt, 899
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) != 899 OR MIN (cd) != 899)
OR (    (num, dt, cd) IN
(  SELECT num, dt, MAX (cd)
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) = 899 AND MIN (cd) = 899)
AND ROWID NOT IN (  SELECT MIN (ROWID)
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) = 899 AND MIN (cd) = 899));


DELETE FROM testcpy1_bkp t1
WHERE ROWID NOT IN
(SELECT MAX (ROWID)
FROM testcpy1_bkp t2
WHERE t1.num = t2.num AND t1.dt = T2.DT AND t1.cd = t2.cd);

1 个答案:

答案 0 :(得分:0)

您的查询正在对要删除的同一个表执行多次查找。根据您的信息,我认为这是一个非常大的表。这使得查找成本很高。

我会尝试从DELETE语句的WHERE子句中挑选子查询,并将这些值选择到临时表中,然后根据临时表中的值执行DELETE语句。这可以显着提高您的查询性能。

它看起来像这样:

INSERT INTO #TempTestCPY1
SELECT num, dt, 899 AS cd
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) != 899 OR MIN (cd) != 899

INSERT INTO #TempTestCPY2
SELECT num, dt, MAX (cd) AS cd
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) = 899 AND MIN (cd) = 899

INSERT INTO #TempTestCPY3
SELECT MIN (ROWID) AS ROWID
FROM testcpy1_bkp
GROUP BY num, dt
HAVING MAX (cd) = 899 AND MIN (cd) = 899

DELETE FROM testcpy1_bkp
WHERE
    (num, dt, cd) IN 
        (SELECT num, dt, cd FROM #TempTestCPY1)
    OR ((num, dt, cd) IN
        (SELECT num, dt, cd FROM #TempTestCPY2)
        AND ROWID NOT IN 
        (SELECT ROWID FROM #TempTestCPY3));

INSERT INTO #TempTestCPY4
SELECT MAX (ROWID) ROWID
FROM testcpy1_bkp
GROUP BY num, dt, cd

DELETE FROM testcpy1_bkp t1
WHERE ROWID NOT IN
(SELECT ROWID FROM #TempTestCPY4 WHERE t1.num = t2.num AND t1.dt = T2.DT AND t1.cd = t2.cd);

我还会考虑你的桌子上是否有任何索引,它会在你执行DELETE之前支付给DROP,之后是REBUILD。例如,从集群列存储索引进行DELETING可能是一项非常昂贵的操作。从堆表中删除的成本较低,因此删除和重建聚簇列存储索引通常会得到回报。

根据您提供的信息,这就是我所能想到的。希望它有所帮助!