T-SQL – Deleting Small Number of Rows Hangs in Azure SQL Database

azure-sql-databaset-sql

So I'm just trying to delete some bad data from a one of our test databases. I've identified the rows to be deleted, and have written a simple query to handle this.

However, when run, the query hangs on 4% complete, and just sits there for hours. Our databases are hosted on Azure – For this test database we're running a Standard SO with 10 DTUs.

What am I missing here?

ql

Best Answer

Might be that cascading foreign key that table [Shift Offer] has pointing to table [Availability], slows down your delete operation

Try following:

1) create nonclustered index on column [Shift Offer][availability_id_fk] and try running your query again; see if nonclustered index speeds things up

2) if still running too long, try disable the foreign key at all (run sql command)

alter table [Shift Offer] nocheck constraint [paste your constraint name     
here I guess its availability_id_fk]

run delete command, and if succeeds and works fast, you will have to manually delete records from [Shift Offer] table, if it had any rows with availability_id_fk values that were deleted from [Availability] table, and then enable foreign key back