0

I have 2 tables "Vector" and "VectorElement". A vector has many elements so there is a foreign key relation between Vector and VectorElement with a cascaded delete.
Vector has a field VectorSize that contains the number the number of related records in VectorElement.
Obviously this field is redundant but it optimizes performance and keeps our queries simple as we are oftyen interested in the number of elements in a vector.
There is a trigger on VectorElement that updates the VectorSize field in Vector. This trigger works but gets very slow when many Vector records are deleted or inserted in one transaction.
When the Vectors get deleted, the cascaded delete deletes the VectorElements after which the trigger fires. Now the trigger does update the to-be-deleted Vector record which could cause some trouble but this also happens with inserts.

Here is the trigger:

CREATE TRIGGER [TFact].[AfterDeleteInsertVectorElement] 
   ON  [TFact].[VectorElement] 
   AFTER DELETE, INSERT
AS 
BEGIN
    SET NOCOUNT ON;

  WITH cteChangedVectors AS
  ( 
      SELECT DISTINCT i.VectorId 
      FROM inserted i
      UNION 
      SELECT DISTINCT i.VectorId 
      FROM deleted i
  )

    UPDATE 
        TFact.Vector
    SET 
        VectorSize = x.size
    FROM 
        Vector v
    JOIN
        (SELECT VectorId, COUNT(*) as size FROM TFact.VectorElement GROUP BY VectorId) x
        ON v.Id = x.VectorId
    JOIN cteChangedVectors chg ON chg.VectorId = v.Id

END
1
  • Also, reading it again, when you insert/ delete a lot of vectors, you might want to drop the trigger, apply the changes, recompute the VectorSize (maybe just for all vectors) and then reenable the trigger. You could do that in a transaction to keep the DB consistent for other users.
    – gjvdkamp
    Jan 21, 2011 at 15:42

4 Answers 4

1

Try tracking the total number of VectorElements using an indexed view.

See http://technet.microsoft.com/en-us/library/cc917715.aspx#XSLTsection124121120120

SQL Server knows how to track aggregates efficiently - that's cheaper than starting a piece of general purpose procedural code with every trigger call.

If you are on the SQL Server Enterprise, just create the view and your queries will be dynamically rewritten to use them.

Something like...

CREATE VIEW VectorSize AS
SELECT VectorId, COUNT(*)
FROM Vector NATURAL JOIN VectorElement
GROUP BY VectorId
GO
CREATE UNIQUE CLUSTERED INDEX VectorSizeInd ON VectorSize( VectorId )

SQL Server will then keep an automatically updated "hard-copy" of the vector sizes in the database.

2
  • Thanks tbarker, this eliminates the need for the trigger so that performance issue is gone. I'm curious though how your solution will perform when our tables grow. We expect millions of records. Jan 24, 2011 at 10:46
  • It should scale nicely as (IIRC) SQL Server knows it's just incrementing with each additional row. Although if you are bulk inserting millions of records in one batch, you might want to disable and then rebuild the index. Jan 24, 2011 at 20:06
0

The SQL looks over complex. And if you expect large sets, treat then separately

IF EXISTS (SELECT * FROM DELETED)
    UPDATE 
        V
    SET 
        VectorSize = x.size
    FROM 
        Vector V
        JOIN
        (SELECT
             VectorId, COUNT(*) as size
        FROM
             DELETED
        GROUP BY
             VectorId
         ) x
        ON v.Id = x.VectorId
ELSE
    UPDATE 
        V
    SET 
        VectorSize = x.size
    FROM 
        Vector V
        JOIN
        (SELECT
             VectorId, COUNT(*) as size
        FROM
             INSERTED
        GROUP BY
             VectorId
         ) x
        ON v.Id = x.VectorId
2
  • Not sure if this happens in the real world, but if a vectorlement is moved from one vector to another by an update, the new vector will not be updated by this query, because the vectorelement will have a deleted AND an inserted row in the trigger.
    – gjvdkamp
    Jan 21, 2011 at 15:34
  • @gjvdkamp: the trigger is defined "AFTER DELETE, INSERT". It isn't handling UPDATEs currently, so we'd have either rows in INSERTED or DELETED, not both.
    – gbn
    Jan 21, 2011 at 16:09
0

Is this any better?

UPDATE TFact.Vector
SET    VectorSize = x.size
FROM   Vector v
       inner join (
         select VectorId, count(*) size
         from   TFact.VectorElement
         where  VectorId in (select VectorId from cteChangedVectors)
         group by VectorId
       )x on x.VectorId = v.Id

Also make sure you have an index on TFact.VectorElement.VectorId if the DB gest big.

Regards GJ

0

Why not compute the "Delta" value from the inserted and deleted tables, rather than recompute the whole sum over the child table?

 UPDATE
     V
SET
     VectorSize = VectorSize + Delta
FROM
     Vector V
         inner join
     (select VectorID,SUM(Deltas) as Delta from
          (select VectorID,1 as Deltas from inserted union all
           select VectorID,-1 from deleted) t
     group by VectorID
     ) u
         on
            V.VectorID = u.VectorID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.