I have a very suspicious feeling that this update trigger is updating ALL rows on the target, not just those that satisfy the “update(shape)” test. Performance was fine ’till I added the second operation. A single spatial join occurs much faster, and this is a not a spatial index issue, as well, there are only a few records in this dataset.
ALTER TRIGGER [dbo].[GRSM_WETLANDS_Point_GIS_tbl_locations_update]
ON [dbo].[GRSM_WETLANDS_POINT]
after update
AS
BEGIN
SET NOCOUNT ON;
if UPDATE (shape)
update GRSM_WETLANDS_Point
set X_Coord =CASE WHEN u.shape.STDimension() = 2 THEN u.shape.STCentroid().STX ELSE u.shape.STEnvelope().STCentroid().STX END,
Y_Coord =CASE WHEN u.shape.STDimension() = 2 THEN u.shape.STCentroid().STY ELSE u.shape.STEnvelope().STCentroid().STY END
from inserted i
inner join GRSM_WETLANDS_POint u on i.GIS_Location_ID = u.GIS_Location_ID;
--second spatial operation
update GRSM_WETLANDS_Point
set QuadName = grsm.dbo.USGS_24K_TOPOMAP_BOUNDARIES.name
FROM GRSM_WETLANDS_POint i
inner join grsm.dbo.USGS_24K_TOPOMAP_BOUNDARIES
on i.GIS_Location_ID = i.GIS_Location_ID
WHERE (USGS_24K_TOPOMAP_BOUNDARIES.Shape.STContains(i.SHAPE) = 1) ;
end
Is my suspicion right?
Upated: Based on suggestion from Aaron…solves the fire on all rows issue.
update GRSM_WETLANDS_Point
set QuadName = grsm.dbo.USGS_24K_TOPOMAP_BOUNDARIES.name
FROM inserted i inner join GRSM_WETLANDS_POint u on i.GIS_Location_ID = u.GIS_Location_ID
left outer join grsm.dbo.USGS_24K_TOPOMAP_BOUNDARIES
on i.GIS_Location_ID = i.GIS_Location_ID
WHERE (USGS_24K_TOPOMAP_BOUNDARIES.Shape.STContains(i.SHAPE) = 1);
If
Shapecan’t be NULL, a better way to see if it has changed is to check if the values ininsertedanddeletedare different. For example:If
Shapeis nullable then you just have to add more conditions there to check, e.g.(You might not care if
Shapehas been updated toNULL, I’m just illustrating how to test for that case.)Since the operation can occur on multiple rows, and this condition will only identify that at least one such update has occurred (but not that ALL rows meet the condition), it may be better to have your operations include similar criteria in the WHERE clause. In fact I think you can perform both updates in a single operation, e.g.
In general, you seem to be having a lot of troubles implementing triggers, and make a lot of guesses about how the syntax should work. Have you considered forcing data updates to occur via stored procedures, where you can control all of this business logic but eliminate the complexity that the
insertedanddeletedpseudo tables add?