SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Friday, July 07, 2006

stored proc with cursor for inserts becomes slower

SQL Apprentice Question
have a procedure that contains a trigger(fast forward read only) which
takes ~300000 records from a table in database A and inserts them in 4
tables in a database B. (We do this because we are in a transition to jump
from the old db schema to a new one and we keep the dbs synchronized)
My problem is that until ~3000 records the insert speed is
acceptable,afterwards the speed keeps reducing!!


What causes this?
Is there anything I can do to sustain a stable speed?



Celko Answers
Without code or DDL, it sounds like the
transaction ought to look something like this:

BEGIN
INSERT INTO [target DB].[table 1].[column list]
SELECT [table].[column list]
FROM [source DB].[table]
WHERE [search condition];


[check for errors]


INSERT INTO [target DB].[table 2].[column list]
SELECT [table].[column list]
FROM [source DB].[table]
WHERE [search condition];


[check for errors]


INSERT INTO [target DB].[table 3].[column list]
SELECT [table].[column list]
FROM [source DB].[table]
WHERE [search condition];


[check for errors]
END;
COMMIT;


I have written five cursors in my career and I know that three of them
could have been avoided if we had a CASE expression 20 years ago. The
other two were awful things (NP-complete problems) that should not have
been done in SQL at all.

No comments: