Marcos
Marcos

Reputation: 90

Delete very slow

I need to delete only the first appear of a record. This have a PK (preld_item, IDENTITY).

This is the very slow DELETE..

DELETE pd
FROM   preliquidaciones_deta pd
WHERE  pd.preld_item IN (SELECT MAX(p.preld_item)
                         FROM   preliquidaciones_deta p
                         WHERE  p.id_preliquidacion = '216'
                         GROUP  BY p.id_sds_linea_equipo,
                                   p.id_concepto
                         HAVING COUNT(p.id_sds_linea_equipo) > 1) 

Thanks

EDIT:

EDIT 2: @vulkanino i think in you answer and i modify the delete...

DECLARE @loPreldItem INT
DECLARE curItems CURSOR FOR (select max(p.preld_item)
    from preliquidaciones_deta p 
    where p.id_preliquidacion = '216' 
    group by p.id_sds_linea_equipo, p.id_concepto
    having count(p.id_sds_linea_equipo) > 1)

OPEN curItems
FETCH NEXT FROM curItems INTO @loPreldItem
WHILE @@FETCH_STATUS = 0
BEGIN
    DELETE FROM preliquidaciones_deta WHERE preld_item = @loPreldItem
    FETCH NEXT FROM curItems INTO @loPreldItem
END
CLOSE curItems
DEALLOCATE curItems

This works best but continue slow

EDIT 3: The execution plan of the first delete

delete from preliquidaciones_deta where preld_item in (  select max(preld_item)  from preliquidaciones_deta   where id_preliquidacion = '216'  group by id_sds_linea_equipo, id_concepto, preld_nse, preld_linea  having count(id_preliquidacion) > 1)
  |--Assert(WHERE:(CASE WHEN NOT [Expr1018] IS NULL THEN (0) ELSE CASE WHEN NOT [Expr1019] IS NULL THEN (1) ELSE NULL END END))
       |--Nested Loops(Left Semi Join, OUTER REFERENCES:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]), DEFINE:([Expr1019] = [PROBE VALUE]))
            |--Nested Loops(Left Semi Join, OUTER REFERENCES:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]), DEFINE:([Expr1018] = [PROBE VALUE]))
            |    |--Clustered Index Delete(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[IX_id_sds]), OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[IX_id_sds_linea_equipo]))
            |    |    |--Top(ROWCOUNT est 0)
            |    |         |--Sort(DISTINCT ORDER BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item] ASC))
            |    |              |--Nested Loops(Inner Join, OUTER REFERENCES:([Expr1007]) OPTIMIZED)
            |    |                   |--Filter(WHERE:([Expr1006]>(1)))
            |    |                   |    |--Compute Scalar(DEFINE:([Expr1006]=CONVERT_IMPLICIT(int,[Expr1023],0)))
            |    |                   |         |--Stream Aggregate(GROUP BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_sds_linea_equipo], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_concepto], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_nse], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_linea]) DEFINE:([Expr1023]=Count(*), [Expr1007]=MAX([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item])))
            |    |                   |              |--Sort(ORDER BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_sds_linea_equipo] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_concepto] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_nse] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_linea] ASC))
            |    |                   |                   |--Clustered Index Scan(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), WHERE:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_preliquidacion]='216'))
            |    |                   |--Clustered Index Seek(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), SEEK:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]=[Expr1007]) ORDERED FORWARD)
            |    |--Clustered Index Scan(OBJECT:([data_dealernet_lucom].[dbo].[liquidaciones_deta].[PK_liquidaciones_deta]), WHERE:([data_dealernet_lucom].[dbo].[liquidaciones_deta].[preld_item]=[data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]))
            |--Table Scan(OBJECT:([data_dealernet_lucom].[dbo].[liquidaciones_diferidas]), WHERE:([data_dealernet_lucom].[dbo].[liquidaciones_diferidas].[preld_item]=[data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]))

Upvotes: 2

Views: 386

Answers (3)

Martin Smith
Martin Smith

Reputation: 453877

You could try this alternative (as you state there won't be more than 2 rows per id_sds_linea_equipo, id_concepto this has the same semantics as your original query)

WITH T
     AS (SELECT *,
                RN = ROW_NUMBER() OVER 
                                 (PARTITION BY id_sds_linea_equipo, id_concepto
                                      ORDER BY preld_item)

         FROM   preliquidaciones_deta
         WHERE  id_preliquidacion = '216')
DELETE FROM T
WHERE RN > 1  

Upvotes: 4

SunWuKung
SunWuKung

Reputation: 557

Not sure if it applies to your case (because of the HAVING clause) but ORDER BY p.preld_item DESC LIMIT 1 could be much faster than the max aggregate.

Upvotes: 0

vulkanino
vulkanino

Reputation: 9134

Change IN with = since the subquery returns only on row. (to begin with).

Upvotes: 0

Related Questions