Foreign keys and SQL Server

Posts   
 
    
benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 18-May-2005 19:00:27   

This is more of a SQL Server question, but I was motivated to set up foreign key relationships so LLBLGen could detect the relationships. The problem is that it seems to make deletes extremely slow. Does anyone have suggestions? My database has millions of records and 'delete from table' is far slower than 'truncate table'.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-May-2005 10:16:42   

benles wrote:

This is more of a SQL Server question, but I was motivated to set up foreign key relationships so LLBLGen could detect the relationships. The problem is that it seems to make deletes extremely slow. Does anyone have suggestions? My database has millions of records and 'delete from table' is far slower than 'truncate table'.

That's because delete from table inserts for every row a rollback statement in the transaction log, so you can rollback the delete later on even when the transaction is committed simple_smile . Truncate table doesn't do that, it just throws away the data.

So with millions of rows, delete from table is pretty slow, as it has to store a lot of transaction log messages. if you don't need to rollback the action, just use truncate table, though when there might be a possibility you need it, use delete from table.

Frans Bouma | Lead developer LLBLGen Pro
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 19-May-2005 21:07:40   

Also if you are trying to delete a row from a table that is the pk side of a fk-pk relationship (and you have referential integrity checking on), sql has to check that there are no records with that pk in the fk table. This is can be slow if the fk table isn't indexed on the fk, as sql has to do a table scan for eack pk delete.

I had this situation yesterday, adding the proper index made the delete go much quicker.

benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 25-May-2005 15:07:04   

Otis wrote:

That's because delete from table inserts for every row a rollback statement in the transaction log, so you can rollback the delete later on even when the transaction is committed simple_smile . Truncate table doesn't do that, it just throws away the data.

So with millions of rows, delete from table is pretty slow, as it has to store a lot of transaction log messages. if you don't need to rollback the action, just use truncate table, though when there might be a possibility you need it, use delete from table.

Is there any way to specify that LLBLGen should turn off transaction logging before a DELETE? If foreign keys are set up to cascade deletes, then truncate will always fail with an error that an FK constraint would be violated, so the DELETE statement is the only option.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 25-May-2005 15:45:08   

You can't switch off transaction logging for DELETE statements.

Frans Bouma | Lead developer LLBLGen Pro